Changeset 1426
- Timestamp:
- Aug 21, 2008, 12:26:31 AM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/commandline_test.cc
r1275 r1426 40 40 bool test_file(yat::test::Suite& error); 41 41 bool test_failures(yat::test::Suite& error); 42 bool test_option_name_clash(yat::test::Suite& suite); 42 43 43 44 int main(int argc, char* argv[]) … … 54 55 suite.add(test_file(suite)); 55 56 suite.add(test_failures(suite)); 57 suite.add(test_option_name_clash(suite)); 56 58 } 57 59 catch (std::runtime_error& e) { 58 suite.err() << "exception thrown\n" << e.what() << std::endl; 60 suite.err() << "Error: unexpected exception thrown\n" << e.what() 61 << std::endl; 62 suite.add(false); 59 63 } 60 64 … … 360 364 suite.err() << "Testing OptionFile... "; 361 365 { 362 int ac = 2;363 char* av[] = { "test_prog", "--clones", "commandline_test.cc" 366 int ac = 7; 367 char* av[] = { "test_prog", "--clones", "commandline_test.cc", 364 368 "--data", "commandline_test.cc", "-o", 365 369 "commandline_test.cc"}; … … 370 374 } 371 375 372 373 374 375 376 bool test_option_name_clash(yat::test::Suite& suite) 377 { 378 bool ok=true; 379 suite.err() << "Testing long option name clash ..."; 380 try { 381 CommandLine cmd; 382 OptionSwitch op1(cmd, "opt", "bla bla"); 383 OptionSwitch op2(cmd, "o,opt", "other bla"); 384 ok=false; 385 suite.err() << "failed\n"; 386 } 387 catch (std::runtime_error& e) { 388 suite.err() << "ok\n"; 389 } 390 suite.err() << "Testing short option name clash ..."; 391 try { 392 CommandLine cmd; 393 OptionSwitch op1(cmd, "o", "bla bla"); 394 OptionSwitch op2(cmd, "o,opt", "other bla"); 395 ok=false; 396 suite.err() << "failed\n"; 397 } 398 catch (std::runtime_error& e) { 399 suite.err() << "ok\n"; 400 } 401 402 return ok; 403 } 404 405 406 407 -
trunk/yat/utility/CommandLine.cc
r1275 r1426 56 56 void CommandLine::add(Option& option) 57 57 { 58 if (option.long_name().size()) 58 if (option.long_name().size()) { 59 if (long_options_.find(option.long_name())!=long_options_.end()) { 60 std::stringstream ss; 61 ss << "yat::utility::Commandline: two options with long_name: " 62 << option.long_name(); 63 throw std::runtime_error(ss.str()); 64 } 59 65 long_options_[option.long_name()] = &option; 60 if (option.short_name()) 66 } 67 if (option.short_name()) { 68 if (short_options_.find(option.short_name())!=short_options_.end()) { 69 std::stringstream ss; 70 ss << "yat::utility::Commandline: two options with short_name: " 71 << option.short_name(); 72 throw std::runtime_error(ss.str()); 73 } 61 74 short_options_[option.short_name()] = &option; 75 } 62 76 if (option.long_name().size() || option.short_name()) 63 77 options_.push_back(&option); 64 78 // allow `no-switch' for option `switch' 65 79 OptionSwitch* o = dynamic_cast<OptionSwitch*>(&option); 80 std::string no_name = std::string("no-")+option.long_name(); 66 81 if (option.long_name().size() && o && 67 !( o->long_name().size()>2 && o->long_name().substr(0,3)=="no-")) 68 long_options_[std::string("no-")+option.long_name()] = &option; 82 !( o->long_name().size()>2 && o->long_name().substr(0,3)=="no-") && 83 long_options_.find(no_name)==long_options_.end()) 84 long_options_[no_name] = &option; 69 85 } 70 86
Note: See TracChangeset
for help on using the changeset viewer.