Changeset 1468


Ignore:
Timestamp:
Sep 2, 2008, 4:57:50 PM (15 years ago)
Author:
Peter
Message:

fixes #413

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/commandline_test.cc

    r1466 r1468  
    5757    suite.add(test_failures(suite));
    5858    suite.add(test_option_name_clash(suite));
    59     //suite.add(test_free_arg(suite));
     59    suite.add(test_free_arg(suite));
    6060  }
    6161  catch (std::runtime_error& e) {
     
    459459
    460460  CommandLine cmd;
     461  cmd.allow_free_args(2);
    461462  OptionHelp help(cmd);
    462463  int ac = 3;
  • trunk/yat/utility/CommandLine.cc

    r1466 r1468  
    4545
    4646  CommandLine::CommandLine(std::string str)
    47     : description_(str)
     47    : description_(str), free_arg_max_(0)
    4848  {}
    4949
     
    8383        long_options_.find(no_name)==long_options_.end())
    8484      long_options_[no_name] = &option;
     85  }
     86
     87
     88  void CommandLine::allow_free_args(size_t n)
     89  {
     90    free_arg_max_ = n;
    8591  }
    8692
     
    155161        else {
    156162          free_arg_.push_back(*arg);
    157           std::stringstream ss;
    158           ss << ": invalid option -- " << *arg << "\n"
    159              << try_help() << "\n";
    160           throw cmd_error(ss.str());
    161          
     163          if (free_arg_.size()>free_arg_max_) {
     164            std::stringstream ss;
     165            ss << ": invalid option -- " << *arg << "\n"
     166               << try_help() << "\n";
     167            throw cmd_error(ss.str());
     168          }
    162169        }
    163170      }
  • trunk/yat/utility/CommandLine.h

    r1466 r1468  
    107107
    108108    /**
     109       \brief Allow at most \a n free arguments.
     110
     111       An free argument is an argument not associated with an Option,
     112       allowing commandlines such as \code prog foo bar \endcode
     113     */
     114    void allow_free_args(size_t n);
     115
     116    /**
    109117       \brief parse the commandline
    110118
     
    134142    std::string description_;
    135143    std::vector<std::string> free_arg_;
     144    size_t free_arg_max_;
    136145    std::vector<Option*> options_;
    137146    std::map<char, Option*> short_options_;
Note: See TracChangeset for help on using the changeset viewer.