Changeset 598 for trunk/c++_tools/utility
- Timestamp:
- Aug 29, 2006, 3:28:29 AM (17 years ago)
- Location:
- trunk/c++_tools/utility
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/c++_tools/utility/CommandLine.cc
r594 r598 37 37 38 38 CommandLine::CommandLine(void) 39 : max_argument_(0), min_argument_(0) 39 40 { 40 41 } … … 49 50 50 51 51 Option* CommandLine::add(c onst std::string&short_name,52 Option* CommandLine::add(char short_name, 52 53 const std::string& long_name, 53 54 Option::argument_type arg, 54 55 const std::string& describtion) 55 56 { 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; 58 60 return NULL; 59 61 } … … 62 64 if (!long_name.empty()){ 63 65 key2option::iterator i = param_.lower_bound(long_name); 64 if (i!=param_.end() && i->first == s hort_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"; 66 68 delete o; 67 69 return NULL; 68 70 } 69 71 else 70 param_.insert(i, std::make_pair(long_name, o));71 } 72 if ( !short_name.empty()){73 key2option::iterator i = param_.lower_bound(s hort_name);74 if (i!=param_.end() && i->first == s hort_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"; 76 78 delete o; 77 79 return NULL; 78 80 } 79 81 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 } 83 84 options_.push_back(o); 84 85 return o; … … 91 92 { 92 93 if (name.size()==1) 93 add(name , "", arg, description);94 add(name[0], "", arg, description); 94 95 else 95 add( "", name, arg, description);96 add('\0', name, arg, description); 96 97 } 97 98 98 99 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 120 107 121 108 void CommandLine::parse(int argc,const char* argv[]) … … 217 204 218 205 219 void CommandLine::set_help(c onst std::string&shortname,206 void CommandLine::set_help(char shortname, 220 207 const std::string& longname, 221 208 const std::string& descr) … … 269 256 key2option::const_iterator i = param_.find(key); 270 257 if (i==param_.end()){ 271 std::cerr << " Error: value(" << key << "): illegal key\n";258 std::cerr << "error: illegal key: value(\"" << key << "\"): \n"; 272 259 exit(-1); 273 260 } … … 293 280 int CommandLine::value_int(const std::string& key) const 294 281 { 282 std::string str = value(key); 295 283 key2option::const_iterator i = param_.find(key); 296 284 if (i->second->arg_type()!=Option::int_arg) 297 285 std::cerr << "error: option " << key << " does not take int argument" 298 286 << std::endl; 299 std::string str = value(key);300 287 int value; 301 288 std::stringstream ss(str); -
trunk/c++_tools/utility/CommandLine.h
r594 r598 36 36 namespace utility { 37 37 38 /// 39 /// @brief Class for parsing the command line. 40 /// 41 /// @see Option 42 /// 38 43 class CommandLine 39 44 { … … 51 56 52 57 /// 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, 56 65 Option::argument_type arg = Option::no_arg, 57 66 const std::string& description = std::string()); 58 67 59 68 /// 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, 63 88 const std::string& long_name, 64 89 Option::argument_type arg = Option::no_arg, … … 66 91 { add(short_name, long_name, arg, description); } 67 92 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', 73 128 const std::string& longname = "help", 74 129 const std::string& descr = "display this help and exit"); 75 130 76 131 /// 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 /// 77 164 /// @todo doc 78 165 /// 79 const std::vector<std::string>& arguments(void) const;80 81 ///82 /// @todo doc83 ///84 inline void set_general_description(const std::string& descr)85 { general_description_=descr; }86 87 ///88 /// @todo doc89 ///90 void parse(int argc, const char* argv[]);91 92 ///93 /// @todo doc94 ///95 bool present(const std::string& parameter) const;96 97 ///98 /// @todo doc99 ///100 bool update(const std::string& key, const std::string& value);101 102 ///103 /// @todo doc104 ///105 std::string value(const std::string& parameter) const;106 107 ///108 /// @todo doc109 ///110 double value_double(const std::string& parameter) const;111 112 ///113 /// @todo doc114 ///115 166 int value_int(const std::string& parameter) const; 116 167 117 168 /// 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. 134 170 /// 135 171 void usage(void) const; 136 172 137 173 private: 138 Option* add(c onst std::string&short_name,174 Option* add(char short_name, 139 175 const std::string& long_name, 140 176 Option::argument_type arg, 141 177 const std::string& describtion); 142 178 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 145 185 void print_try_help(void) const; 146 186 std::string split(std::string&, char) const; 187 bool update(const std::string& key, const std::string& value); 188 147 189 148 190 typedef std::map<std::string, Option*> key2option; … … 151 193 const Option* help_option_; 152 194 std::string general_description_; 195 u_int max_argument_; 196 u_int min_argument_; 153 197 key2option param_; 154 198 std::list<Option*> options_; … … 157 201 }; 158 202 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 165 203 166 204 }} // end of namespace utility and namespace theplu -
trunk/c++_tools/utility/Option.cc
r594 r598 32 32 33 33 34 Option::Option( std::stringname, std::string long_name, argument_type arg,34 Option::Option(char short_name, std::string long_name, argument_type arg, 35 35 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) 38 38 { 39 if (name.size()>1 && long_name.empty()){40 long_name_=name;41 short_name_="";42 }43 39 } 44 40 … … 47 43 { 48 44 using std::cerr; 49 if ( !short_name_.empty()){45 if (short_name_!='\0'){ 50 46 cerr << " -" << short_name_; 51 47 if (!long_name_.empty()) -
trunk/c++_tools/utility/Option.h
r594 r598 31 31 32 32 /// 33 /// @brief Option33 /// @brief Container of variables for an option. @see CommandLine 34 34 /// 35 35 class Option … … 38 38 39 39 /// 40 /// different types of arguments 40 /// different types of arguments to an option 41 41 /// 42 42 enum argument_type { … … 48 48 /// @brief Constructor 49 49 /// 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, 51 56 argument_type arg, std::string desc); 52 57 53 58 54 59 /// 60 /// @return argument type for option 61 /// 55 62 inline const argument_type& arg_type(void) const { return arg_type_; } 56 63 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 /// 60 79 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 /// 61 86 void print(void) const; 87 88 /// 89 /// @return true if option has been detected in parsing 90 /// 62 91 inline bool& present(void) { return present_; } 92 93 /// 94 /// @return argument value 95 /// 63 96 inline std::string& value(void) { return value_; } 64 97 … … 68 101 std::string long_name_; 69 102 std::string mess_; 70 std::stringshort_name_;103 char short_name_; 71 104 bool present_; 72 105 std::string value_;
Note: See TracChangeset
for help on using the changeset viewer.