Changeset 980
- Timestamp:
- Oct 22, 2007, 2:55:19 AM (15 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/commandline_test.cc
r979 r980 340 340 } 341 341 } 342 343 344 error << "Testing OptionArg required ..."; 345 { 346 OptionArg<std::string> required(cmd, "required", "required", true); 347 int ac = 1; 348 char* av[] = { "test_prog" }; 349 try{ 350 cmd.parse(ac,av); 351 error << "failed\n"; 352 ok =false; 353 } 354 catch (...) { 355 error << "ok\n"; 356 } 357 } 358 359 342 360 return ok; 343 361 } -
trunk/yat/utility/Option.cc
r975 r980 36 36 37 37 Option::Option(CommandLine& cmd, std::string flag, std::string desc) 38 : description_(desc), present_(false)38 : cmd_(cmd), description_(desc), present_(false) 39 39 { 40 40 if (flag.empty()) … … 58 58 Option::~Option(void) 59 59 { 60 } 61 62 63 const CommandLine& Option::cmd(void) const 64 { 65 return cmd_; 60 66 } 61 67 -
trunk/yat/utility/Option.h
r965 r980 99 99 void validate(void); 100 100 101 protected: 102 /** 103 \return const reference to CommandLine Option belongs to. 104 */ 105 const CommandLine& cmd(void) const; 106 101 107 private: 102 108 virtual void do_parse(std::vector<std::string>::iterator, … … 124 130 125 131 132 const CommandLine& cmd_; 126 133 std::string description_; 127 134 std::string long_name_; -
trunk/yat/utility/OptionArg.h
r975 r980 26 26 27 27 #include "Option.h" 28 #include "CommandLine.h" 28 29 29 30 #include <stdexcept> … … 56 57 "h,help" for having both short and long option name 57 58 \param desc string used in help display 59 \param required If true option must be found in commandline or 60 exception is thrown in validation 58 61 */ 59 OptionArg(CommandLine& cmd, std::string name, std::string desc) 60 : Option(cmd, name, desc) {} 62 OptionArg(CommandLine& cmd, std::string name, std::string desc, 63 bool required=false) 64 : Option(cmd, name, desc), required_(required) {} 61 65 62 66 /** … … 65 69 T value(void) const { return value_; } 66 70 71 protected: 72 inline bool required(void) const { return required_; } 73 67 74 private: 68 T def_;75 bool required_; 69 76 T value_; 70 77 … … 73 80 { 74 81 if ( first->size()>2 && (*first)[0]=='-' && (*first)[1]!='-'){ 75 std::string str("option requires an argument -- "); 76 str += short_name(); 77 throw std::runtime_error(str); 82 std::stringstream ss; 83 ss << "option requires an argument -- " << short_name() << "\n" 84 << cmd().try_help(); 85 throw std::runtime_error(ss.str()); 78 86 } 79 87 if (first+1==last ) { 80 88 if (first->size()>2){ 81 89 std::stringstream ss; 82 ss << "option `--" << long_name() << "' requires an argument"; 90 ss << "option `--" << long_name() << "' requires an argument\n" 91 << cmd().try_help(); 83 92 throw std::runtime_error(ss.str()); 84 93 } 85 94 else { 86 std::string str("option requires an argument -- "); 87 str += short_name(); 88 throw std::runtime_error(str); 95 std::stringstream ss; 96 ss << "option requires an argument -- " << short_name() << "\n" 97 << cmd().try_help(); 98 throw std::runtime_error(ss.str()); 89 99 } 90 100 } … … 117 127 /** 118 128 */ 119 virtual void do_validate(void) const {} 129 void do_validate(void) const 130 { 131 if (required_ && !present()) { 132 std::stringstream ss; 133 ss << "mandatory opition `"; 134 if (long_name().size()) 135 ss << long_name(); 136 else 137 ss << short_name(); 138 ss << "' not given\n"; 139 ss << cmd().try_help(); 140 throw std::runtime_error(ss.str()); 141 } 142 do_validate2(); 143 } 120 144 145 146 virtual void do_validate2(void) const {} 121 147 }; 122 148 -
trunk/yat/utility/OptionFile.cc
r979 r980 25 25 #include "FileUtil.h" 26 26 27 #include <stdexcept> 27 28 #include <string> 28 29 … … 32 33 33 34 34 OptionFile::OptionFile(CommandLine& cmd, std::string flag, 35 std::string desc, bool exist, std::string bits) 36 : OptionArg<std::string>(cmd, flag, desc), exist_(exist), bits_(bits) 37 { 38 } 35 OptionFile::OptionFile(CommandLine& cmd, std::string flag, std::string desc, 36 bool required, bool exist, std::string bits) 37 : OptionArg<std::string>(cmd, flag, desc, required), exist_(exist), 38 bits_(bits) {} 39 39 40 40 41 void OptionFile::do_validate () const41 void OptionFile::do_validate2() const 42 42 { 43 43 if (!present()) -
trunk/yat/utility/OptionFile.h
r979 r980 47 47 "f,file" for having both short and long option name 48 48 \param desc string used in help display 49 \param required If true option must be found in commandline or 50 exception is thrown in validation 49 51 \param exist if true File must exist 50 52 \param bits used to check permission on file, see 51 53 FileUtil 52 54 */ 53 OptionFile(CommandLine& cmd, std::string name, 54 std::string desc, bool exist=false, std::string bits="");55 OptionFile(CommandLine& cmd, std::string name, std::string desc, 56 bool required=false, bool exist=false, std::string bits=""); 55 57 56 58 57 59 private: 58 void do_validate () const;60 void do_validate2() const; 59 61 60 62 bool exist_;
Note: See TracChangeset
for help on using the changeset viewer.