1 #include <msp/strings/format.h>
11 add_option("help", help, NO_ARG).set_help("Displays this help");
16 for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
18 for(ArgumentList::iterator i=args.begin(); i!=args.end(); ++i)
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(OptionList::iterator 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 a)
47 throw invalid_argument("GetOpt::add_argument");
49 bool have_list = false;
50 bool have_optional = false;
51 for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
53 if((*i)->is_list_store())
55 else if((*i)->get_type()==OPTIONAL_ARG)
59 if(have_optional && (t.is_list() || a!=OPTIONAL_ARG))
60 throw invalid_argument("GetOpt::add_argument");
61 if(have_list && (t.is_list() || a==OPTIONAL_ARG))
62 throw invalid_argument("GetOpt::add_argument");
64 args.push_back(new ArgumentImpl(n, t, a));
68 GetOpt::OptionImpl &GetOpt::get_option(char s)
70 for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
71 if((*i)->get_short()==s)
73 throw usage_error(string("Unknown option -")+s);
76 GetOpt::OptionImpl &GetOpt::get_option(const string &l)
78 for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
79 if((*i)->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(ArgumentList::const_iterator j=args.begin(); j!=args.end(); ++j)
115 if((*j)->is_list_store())
117 unsigned end = args_raw.size();
118 for(ArgumentList::const_iterator 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(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
219 if((*i)->get_short())
221 result += format("-%c", (*i)->get_short());
222 if(!(*i)->get_long().empty())
224 else if((*i)->get_arg_type()==OPTIONAL_ARG)
225 result += format("[%s]", (*i)->get_metavar());
226 else if((*i)->get_arg_type()==REQUIRED_ARG)
227 result += format(" %s", (*i)->get_metavar());
229 if(!(*i)->get_long().empty())
231 result += format("--%s", (*i)->get_long());
233 if((*i)->get_arg_type()==OPTIONAL_ARG)
234 result += format("[=%s]", (*i)->get_metavar());
235 else if((*i)->get_arg_type()==REQUIRED_ARG)
236 result += format("=%s", (*i)->get_metavar());
242 for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
245 if((*i)->get_type()==OPTIONAL_ARG)
247 result += format("<%s>", (*i)->get_name());
248 if((*i)->is_list_store())
250 if((*i)->get_type()==OPTIONAL_ARG)
257 string GetOpt::generate_help() const
259 bool any_short = false;
260 for(OptionList::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i)
261 any_short = (*i)->get_short();
263 string::size_type maxw = 0;
264 list<string> switches;
265 for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
268 if((*i)->get_short())
270 swtch += format("-%c", (*i)->get_short());
271 if(!(*i)->get_long().empty())
273 else if((*i)->get_arg_type()==OPTIONAL_ARG)
274 swtch += format("[%s]", (*i)->get_metavar());
275 else if((*i)->get_arg_type()==REQUIRED_ARG)
276 swtch += format(" %s", (*i)->get_metavar());
280 if(!(*i)->get_long().empty())
282 swtch += format("--%s", (*i)->get_long());
284 if((*i)->get_arg_type()==OPTIONAL_ARG)
285 swtch += format("[=%s]", (*i)->get_metavar());
286 else if((*i)->get_arg_type()==REQUIRED_ARG)
287 swtch += format("=%s", (*i)->get_metavar());
289 switches.push_back(swtch);
290 maxw = max(maxw, swtch.size());
294 for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
296 string parg = format("<%s>", (*i)->get_name());
297 pargs.push_back(parg);
298 maxw = max(maxw, parg.size());
302 result += "Options:\n";
303 list<string>::const_iterator j = switches.begin();
304 for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j)
305 result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
308 result += "\nArguments:\n";
310 for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i, ++j)
311 result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
318 GetOpt::OptionImpl::OptionImpl(char s, const std::string &l, const Store &t, ArgType a):
328 GetOpt::OptionImpl::~OptionImpl()
333 GetOpt::OptionImpl &GetOpt::OptionImpl::set_help(const string &h)
339 GetOpt::OptionImpl &GetOpt::OptionImpl::set_help(const string &h, const string &m)
346 GetOpt::OptionImpl &GetOpt::OptionImpl::bind_seen_count(unsigned &c)
352 void GetOpt::OptionImpl::process()
354 if(arg_type==REQUIRED_ARG)
355 throw usage_error("--"+lng+" requires an argument");
359 *ext_seen_count = seen_count;
365 catch(const exception &e)
367 throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
371 void GetOpt::OptionImpl::process(const string &arg)
374 throw usage_error("--"+lng+" takes no argument");
378 *ext_seen_count = seen_count;
384 catch(const exception &e)
386 throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
391 GetOpt::ArgumentImpl::ArgumentImpl(const string &n, const Store &t, ArgType a):
397 GetOpt::ArgumentImpl::~ArgumentImpl()
402 GetOpt::ArgumentImpl &GetOpt::ArgumentImpl::set_help(const string &h)
408 void GetOpt::ArgumentImpl::process(const string &arg)
414 catch(const exception &e)
416 throw usage_error("Invalid "+name+" ("+e.what()+")");