Changeset 1351
- Timestamp:
- Apr 3, 2011, 8:30:06 PM (12 years ago)
- Location:
- trunk/yat
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/CommandLine.cc
r1336 r1351 1 // $Id: CommandLine.cc 2 384 2010-12-22 14:03:36Z peter $1 // $Id: CommandLine.cc 2458 2011-04-03 15:14:46Z peter $ 2 2 3 3 /* … … 115 115 arguments.push_back(argv[i]); 116 116 std::vector<std::string>::iterator arg(arguments.begin()); 117 std::vector<std::string>::iterator end(arguments.end()); 117 118 stringstream ss(*arg++); 118 119 // keeping string after last / 119 120 while (getline(ss, program_name_,'/')) {} 120 121 122 bool ok=true; 123 std::string error_message; 124 for (; arg!=end; ++arg) { 125 try { 126 parse(arg, end); 127 } 128 catch (cmd_error& e) { 129 if (ok) { // we only store first error 130 ok = false; 131 error_message = e.what(); 132 } 133 } 134 } 135 // validate all options 121 136 try { 122 for (; arg!=arguments.end(); ++arg) { 123 if (is_long_option(*arg)) { 124 std::string key(arg->substr(2)); 125 std::stringstream ss2(key); 126 getline(ss2, key, '='); 127 std::string value; 128 getline(ss2, value, '\0'); 129 if (!value.empty()){ 130 *arg = value; 131 *(--arg) = std::string("--")+key; 132 } 133 else 134 *arg = key; 135 std::map<std::string, Option*>::const_iterator 136 iter(long_options_.find(key)); 137 if (iter!=long_options_.end()) 138 iter->second->parse(arg, arguments.end()); 139 else if (key.size()>3 && key.substr(0,3)=="no-") { 140 iter = long_options_.find(key.substr(3)); 141 if (iter!=long_options_.end()) 142 iter->second->parse(arg, arguments.end()); 143 } 144 else if (iter==long_options_.end()) { 145 std::stringstream ss3; 146 ss3 << "unrecognized option `" << key << "'\n" << try_help(); 147 throw cmd_error(ss3.str()); 148 } 149 } 150 else if (is_short_option(*arg)) { 151 size_t size=arg->size(); 152 for (size_t i=1; i<size; ++i){ 153 std::map<char, Option*>::const_iterator 154 iter(short_options_.find((*arg)[i])); 155 if (iter==short_options_.end()) { 156 std::stringstream ss2; 157 ss2 << ": invalid option -- " << (*arg)[i] << "\n" 158 << try_help() << "\n"; 159 throw cmd_error(ss2.str()); 160 } 161 else 162 iter->second->parse(arg, arguments.end()); 163 } 164 } 165 else { 166 free_arg_.push_back(*arg); 167 if (free_arg_.size()>free_arg_max_) { 168 std::stringstream ss2; 169 ss2 << ": invalid option -- " << *arg << "\n" 170 << try_help() << "\n"; 171 throw cmd_error(ss2.str()); 172 } 173 } 174 } 175 for_each(options_.begin(),options_.end(), 137 for_each(options_.begin(), options_.end(), 176 138 std::mem_fun(&Option::validate)); 177 139 } 178 catch (cmd_error& e){ 140 catch (cmd_error& e) { 141 if (ok) { // we only store first error 142 ok = false; 143 error_message = e.what(); 144 } 145 } 146 147 if (!ok) { 148 std::stringstream ss; 149 ss << program_name_ << ": " << error_message; 150 throw cmd_error(ss.str()); 151 } 152 } 153 154 155 void CommandLine::parse_long(std::vector<std::string>::iterator& arg, 156 std::vector<std::string>::iterator& last) 157 { 158 std::string key(arg->substr(2)); 159 std::stringstream ss2(key); 160 getline(ss2, key, '='); 161 std::string value; 162 getline(ss2, value, '\0'); 163 if (!value.empty()){ 164 *arg = value; 165 *(--arg) = std::string("--")+key; 166 } 167 else 168 *arg = key; 169 std::map<std::string, Option*>::const_iterator 170 iter(long_options_.find(key)); 171 if (iter!=long_options_.end()) 172 iter->second->parse(arg, last); 173 else if (key.size()>3 && key.substr(0,3)=="no-") { 174 iter = long_options_.find(key.substr(3)); 175 if (iter!=long_options_.end()) 176 iter->second->parse(arg, last); 177 } 178 else if (iter==long_options_.end()) { 179 std::stringstream ss3; 180 ss3 << "unrecognized option `" << key << "'\n" << try_help(); 181 throw cmd_error(ss3.str()); 182 } 183 } 184 185 void CommandLine::parse_short(std::vector<std::string>::iterator& arg, 186 std::vector<std::string>::iterator& last) 187 { 188 size_t size=arg->size(); 189 for (size_t i=1; i<size; ++i){ 190 std::map<char, Option*>::const_iterator 191 iter(short_options_.find((*arg)[i])); 192 if (iter==short_options_.end()) { 193 std::stringstream ss2; 194 ss2 << ": invalid option -- " << (*arg)[i] << "\n" 195 << try_help() << "\n"; 196 throw cmd_error(ss2.str()); 197 } 198 else 199 iter->second->parse(arg, last); 200 } 201 } 202 203 204 void CommandLine::parse_free_arg(std::vector<std::string>::iterator& arg, 205 std::vector<std::string>::iterator& last) 206 { 207 free_arg_.push_back(*arg); 208 if (free_arg_.size()>free_arg_max_) { 179 209 std::stringstream ss2; 180 ss2 << program_name_ << ": " << e.what(); 210 ss2 << ": invalid option -- " << *arg << "\n" 211 << try_help() << "\n"; 181 212 throw cmd_error(ss2.str()); 182 213 } 183 184 } 185 214 } 215 216 217 void CommandLine::parse(std::vector<std::string>::iterator& first, 218 std::vector<std::string>::iterator& last) 219 { 220 if (is_long_option(*first)) 221 parse_long(first, last); 222 else if (is_short_option(*first)) 223 parse_short(first, last); 224 else 225 parse_free_arg(first, last); 226 227 } 228 186 229 187 230 bool CommandLine::parsed(void) const -
trunk/yat/CommandLine.h
r1336 r1351 2 2 #define _theplu_yat_utility_commandline_ 3 3 4 //$Id: CommandLine.h 2 384 2010-12-22 14:03:36Z peter $4 //$Id: CommandLine.h 2458 2011-04-03 15:14:46Z peter $ 5 5 6 6 /* 7 7 Copyright (C) 2007, 2008, 2009 Jari Häkkinen, Peter Johansson 8 Copyright (C) 2010 Peter Johansson8 Copyright (C) 2010, 2011 Peter Johansson 9 9 10 10 This file is part of the yat library, http://dev.thep.lu.se/yat … … 191 191 bool is_long_option(std::string str) const; 192 192 bool is_short_option(std::string str) const; 193 193 void parse(std::vector<std::string>::iterator& first, 194 std::vector<std::string>::iterator& last); 195 void parse_long(std::vector<std::string>::iterator& first, 196 std::vector<std::string>::iterator& last); 197 void parse_short(std::vector<std::string>::iterator& first, 198 std::vector<std::string>::iterator& last); 199 void parse_free_arg(std::vector<std::string>::iterator& first, 200 std::vector<std::string>::iterator& last); 194 201 std::string description_; 195 202 std::vector<std::string> free_arg_; … … 202 209 203 210 // use cond to make doxygen ignore this privat class 204 /// \cond 211 /// \cond yat_ignore_this 205 212 struct OptionCompare 206 213 { -
trunk/yat/Exception.cc
r1146 r1351 1 // $Id: Exception.cc 2 303 2010-07-28 17:47:58Z peter $1 // $Id: Exception.cc 2459 2011-04-03 15:42:44Z peter $ 2 2 3 3 /* … … 37 37 38 38 cmd_error::cmd_error(std::string message) 39 : std::runtime_error(message)39 : runtime_error(message) 40 40 {} 41 41 42 42 43 43 errno_error::errno_error(std::string message) 44 : std::runtime_error(message + strerror(errno))44 : runtime_error(message + strerror(errno)) 45 45 {} 46 46 47 47 48 48 IO_error::IO_error(std::string message) 49 : std::runtime_error("IO_error: " + message)49 : runtime_error("IO_error: " + message) 50 50 {} 51 51 -
trunk/yat/Exception.cc.patch
r1307 r1351 1 --- Exception.cc.orig 201 0-03-06 20:31:52.000000000 -05002 +++ Exception.cc 201 0-03-06 20:32:12.000000000 -05001 --- Exception.cc.orig 2011-04-03 14:20:22.000000000 -0400 2 +++ Exception.cc 2011-04-03 14:18:09.000000000 -0400 3 3 @@ -21,8 +21,6 @@ 4 4 … … 8 8 - 9 9 #include <cerrno> 10 #include <cstring> 10 11 #include <stdexcept> 11 #include <string> 12 @@ -46,17 +44,6 @@ 12 @@ -47,17 +45,6 @@ 13 13 {} 14 14 15 15 16 16 - GSL_error::GSL_error(std::string message) 17 - : std::runtime_error("GSL_error: " + message)17 - : runtime_error("GSL_error: " + message) 18 18 - {} 19 19 - 20 20 - 21 21 - GSL_error::GSL_error(std::string message, int gsl_status) 22 - : std::runtime_error("GSL_error: " + message + " " +22 - : runtime_error("GSL_error: " + message + " " + 23 23 - gsl_strerror(gsl_status)) 24 24 - {} … … 26 26 - 27 27 IO_error::IO_error(std::string message) 28 : std::runtime_error("IO_error: " + message)28 : runtime_error("IO_error: " + message) 29 29 {} -
trunk/yat/Exception.h
r1193 r1351 2 2 #define _theplu_yat_utility_exception_ 3 3 4 // $Id: Exception.h 2 331 2010-10-02 16:07:14Z peter $4 // $Id: Exception.h 2459 2011-04-03 15:42:44Z peter $ 5 5 6 6 /* … … 51 51 \brief Class used for error reported from Commandline or Option. 52 52 */ 53 class cmd_error : public std::runtime_error53 class cmd_error : public runtime_error 54 54 { 55 55 public: … … 69 69 \since New in yat 0.7 70 70 */ 71 class errno_error : public std::runtime_error71 class errno_error : public runtime_error 72 72 { 73 73 public: … … 85 85 exceptions. 86 86 */ 87 class GSL_error : public std::runtime_error87 class GSL_error : public runtime_error 88 88 { 89 89 public: … … 107 107 exceptions. 108 108 */ 109 class IO_error : public std::runtime_error109 class IO_error : public runtime_error 110 110 { 111 111 public: -
trunk/yat/config_public.h.in
r1027 r1351 2 2 #define _theplu_yat_utility_config_public_ 3 3 4 // $Id: config_public.h.in 2 140 2009-12-31 22:43:29Z peter $4 // $Id: config_public.h.in 2431 2011-03-06 20:04:31Z peter $ 5 5 6 6 /* 7 7 Copyright (C) 2008 Jari Häkkinen, Peter Johansson 8 Copyright (C) 2009 Peter Johansson8 Copyright (C) 2009, 2011 Peter Johansson 9 9 10 10 This file is part of the yat library, http://dev.thep.lu.se/yat … … 31 31 #undef YAT_HAVE_GCC_DEPRECATED 32 32 33 /// Version of yat in string format 34 #undef YAT_VERSION 35 /// First digit in VERSION triplet 36 #undef YAT_MAJOR_VERSION 37 /// Second digit in VERSION triplet 38 #undef YAT_MINOR_VERSION 39 /// Third digit in VERSION triplet or zero if VERSION is MAJOR.MINOR 40 #undef YAT_PATCH_VERSION 41 /// Set to true when releasing tarball 42 #undef YAT_DEV_BUILD 43 33 44 #endif -
trunk/yat/split.cc
r1152 r1351 1 // $Id: split.cc 2 305 2010-08-02 14:14:32Z peter $1 // $Id: split.cc 2434 2011-03-08 16:52:23Z peter $ 2 2 3 3 /* -
trunk/yat/split.h
r1152 r1351 2 2 #define _theplu_yat_utility_split_ 3 3 4 // $Id: split.h 2 305 2010-08-02 14:14:32Z peter $4 // $Id: split.h 2434 2011-03-08 16:52:23Z peter $ 5 5 6 6 /* -
trunk/yat/utility.h
r1336 r1351 2 2 #define _theplu_yat_utility_utility_ 3 3 4 // $Id: utility.h 2 386 2010-12-22 20:12:49Z peter $4 // $Id: utility.h 2451 2011-03-28 23:16:14Z peter $ 5 5 6 6 /* … … 8 8 Copyright (C) 2006 Jari Häkkinen 9 9 Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson 10 Copyright (C) 2009, 2010 Peter Johansson10 Copyright (C) 2009, 2010, 2011 Peter Johansson 11 11 12 12 This file is part of the yat library, http://dev.thep.lu.se/yat … … 62 62 OutputIterator result); 63 63 64 /** 65 \brief convert T to a string 66 67 T is supposed to be a numerical type. 68 69 \since new in yat 0.8 70 */ 71 template<typename T> 72 std::string convert(T input); 64 73 65 74 /** … … 259 268 260 269 // template implementations 270 template<typename T> 271 std::string convert(T input) 272 { 273 std::ostringstream ss; 274 ss << input; 275 return ss.str(); 276 } 277 278 261 279 template<typename T> 262 280 T convert(const std::string& s)
Note: See TracChangeset
for help on using the changeset viewer.