Ignore:
Timestamp:
Aug 29, 2006, 3:28:29 AM (17 years ago)
Author:
Peter
Message:

Changed interface of CommandLine? so short option is set with char. Also added som documentation.

Location:
trunk/c++_tools/utility
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/c++_tools/utility/CommandLine.cc

    r594 r598  
    3737
    3838  CommandLine::CommandLine(void)
     39    : max_argument_(0), min_argument_(0)
    3940  {
    4041  }
     
    4950
    5051
    51   Option* CommandLine::add(const std::string& short_name,
     52  Option* CommandLine::add(char short_name,
    5253                           const std::string& long_name,
    5354                           Option::argument_type arg,
    5455                           const std::string& describtion)
    5556  {
    56     if (short_name.empty() && long_name.empty()){
    57       std::cerr << "warning: cannot add parameter with no name" << std::endl;
     57    using namespace std;
     58    if (short_name=='\0' && long_name.empty()){
     59      cerr << "warning: cannot add parameter with no name" << endl;
    5860      return NULL;
    5961    }
     
    6264    if (!long_name.empty()){
    6365      key2option::iterator i = param_.lower_bound(long_name);
    64       if (i!=param_.end() && i->first == short_name){
    65         std::cerr << "warning: " << long_name << " already defined.\n";
     66      if (i!=param_.end() && i->first == string(1,short_name)){
     67        cerr << "warning: " << long_name << " already defined.\n";
    6668        delete o;
    6769        return NULL;
    6870      }
    6971      else
    70         param_.insert(i, std::make_pair(long_name, o));
    71     }
    72     if (!short_name.empty()){
    73       key2option::iterator i = param_.lower_bound(short_name);
    74       if (i!=param_.end() && i->first == short_name){
    75         std::cerr << "warning: " << short_name << " already defined.\n";
     72        param_.insert(i, make_pair(long_name, o));
     73    }
     74    if (short_name!='\0'){
     75      key2option::iterator i = param_.lower_bound(string(1,short_name));
     76      if (i!=param_.end() && i->first == string(1,short_name)){
     77        cerr << "warning: " << short_name << " already defined.\n";
    7678        delete o;
    7779        return NULL;
    7880      }
    7981      else
    80         param_.insert(i, std::make_pair(short_name, o));
    81     }
    82 
     82        param_.insert(i, make_pair(string(1,short_name), o));
     83    }
    8384    options_.push_back(o);
    8485    return o;
     
    9192  {
    9293    if (name.size()==1)
    93       add(name, "", arg, description);
     94      add(name[0], "", arg, description);
    9495    else
    95       add("", name, arg, description);
     96      add('\0', name, arg, description);
    9697  }
    9798 
    9899
    99   void CommandLine::check_double(const std::string& str) const
    100   {
    101     double value;
    102     std::stringstream ss(str);
    103     if (!(ss>>value)){
    104     }
    105   }
    106 
    107 
    108   void CommandLine::check_int(const std::string& str) const
    109   {
    110     int value;
    111     std::stringstream ss(str);
    112     if (!(ss>>value)){
    113       std::cerr << app_name_ << ": invalid value `" << str << "'" << std::endl;
    114       exit(-1);
    115     }
    116     std::cout << str << " as int: " << value << std::endl;
    117    
    118   }
    119 
     100  void CommandLine::add_parameter(char name,
     101                                  Option::argument_type arg,
     102                                  const std::string& description)
     103  {
     104    add(name, "", arg, description);
     105  }
     106 
    120107
    121108  void CommandLine::parse(int argc,const char* argv[])
     
    217204
    218205
    219   void CommandLine::set_help(const std::string& shortname,
     206  void CommandLine::set_help(char shortname,
    220207                             const std::string& longname,
    221208                             const std::string& descr)
     
    269256    key2option::const_iterator i = param_.find(key);
    270257    if (i==param_.end()){
    271       std::cerr << "Error: value(" << key << "): illegal key\n";
     258      std::cerr << "error: illegal key: value(\"" << key << "\"): \n";
    272259      exit(-1);
    273260    }
     
    293280  int CommandLine::value_int(const std::string& key) const
    294281  {
     282    std::string str = value(key);
    295283    key2option::const_iterator i = param_.find(key);
    296284    if (i->second->arg_type()!=Option::int_arg)
    297285      std::cerr << "error: option " << key << " does not take int argument"
    298286                << std::endl;
    299     std::string str = value(key);
    300287    int value;
    301288    std::stringstream ss(str);
  • trunk/c++_tools/utility/CommandLine.h

    r594 r598  
    3636namespace utility {
    3737
     38  ///
     39  /// @brief Class for parsing the command line.
     40  ///
     41  /// @see Option
     42  ///
    3843  class CommandLine
    3944  {
     
    5156
    5257    ///
    53     /// @todo doc
    54     ///
    55     void add_parameter(const std::string& name,
     58    /// @brief Function to add a parameter.
     59    ///
     60    /// @param long_name string key such as "help" for --help flag
     61    /// @param telling what kind argument this option expects
     62    /// @param description string used in help display
     63    ///
     64    void add_parameter(const std::string& long_name,
    5665                       Option::argument_type arg = Option::no_arg,
    5766                       const std::string& description = std::string());
    5867
    5968    ///
    60     /// @todo doc
    61     ///
    62     inline void add_parameter(const std::string& short_name,
     69    /// @brief Function to add a parameter.
     70    ///
     71    /// @param short_name one character key such as 'h' for -h flag
     72    /// @param telling what kind argument this option expects
     73    /// @param description string used in help display
     74    ///
     75    void add_parameter(const char short_name,
     76                       Option::argument_type arg = Option::no_arg,
     77                       const std::string& description = std::string());
     78
     79    ///
     80    /// @brief Function to add a parameter.
     81    ///
     82    /// @param short_name one character key such as 'h' for -h flag
     83    /// @param long_name string key such as "help" for --help flag
     84    /// @param telling what kind argument this option expects
     85    /// @param description string used in help display
     86    ///
     87    inline void add_parameter(const char short_name,
    6388                              const std::string& long_name,
    6489                              Option::argument_type arg = Option::no_arg,
     
    6691    { add(short_name, long_name, arg, description); }
    6792
    68 
    69     ///
    70     /// @todo doc
    71     ///
    72     void set_help(const std::string& shortname = "h",
     93    ///
     94    /// @return vector of arguments not associated to a specific parameter
     95    ///
     96    const std::vector<std::string>& arguments(void) const;
     97
     98    ///
     99    /// If more than maximal number of arguments is found during
     100    /// parsing an error message is displayed followed by exit.
     101    ///
     102    /// @return maximal number of arguments allowed.
     103    ///
     104    inline u_int& max_argument(void) { return max_argument_; }
     105
     106    ///
     107    /// If less than minimal number of arguments is found during
     108    /// parsing an error message is displayed followed by exit.
     109    ///
     110    /// @return minimal number of arguments allowed.
     111    ///
     112    inline u_int& min_argument(void) { return min_argument_; }
     113
     114    ///
     115    /// @brief parse the commandline
     116    ///
     117    void parse(int argc, const char* argv[]);
     118
     119    ///
     120    /// @return true if @a parameter has been detected in parsing.
     121    ///
     122    bool present(const std::string& parameter) const;
     123
     124    ///
     125    /// @brief allow help.
     126    ///
     127    void set_help(char shortname = 'h',
    73128                  const std::string& longname = "help",
    74129                  const std::string& descr = "display this help and exit");
    75130
    76131    ///
     132    /// The @a description will be included in help display giving a
     133    /// general explanation what program is doing.
     134    ///
     135    inline void set_general_description(const std::string& description)
     136    { general_description_=description; }
     137
     138    ///
     139    /// @note Using function for @a parameter not added previously
     140    /// will cause an error message and exit.
     141    ///
     142    /// @return argument value for @a parameter
     143    ///
     144    std::string value(const std::string& parameter) const;
     145
     146    ///
     147    /// If the value for @a parameter is not a valid double an error
     148    /// message will be displayed followed by an exit.
     149    ///
     150    /// @note Using function for @a parameter not added previously
     151    /// will cause an error message and exit.
     152    ///
     153    /// @return argument value for @a parameter
     154    ///
     155    double value_double(const std::string& parameter) const;
     156
     157    ///
     158    /// If the value for @a parameter is not a valid double an error
     159    /// message will be displayed followed by an exit.
     160    ///
     161    /// @note Using function for @a parameter not added previously
     162    /// will cause an error message and exit.
     163    ///
    77164    /// @todo doc
    78165    ///
    79     const std::vector<std::string>& arguments(void) const;
    80 
    81     ///
    82     /// @todo doc
    83     ///
    84     inline void set_general_description(const std::string& descr)
    85     { general_description_=descr; }
    86 
    87     ///
    88     /// @todo doc
    89     ///
    90     void parse(int argc, const char* argv[]);
    91 
    92     ///
    93     /// @todo doc
    94     ///
    95     bool present(const std::string& parameter) const;
    96 
    97     ///
    98     /// @todo doc
    99     ///
    100     bool update(const std::string& key, const std::string& value);
    101 
    102     ///
    103     /// @todo doc
    104     ///
    105     std::string value(const std::string& parameter) const;
    106 
    107     ///
    108     /// @todo doc
    109     ///
    110     double value_double(const std::string& parameter) const;
    111 
    112     ///
    113     /// @todo doc
    114     ///
    115166    int value_int(const std::string& parameter) const;
    116167
    117168    ///
    118     /// @todo doc
    119     ///
    120     void reset();
    121 
    122     ///
    123     /// @todo doc
    124     ///
    125     u_int min_argument(void);
    126 
    127     ///
    128     /// @todo doc
    129     ///
    130     u_int max_argument(void);
    131 
    132     ///
    133     /// @todo doc
     169    /// Function to display the help message.
    134170    ///
    135171    void usage(void) const;
    136172
    137173    private:
    138     Option* add(const std::string& short_name,
     174    Option* add(char short_name,
    139175                const std::string& long_name,
    140176                Option::argument_type arg,
    141177                const std::string& describtion);
    142178
    143     void check_double(const std::string&) const;
    144     void check_int(const std::string&) const;
     179    inline bool is_long_option(const std::string& str)
     180    { return (str.size()>3 && str[0]=='-' && str[1]=='-'); }
     181   
     182    inline bool is_short_option(const std::string& str)
     183    { return (str.size()==2 && str[0]=='-' && isalpha(str[1])); }
     184
    145185    void print_try_help(void) const;
    146186    std::string split(std::string&, char) const;
     187    bool update(const std::string& key, const std::string& value);
     188
    147189
    148190    typedef std::map<std::string, Option*> key2option;
     
    151193    const Option* help_option_;
    152194    std::string general_description_;
     195    u_int max_argument_;
     196    u_int min_argument_;
    153197    key2option param_;
    154198    std::list<Option*> options_;
     
    157201  };
    158202
    159   inline bool is_long_option(const std::string& str)
    160   { return (str.size()>3 && str[0]=='-' && str[1]=='-'); }
    161  
    162   inline bool is_short_option(const std::string& str)
    163   { return (str.size()==2 && str[0]=='-' && isalpha(str[1])); }
    164 
    165203
    166204}} // end of namespace utility and namespace theplu
  • trunk/c++_tools/utility/Option.cc

    r594 r598  
    3232
    3333
    34   Option::Option(std::string name, std::string long_name, argument_type arg,
     34  Option::Option(char short_name, std::string long_name, argument_type arg,
    3535                 std::string mess)
    36     : arg_type_(arg), long_name_(long_name), mess_(mess), short_name_(name),
    37       present_(false)
     36    : arg_type_(arg), long_name_(long_name), mess_(mess),
     37      short_name_(short_name), present_(false)
    3838  {
    39     if (name.size()>1 && long_name.empty()){
    40       long_name_=name;
    41       short_name_="";
    42     }
    4339  }
    4440
     
    4743  {
    4844    using std::cerr;
    49     if (!short_name_.empty()){
     45    if (short_name_!='\0'){
    5046      cerr << " -" << short_name_;
    5147      if (!long_name_.empty())
  • trunk/c++_tools/utility/Option.h

    r594 r598  
    3131
    3232  ///
    33   /// @brief Option
     33  /// @brief Container of variables for an option. @see CommandLine
    3434  ///
    3535  class Option
     
    3838
    3939    ///
    40     /// different types of arguments
     40    /// different types of arguments to an option
    4141    ///
    4242    enum argument_type {
     
    4848    /// @brief Constructor
    4949    ///
    50     Option(std::string short_name, std::string long_name,
     50    /// @param short_name one character key such as 'h' for -h flag
     51    /// @param long_name string key such as "help" for --help flag
     52    /// @param telling what kind argument this option expects
     53    /// @param desc string used in help display
     54    ///
     55    Option(char short_name, std::string long_name,
    5156           argument_type arg, std::string desc);
    5257           
    5358
    54    
     59    ///
     60    /// @return argument type for option
     61    ///
    5562    inline const argument_type& arg_type(void) const { return arg_type_; }
    5663   
    57     inline const std::string& name(void) const
    58     { return !long_name_.empty() ? long_name_ : short_name_ ; }
    59     inline const std::string& short_name(void) const { return short_name_; }
     64    ///
     65    /// @return long name unless long name is empty in which case the
     66    /// short one character name is returned.
     67    ///
     68    inline std::string name(void) const
     69    { return !long_name_.empty() ? long_name_ : std::string(&short_name_) ; }
     70
     71    ///
     72    /// @return short name
     73    ///
     74    inline char short_name(void) const { return short_name_; }
     75
     76    ///
     77    /// @return long name
     78    ///
    6079    inline const std::string& long_name(void) const { return long_name_; }
     80
     81    ///
     82    /// sends output to std::cerr of type
     83    ///
     84    /// -v, --verbose  explain what is going on
     85    ///
    6186    void print(void) const;
     87
     88    ///
     89    /// @return true if option has been detected in parsing
     90    ///
    6291    inline bool& present(void) { return present_; }
     92
     93    ///
     94    /// @return argument value
     95    ///
    6396    inline std::string& value(void) { return value_; }
    6497
     
    68101    std::string long_name_;
    69102    std::string mess_;
    70     std::string short_name_;
     103    char short_name_;
    71104    bool present_;
    72105    std::string value_;
Note: See TracChangeset for help on using the changeset viewer.