1 #include <msp/strings/format.h>
12 add_option("help", help, NO_ARG).set_help("Displays this help");
17 for(OptionImpl *i: opts)
19 for(ArgumentImpl *i: args)
23 GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, ArgType a)
26 throw invalid_argument("GetOpt::add_option");
27 if(t.is_list() && a!=REQUIRED_ARG)
28 throw invalid_argument("GetOpt::add_option");
30 for(auto i=opts.begin(); i!=opts.end(); )
32 if((s!=0 && (*i)->get_short()==s) || (*i)->get_long()==l)
41 opts.push_back(new OptionImpl(s, l, t, a));
45 GetOpt::ArgumentImpl &GetOpt::add_argument(const string &n, const Store &t, ArgType y)
48 throw invalid_argument("GetOpt::add_argument");
50 bool have_list = false;
51 bool have_optional = false;
52 for(const ArgumentImpl *a: args)
54 if(a->is_list_store())
56 else if(a->get_type()==OPTIONAL_ARG)
60 if(have_optional && (t.is_list() || y!=OPTIONAL_ARG))
61 throw invalid_argument("GetOpt::add_argument");
62 if(have_list && (t.is_list() || y==OPTIONAL_ARG))
63 throw invalid_argument("GetOpt::add_argument");
65 args.push_back(new ArgumentImpl(n, t, y));
69 GetOpt::OptionImpl &GetOpt::get_option(char s)
71 auto i = find_if(opts, [s](const OptionImpl *o){ return o->get_short()==s; });
74 throw usage_error(string("Unknown option -")+s);
77 GetOpt::OptionImpl &GetOpt::get_option(const string &l)
79 auto i = find_if(opts, [&l](const OptionImpl *o){ return o->get_long()==l; });
82 throw usage_error(string("Unknown option --")+l);
85 void GetOpt::operator()(unsigned argc, const char *const *argv)
89 /* Arguments must first be collected into an array to handle the case
90 where a variable-length argument list is followed by fixed arguments. */
101 i += process_long(argv+i);
104 i += process_short(argv+i);
107 args_raw.push_back(argv[i++]);
111 args_raw.push_back(argv[i]);
114 for(auto j=args.begin(); j!=args.end(); ++j)
116 if((*j)->is_list_store())
118 unsigned end = args_raw.size();
119 for(auto k=j; ++k!=args.end(); )
121 if(i==end && (*j)->get_type()==REQUIRED_ARG)
122 throw usage_error((*j)->get_name()+" is required");
124 (*j)->process(args_raw[i]);
128 if(i<args_raw.size())
129 (*j)->process(args_raw[i++]);
130 else if((*j)->get_type()==REQUIRED_ARG)
131 throw usage_error((*j)->get_name()+" is required");
135 // XXX Enable this when get_args() is completely removed
136 /*if(i<args_raw.size())
137 throw usage_error("Extra positional arguments");*/
139 catch(const usage_error &e)
142 throw usage_error(e.what(), "Usage: "+generate_usage(argv[0]));
146 throw usage_error(string("Help for ")+argv[0]+":", "\nUsage:\n "+generate_usage(argv[0], true)+"\n\n"+generate_help());
149 unsigned GetOpt::process_long(const char *const *argp)
152 const char *arg = argp[0]+2;
154 // See if the argument contains an =
156 for(; arg[equals] && arg[equals]!='='; ++equals) ;
158 OptionImpl &opt = get_option(string(arg, equals));
161 // Process the part after the = as option argument
162 opt.process(arg+equals+1);
163 else if(opt.get_arg_type()==REQUIRED_ARG)
166 throw usage_error("--"+string(arg)+" requires an argument");
168 // Process the next argument as option argument
169 opt.process(argp[1]);
178 unsigned GetOpt::process_short(const char *const *argp)
181 const char *arg = argp[0]+1;
183 // Loop through all characters in the argument
186 OptionImpl &opt = get_option(*arg);
188 if(arg[1] && opt.get_arg_type()!=NO_ARG)
190 // Need an option argument and we have characters left - use them
194 else if(opt.get_arg_type()==REQUIRED_ARG)
197 throw usage_error("-"+string(1, *arg)+" requires an argument");
199 // Use the next argument as option argument
200 opt.process(argp[1]);
210 string GetOpt::generate_usage(const string &argv0, bool compact) const
212 string result = argv0;
214 result += " [options]";
217 for(const OptionImpl *o: opts)
222 result += format("-%c", o->get_short());
223 if(!o->get_long().empty())
225 else if(o->get_arg_type()==OPTIONAL_ARG)
226 result += format("[%s]", o->get_metavar());
227 else if(o->get_arg_type()==REQUIRED_ARG)
228 result += format(" %s", o->get_metavar());
230 if(!o->get_long().empty())
232 result += format("--%s", o->get_long());
234 if(o->get_arg_type()==OPTIONAL_ARG)
235 result += format("[=%s]", o->get_metavar());
236 else if(o->get_arg_type()==REQUIRED_ARG)
237 result += format("=%s", o->get_metavar());
243 for(const ArgumentImpl *a: args)
246 if(a->get_type()==OPTIONAL_ARG)
248 result += format("<%s>", a->get_name());
249 if(a->is_list_store())
251 if(a->get_type()==OPTIONAL_ARG)
258 string GetOpt::generate_help() const
260 bool any_short = any_of(opts.begin(), opts.end(), [](const OptionImpl *o){ return o->get_short(); });
262 string::size_type maxw = 0;
263 vector<string> switches;
264 for(const OptionImpl *o: opts)
269 swtch += format("-%c", o->get_short());
270 if(!o->get_long().empty())
272 else if(o->get_arg_type()==OPTIONAL_ARG)
273 swtch += format("[%s]", o->get_metavar());
274 else if(o->get_arg_type()==REQUIRED_ARG)
275 swtch += format(" %s", o->get_metavar());
279 if(!o->get_long().empty())
281 swtch += format("--%s", o->get_long());
283 if(o->get_arg_type()==OPTIONAL_ARG)
284 swtch += format("[=%s]", o->get_metavar());
285 else if(o->get_arg_type()==REQUIRED_ARG)
286 swtch += format("=%s", o->get_metavar());
288 switches.push_back(swtch);
289 maxw = max(maxw, swtch.size());
292 vector<string> pargs;
293 for(const ArgumentImpl *a: args)
295 string parg = format("<%s>", a->get_name());
296 pargs.push_back(parg);
297 maxw = max(maxw, parg.size());
301 result += "Options:\n";
302 auto j = switches.begin();
303 for(auto i=opts.begin(); i!=opts.end(); ++i, ++j)
304 result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
307 result += "\nArguments:\n";
309 for(auto i=args.begin(); i!=args.end(); ++i, ++j)
310 result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
317 GetOpt::OptionImpl::OptionImpl(char s, const string &l, const Store &t, ArgType a):
327 GetOpt::OptionImpl::~OptionImpl()
332 GetOpt::OptionImpl &GetOpt::OptionImpl::set_help(const string &h)
338 GetOpt::OptionImpl &GetOpt::OptionImpl::set_help(const string &h, const string &m)
345 GetOpt::OptionImpl &GetOpt::OptionImpl::bind_seen_count(unsigned &c)
351 void GetOpt::OptionImpl::process()
353 if(arg_type==REQUIRED_ARG)
354 throw usage_error("--"+lng+" requires an argument");
358 *ext_seen_count = seen_count;
364 catch(const exception &e)
366 throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
370 void GetOpt::OptionImpl::process(const string &arg)
373 throw usage_error("--"+lng+" takes no argument");
377 *ext_seen_count = seen_count;
383 catch(const exception &e)
385 throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
390 GetOpt::ArgumentImpl::ArgumentImpl(const string &n, const Store &t, ArgType a):
396 GetOpt::ArgumentImpl::~ArgumentImpl()
401 GetOpt::ArgumentImpl &GetOpt::ArgumentImpl::set_help(const string &h)
407 void GetOpt::ArgumentImpl::process(const string &arg)
413 catch(const exception &e)
415 throw usage_error("Invalid "+name+" ("+e.what()+")");