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)
140 throw usage_error(e.what(), "Usage: "+generate_usage(argv[0]));
144 throw usage_error(string("Help for ")+argv[0]+":", "\nUsage:\n "+generate_usage(argv[0], true)+"\n\n"+generate_help());
147 unsigned GetOpt::process_long(const char *const *argp)
150 const char *arg = argp[0]+2;
152 // See if the argument contains an =
154 for(; arg[equals] && arg[equals]!='='; ++equals) ;
156 OptionImpl &opt = get_option(string(arg, equals));
159 // Process the part after the = as option argument
160 opt.process(arg+equals+1);
161 else if(opt.get_arg_type()==REQUIRED_ARG)
164 throw usage_error("--"+string(arg)+" requires an argument");
166 // Process the next argument as option argument
167 opt.process(argp[1]);
176 unsigned GetOpt::process_short(const char *const *argp)
179 const char *arg = argp[0]+1;
181 // Loop through all characters in the argument
184 OptionImpl &opt = get_option(*arg);
186 if(arg[1] && opt.get_arg_type()!=NO_ARG)
188 // Need an option argument and we have characters left - use them
192 else if(opt.get_arg_type()==REQUIRED_ARG)
195 throw usage_error("-"+string(1, *arg)+" requires an argument");
197 // Use the next argument as option argument
198 opt.process(argp[1]);
208 string GetOpt::generate_usage(const string &argv0, bool compact) const
210 string result = argv0;
212 result += " [options]";
215 for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
218 if((*i)->get_short())
220 result += format("-%c", (*i)->get_short());
221 if(!(*i)->get_long().empty())
223 else if((*i)->get_arg_type()==OPTIONAL_ARG)
224 result += format("[%s]", (*i)->get_metavar());
225 else if((*i)->get_arg_type()==REQUIRED_ARG)
226 result += format(" %s", (*i)->get_metavar());
228 if(!(*i)->get_long().empty())
230 result += format("--%s", (*i)->get_long());
232 if((*i)->get_arg_type()==OPTIONAL_ARG)
233 result += format("[=%s]", (*i)->get_metavar());
234 else if((*i)->get_arg_type()==REQUIRED_ARG)
235 result += format("=%s", (*i)->get_metavar());
241 for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
244 if((*i)->get_type()==OPTIONAL_ARG)
246 result += format("<%s>", (*i)->get_name());
247 if((*i)->is_list_store())
249 if((*i)->get_type()==OPTIONAL_ARG)
256 string GetOpt::generate_help() const
258 bool any_short = false;
259 for(OptionList::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i)
260 any_short = (*i)->get_short();
262 string::size_type maxw = 0;
263 list<string> switches;
264 for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
267 if((*i)->get_short())
269 swtch += format("-%c", (*i)->get_short());
270 if(!(*i)->get_long().empty())
272 else if((*i)->get_arg_type()==OPTIONAL_ARG)
273 swtch += format("[%s]", (*i)->get_metavar());
274 else if((*i)->get_arg_type()==REQUIRED_ARG)
275 swtch += format(" %s", (*i)->get_metavar());
279 if(!(*i)->get_long().empty())
281 swtch += format("--%s", (*i)->get_long());
283 if((*i)->get_arg_type()==OPTIONAL_ARG)
284 swtch += format("[=%s]", (*i)->get_metavar());
285 else if((*i)->get_arg_type()==REQUIRED_ARG)
286 swtch += format("=%s", (*i)->get_metavar());
288 switches.push_back(swtch);
289 maxw = max(maxw, swtch.size());
293 for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
295 string parg = format("<%s>", (*i)->get_name());
296 pargs.push_back(parg);
297 maxw = max(maxw, parg.size());
301 result += "Options:\n";
302 list<string>::const_iterator j = switches.begin();
303 for(OptionList::const_iterator 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(ArgumentList::const_iterator 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 std::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()+")");