35 static const char * falseStr =
"false";
36 static const char * trueStr =
"true";
37 static const char * blankStr =
"";
39 static option::ArgStatus Arg_Required(
const option::Option& option,
43 static_cast<void>(msg);
46 return option::ARG_OK;
48 return option::ARG_ILLEGAL;
54 if(options)
delete[] options;
55 if(buffer)
delete[] buffer;
60 if(options)
delete[] options;
63 if(buffer)
delete[] buffer;
72 const char *
const shortOpt,
73 const char *
const longOpt,
78 it = map.find(optName);
83 if(argT == argType::Optional)
85 descriptions.push_back({
nOpts, argT, shortOpt, longOpt, option::Arg::Optional,
""});
87 else if(argT == argType::Required)
89 descriptions.push_back({
nOpts, argT, shortOpt, longOpt, Arg_Required,
""});
93 descriptions.push_back({
nOpts, argT, shortOpt, longOpt, option::Arg::None,
""});
96 map.insert({optName,
nOpts});
97 typeMap.insert({optName, argT});
106 std::vector<std::string> * nonOptions
109 argc-=(argc>0); argv+=(argc>0);
112 if(descriptions.back().index != 0)
114 descriptions.push_back({
nOpts, 0,
"",
"", option::Arg::None,
""});
115 descriptions.push_back({0,0,0,0,0,0});
119 option::Stats stats(descriptions.data(), argc, argv);
120 options =
new option::Option[stats.options_max];
121 buffer =
new option::Option[stats.buffer_max];
123 option::Parser
parse(
false, descriptions.data(), argc, argv, options, buffer);
127 nonOptions->resize(
parse.nonOptionsCount());
129 for(
int i=0;i<
parse.nonOptionsCount(); ++i)
131 (*nonOptions)[i] =
parse.nonOption(i);
138 mapIterator it = map.find(key);
142 std::cerr <<
"oh no\n";
146 int typeDesc = typeMap[key];
149 if(options[it->second].type() == argType::None)
151 if(typeDesc == argType::False)
return trueStr;
152 if(typeDesc == argType::True)
return falseStr;
156 if(typeDesc == argType::False || typeDesc == argType::True)
158 if(options[it->second].last()->type() == argType::False)
return falseStr;
162 if(options[it->second].arg == 0)
return blankStr;
164 return options[it->second].last()->arg;
169 mapIterator it = map.find(key);
171 if(it == map.end())
return -1;
173 return options[it->second].count();
177 const std::string & key
180 mapIterator it = map.find(key);
185 std::cerr <<
"oh no\n";
189 int typeDesc = typeMap[key];
192 if(options[it->second].type() == argType::None)
195 if(typeDesc == argType::False)
202 if(typeDesc == argType::True)
214 if(typeDesc == argType::False || typeDesc == argType::True)
218 if(options[it->second].last()->type() == argType::False) args[0] = falseStr;
219 else args[0] = trueStr;
223 if(options[it->second].arg == 0)
229 int N = options[it->second].count();
235 for (option::Option* opt = options[it->second]; opt != NULL && i < N; opt = opt->next())
244 mapIterator it = map.find(key);
246 if(it == map.end())
return false;
248 if( options[it->second].type() != argType::None)
return true;
255 return options[
nOpts].count();
264 for (option::Option* opt = options[
nOpts]; opt != NULL; opt = opt->next())
266 unk.push_back( opt->name );
void parse(int argc, char **argv, std::vector< std::string > *nonOptions=0)
Parse the command line.
const char * operator[](const std::string &key)
Get the value of the option, if present.
void clear()
Clear the memory held by this object.
int numUnknown()
Get the number of unknown options found by the parser.
void add(const std::string &optName, const char *const shortOpt, const char *const longOpt, int argT)
Add a command line option target.
int count(const std::string &key)
Get the number of times the option was set on the command line.
unsigned int nOpts
The number of options added.
void getAll(std::vector< std::string > &args, const std::string &key)
Fill a vector of strings with the arguments supplied for key, last to first.
~clOptions()
D'tor. Deletes the options and buffer pointers.
int unknown(std::vector< std::string > &unk)
Get a vector of the unknown options found by the parser.
bool optSet(const std::string &key)
Test whether this option was set.