1 #include <msp/strings/format.h>
11 add_option("help", help, NO_ARG).set_help("Displays this help");
16 for(OptionImpl *i: opts)
18 for(ArgumentImpl *i: args)
22 GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, ArgType a)
25 throw invalid_argument("GetOpt::add_option");
26 if(t.is_list() && a!=REQUIRED_ARG)
27 throw invalid_argument("GetOpt::add_option");
29 for(auto i=opts.begin(); i!=opts.end(); )
31 if((s!=0 && (*i)->get_short()==s) || (*i)->get_long()==l)
40 opts.push_back(new OptionImpl(s, l, t, a));
44 GetOpt::ArgumentImpl &GetOpt::add_argument(const string &n, const Store &t, ArgType y)
47 throw invalid_argument("GetOpt::add_argument");
49 bool have_list = false;
50 bool have_optional = false;
51 for(const ArgumentImpl *a: args)
53 if(a->is_list_store())
55 else if(a->get_type()==OPTIONAL_ARG)
59 if(have_optional && (t.is_list() || y!=OPTIONAL_ARG))
60 throw invalid_argument("GetOpt::add_argument");
61 if(have_list && (t.is_list() || y==OPTIONAL_ARG))
62 throw invalid_argument("GetOpt::add_argument");
64 args.push_back(new ArgumentImpl(n, t, y));
68 GetOpt::OptionImpl &GetOpt::get_option(char s)
70 auto i = find_if(opts, [s](const OptionImpl *o){ return o->get_short()==s; });
73 throw usage_error(string("Unknown option -")+s);
76 GetOpt::OptionImpl &GetOpt::get_option(const string &l)
78 auto i = find_if(opts, [&l](const OptionImpl *o){ return o->get_long()==l; });
81 throw usage_error(string("Unknown option --")+l);
84 void GetOpt::operator()(unsigned argc, const char *const *argv)
88 /* Arguments must first be collected into an array to handle the case
89 where a variable-length argument list is followed by fixed arguments. */
100 i += process_long(argv+i);
103 i += process_short(argv+i);
106 args_raw.push_back(argv[i++]);
110 args_raw.push_back(argv[i]);
113 for(auto j=args.begin(); j!=args.end(); ++j)
115 if((*j)->is_list_store())
117 unsigned end = args_raw.size();
118 for(auto k=j; ++k!=args.end(); )
120 if(i==end && (*j)->get_type()==REQUIRED_ARG)
121 throw usage_error((*j)->get_name()+" is required");
123 (*j)->process(args_raw[i]);
127 if(i<args_raw.size())
128 (*j)->process(args_raw[i++]);
129 else if((*j)->get_type()==REQUIRED_ARG)
130 throw usage_error((*j)->get_name()+" is required");
134 // XXX Enable this when get_args() is completely removed
135 /*if(i<args_raw.size())
136 throw usage_error("Extra positional arguments");*/
138 catch(const usage_error &e)
141 throw usage_error(e.what(), "Usage: "+generate_usage(argv[0]));
145 throw usage_error(string("Help for ")+argv[0]+":", "\nUsage:\n "+generate_usage(argv[0], true)+"\n\n"+generate_help());
148 unsigned GetOpt::process_long(const char *const *argp)
151 const char *arg = argp[0]+2;
153 // See if the argument contains an =
155 for(; arg[equals] && arg[equals]!='='; ++equals) ;
157 OptionImpl &opt = get_option(string(arg, equals));
160 // Process the part after the = as option argument
161 opt.process(arg+equals+1);
162 else if(opt.get_arg_type()==REQUIRED_ARG)
165 throw usage_error("--"+string(arg)+" requires an argument");
167 // Process the next argument as option argument
168 opt.process(argp[1]);
177 unsigned GetOpt::process_short(const char *const *argp)
180 const char *arg = argp[0]+1;
182 // Loop through all characters in the argument
185 OptionImpl &opt = get_option(*arg);
187 if(arg[1] && opt.get_arg_type()!=NO_ARG)
189 // Need an option argument and we have characters left - use them
193 else if(opt.get_arg_type()==REQUIRED_ARG)
196 throw usage_error("-"+string(1, *arg)+" requires an argument");
198 // Use the next argument as option argument
199 opt.process(argp[1]);
209 string GetOpt::generate_usage(const string &argv0, bool compact) const
211 string result = argv0;
213 result += " [options]";
216 for(const OptionImpl *o: opts)
221 result += format("-%c", o->get_short());
222 if(!o->get_long().empty())
224 else if(o->get_arg_type()==OPTIONAL_ARG)
225 result += format("[%s]", o->get_metavar());
226 else if(o->get_arg_type()==REQUIRED_ARG)
227 result += format(" %s", o->get_metavar());
229 if(!o->get_long().empty())
231 result += format("--%s", o->get_long());
233 if(o->get_arg_type()==OPTIONAL_ARG)
234 result += format("[=%s]", o->get_metavar());
235 else if(o->get_arg_type()==REQUIRED_ARG)
236 result += format("=%s", o->get_metavar());
242 for(const ArgumentImpl *a: args)
245 if(a->get_type()==OPTIONAL_ARG)
247 result += format("<%s>", a->get_name());
248 if(a->is_list_store())
250 if(a->get_type()==OPTIONAL_ARG)
257 string GetOpt::generate_help() const
259 bool any_short = any_of(opts.begin(), opts.end(), [](const OptionImpl *o){ return o->get_short(); });
261 string::size_type maxw = 0;
262 vector<string> switches;
263 for(const OptionImpl *o: opts)
268 swtch += format("-%c", o->get_short());
269 if(!o->get_long().empty())
271 else if(o->get_arg_type()==OPTIONAL_ARG)
272 swtch += format("[%s]", o->get_metavar());
273 else if(o->get_arg_type()==REQUIRED_ARG)
274 swtch += format(" %s", o->get_metavar());
278 if(!o->get_long().empty())
280 swtch += format("--%s", o->get_long());
282 if(o->get_arg_type()==OPTIONAL_ARG)
283 swtch += format("[=%s]", o->get_metavar());
284 else if(o->get_arg_type()==REQUIRED_ARG)
285 swtch += format("=%s", o->get_metavar());
287 switches.push_back(swtch);
288 maxw = max(maxw, swtch.size());
291 vector<string> pargs;
292 for(const ArgumentImpl *a: args)
294 string parg = format("<%s>", a->get_name());
295 pargs.push_back(parg);
296 maxw = max(maxw, parg.size());
300 result += "Options:\n";
301 auto j = switches.begin();
302 for(auto i=opts.begin(); i!=opts.end(); ++i, ++j)
303 result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
306 result += "\nArguments:\n";
308 for(auto i=args.begin(); i!=args.end(); ++i, ++j)
309 result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
316 GetOpt::OptionImpl::OptionImpl(char s, const string &l, const Store &t, ArgType a):
323 GetOpt::OptionImpl::~OptionImpl()
328 GetOpt::OptionImpl &GetOpt::OptionImpl::set_help(const string &h)
334 GetOpt::OptionImpl &GetOpt::OptionImpl::set_help(const string &h, const string &m)
341 GetOpt::OptionImpl &GetOpt::OptionImpl::bind_seen_count(unsigned &c)
347 void GetOpt::OptionImpl::process()
349 if(arg_type==REQUIRED_ARG)
350 throw usage_error("--"+lng+" requires an argument");
354 *ext_seen_count = seen_count;
360 catch(const exception &e)
362 throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
366 void GetOpt::OptionImpl::process(const string &arg)
369 throw usage_error("--"+lng+" takes no argument");
373 *ext_seen_count = seen_count;
379 catch(const exception &e)
381 throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
386 GetOpt::ArgumentImpl::ArgumentImpl(const string &n, const Store &t, ArgType a):
392 GetOpt::ArgumentImpl::~ArgumentImpl()
397 GetOpt::ArgumentImpl &GetOpt::ArgumentImpl::set_help(const string &h)
403 void GetOpt::ArgumentImpl::process(const string &arg)
409 catch(const exception &e)
411 throw usage_error("Invalid "+name+" ("+e.what()+")");