Changeset 1426


Ignore:
Timestamp:
Aug 21, 2008, 12:26:31 AM (15 years ago)
Author:
Peter
Message:

fixes #414

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/commandline_test.cc

    r1275 r1426  
    4040bool test_file(yat::test::Suite& error);
    4141bool test_failures(yat::test::Suite& error);
     42bool test_option_name_clash(yat::test::Suite& suite);
    4243
    4344int main(int argc, char* argv[])
     
    5455    suite.add(test_file(suite));
    5556    suite.add(test_failures(suite));
     57    suite.add(test_option_name_clash(suite));
    5658  }
    5759  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);
    5963  }
    6064
     
    360364  suite.err() << "Testing OptionFile... ";
    361365  {
    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",
    364368                   "--data", "commandline_test.cc", "-o",
    365369                   "commandline_test.cc"};
     
    370374}
    371375
    372 
    373 
    374 
    375 
     376bool 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  
    5656  void CommandLine::add(Option& option)
    5757  {
    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      }
    5965      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      }
    6174      short_options_[option.short_name()] = &option;
     75    }
    6276    if (option.long_name().size() || option.short_name())
    6377      options_.push_back(&option);
    6478    // allow `no-switch' for option `switch'
    6579    OptionSwitch* o = dynamic_cast<OptionSwitch*>(&option);
     80    std::string no_name = std::string("no-")+option.long_name();
    6681    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;
    6985  }
    7086
Note: See TracChangeset for help on using the changeset viewer.