1 #ifndef _theplu_yat_utility_option_arg_
2 #define _theplu_yat_utility_option_arg_
27 #include "CommandLine.h"
28 #include "Exception.h"
74 void print_arg(std::string arg) { print_arg_ = arg; }
81 if (!
cmd().parsed()) {
82 std::string s(
"OptionArg::value called before Commandline was parsed");
83 throw std::logic_error(s);
100 inline bool required(
void)
const {
return required_; }
103 std::string print_arg_;
107 void do_parse(std::vector<std::string>::iterator& first,
108 const std::vector<std::string>::iterator& last)
110 if ( first->size()>2 && (*first)[0]==
'-' && (*first)[1]!=
'-'){
111 std::stringstream ss;
112 ss <<
"option requires an argument -- " <<
short_name() <<
"\n"
116 if (first+1==last ) {
117 if (first->size()>2){
118 std::stringstream ss;
119 ss <<
"option `--" <<
long_name() <<
"' requires an argument\n"
121 throw cmd_error(ss.str());
124 std::stringstream ss;
125 ss <<
"option requires an argument -- " <<
short_name() <<
"\n"
127 throw cmd_error(ss.str());
131 if ( *(first+1)->begin() ==
'"' && *((first+1)->end()-1) ==
'"')
132 *(first+1) = (first+1)->substr(1, (first+1)->size()-2);
133 assign(value_, *(++first));
136 void assign(std::string& lhs,
const std::string& rhs )
142 void assign(T1& lhs,
const std::string& rhs )
145 lhs = convert<T1>(rhs);
147 catch (runtime_error& e) {
148 std::stringstream sstr(rhs);
149 sstr <<
"invalid argument";
150 sstr <<
"`" << rhs <<
"' for `";
156 throw cmd_error(sstr.str());
162 void do_validate(
void)
const
165 std::stringstream ss;
166 ss <<
"mandatory option `";
171 ss <<
"' not given\n";
173 throw cmd_error(ss.str());
179 virtual void do_validate2(
void)
const {}
181 virtual std::string print3(
void)
const