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]+":", 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) const
210 string result = argv0;
211 for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
214 if((*i)->get_short())
216 result += format("-%c", (*i)->get_short());
217 if(!(*i)->get_long().empty())
219 else if((*i)->get_arg_type()==OPTIONAL_ARG)
220 result += format("[%s]", (*i)->get_metavar());
221 else if((*i)->get_arg_type()==REQUIRED_ARG)
222 result += format(" %s", (*i)->get_metavar());
224 if(!(*i)->get_long().empty())
226 result += format("--%s", (*i)->get_long());
228 if((*i)->get_arg_type()==OPTIONAL_ARG)
229 result += format("[=%s]", (*i)->get_metavar());
230 else if((*i)->get_arg_type()==REQUIRED_ARG)
231 result += format("=%s", (*i)->get_metavar());
236 for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
239 if((*i)->get_type()==OPTIONAL_ARG)
241 result += format("<%s>", (*i)->get_name());
242 if((*i)->is_list_store())
244 if((*i)->get_type()==OPTIONAL_ARG)
251 string GetOpt::generate_help() const
253 bool any_short = false;
254 for(OptionList::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i)
255 any_short = (*i)->get_short();
257 string::size_type maxw = 0;
258 list<string> switches;
259 for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
262 if((*i)->get_short())
264 swtch += format("-%c", (*i)->get_short());
265 if(!(*i)->get_long().empty())
267 else if((*i)->get_arg_type()==OPTIONAL_ARG)
268 swtch += format("[%s]", (*i)->get_metavar());
269 else if((*i)->get_arg_type()==REQUIRED_ARG)
270 swtch += format(" %s", (*i)->get_metavar());
274 if(!(*i)->get_long().empty())
276 swtch += format("--%s", (*i)->get_long());
278 if((*i)->get_arg_type()==OPTIONAL_ARG)
279 swtch += format("[=%s]", (*i)->get_metavar());
280 else if((*i)->get_arg_type()==REQUIRED_ARG)
281 swtch += format("=%s", (*i)->get_metavar());
283 switches.push_back(swtch);
284 maxw = max(maxw, swtch.size());
288 for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
290 string parg = format("<%s>", (*i)->get_name());
291 pargs.push_back(parg);
292 maxw = max(maxw, parg.size());
296 result += "Options:\n";
297 list<string>::const_iterator j = switches.begin();
298 for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j)
299 result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
302 result += "\nArguments:\n";
304 for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i, ++j)
305 result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
312 GetOpt::OptionImpl::OptionImpl(char s, const std::string &l, const Store &t, ArgType a):
322 GetOpt::OptionImpl::~OptionImpl()
327 GetOpt::OptionImpl &GetOpt::OptionImpl::set_help(const string &h)
333 GetOpt::OptionImpl &GetOpt::OptionImpl::set_help(const string &h, const string &m)
340 GetOpt::OptionImpl &GetOpt::OptionImpl::bind_seen_count(unsigned &c)
346 void GetOpt::OptionImpl::process()
348 if(arg_type==REQUIRED_ARG)
349 throw usage_error("--"+lng+" requires an argument");
353 *ext_seen_count = seen_count;
359 catch(const exception &e)
361 throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
365 void GetOpt::OptionImpl::process(const string &arg)
368 throw usage_error("--"+lng+" takes no argument");
372 *ext_seen_count = seen_count;
378 catch(const exception &e)
380 throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
385 GetOpt::ArgumentImpl::ArgumentImpl(const string &n, const Store &t, ArgType a):
391 GetOpt::ArgumentImpl::~ArgumentImpl()
396 GetOpt::ArgumentImpl &GetOpt::ArgumentImpl::set_help(const string &h)
402 void GetOpt::ArgumentImpl::process(const string &arg)
408 catch(const exception &e)
410 throw usage_error("Invalid "+name+" ("+e.what()+")");