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
char short_name(void) const
std::string try_help(void) const
Some useful functions are placed here.
bool present(void) const
Get if option was found in cmd.
bool required(void) const
Definition: OptionArg.h:100
void value(T v)
set value
Definition: OptionArg.h:93
Option with argument.
Definition: OptionArg.h:51
void print_arg(std::string arg)
Definition: OptionArg.h:74
Class for parsing the command line.
Definition: CommandLine.h:98
T value(void) const
Definition: OptionArg.h:79
const CommandLine & cmd(void) const
OptionArg(CommandLine &cmd, std::string name, std::string desc, bool required=false)
Constructor.
Definition: OptionArg.h:64
std::string long_name(void) const
Class used for error reported from Commandline or Option.
Definition: Exception.h:53
Container of variables for an option.
Definition: Option.h:37