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)
20 GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, ArgType a)
23 throw invalid_argument("GetOpt::add_option");
25 for(OptionList::iterator i=opts.begin(); i!=opts.end(); )
27 if((s!=0 && (*i)->get_short()==s) || (*i)->get_long()==l)
36 opts.push_back(new OptionImpl(s, l, t, a));
40 GetOpt::OptionImpl &GetOpt::get_option(char s)
42 for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
43 if((*i)->get_short()==s)
45 throw usage_error(string("Unknown option -")+s);
48 GetOpt::OptionImpl &GetOpt::get_option(const string &l)
50 for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
51 if((*i)->get_long()==l)
53 throw usage_error(string("Unknown option --")+l);
56 void GetOpt::operator()(unsigned argc, const char *const *argv)
70 i += process_long(argv+i);
73 i += process_short(argv+i);
76 args.push_back(argv[i++]);
80 args.push_back(argv[i]);
82 catch(const usage_error &e)
84 throw usage_error(e.what(), "Usage: "+generate_usage(argv[0]));
88 throw usage_error(string("Help for ")+argv[0]+":", generate_help());
91 unsigned GetOpt::process_long(const char *const *argp)
94 const char *arg = argp[0]+2;
96 // See if the argument contains an =
98 for(; arg[equals] && arg[equals]!='='; ++equals) ;
100 OptionImpl &opt = get_option(string(arg, equals));
103 // Process the part after the = as option argument
104 opt.process(arg+equals+1);
105 else if(opt.get_arg_type()==REQUIRED_ARG)
108 throw usage_error("--"+string(arg)+" requires an argument");
110 // Process the next argument as option argument
111 opt.process(argp[1]);
120 unsigned GetOpt::process_short(const char *const *argp)
123 const char *arg = argp[0]+1;
125 // Loop through all characters in the argument
128 OptionImpl &opt = get_option(*arg);
130 if(arg[1] && opt.get_arg_type()!=NO_ARG)
132 // Need an option argument and we have characters left - use them
136 else if(opt.get_arg_type()==REQUIRED_ARG)
139 throw usage_error("-"+string(1, *arg)+" requires an argument");
141 // Use the next argument as option argument
142 opt.process(argp[1]);
152 string GetOpt::generate_usage(const string &argv0) const
154 string result = argv0;
155 for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
158 if((*i)->get_short())
160 result += format("-%c", (*i)->get_short());
161 if(!(*i)->get_long().empty())
163 else if((*i)->get_arg_type()==OPTIONAL_ARG)
164 result += format("[%s]", (*i)->get_metavar());
165 else if((*i)->get_arg_type()==REQUIRED_ARG)
166 result += format(" %s", (*i)->get_metavar());
168 if(!(*i)->get_long().empty())
170 result += format("--%s", (*i)->get_long());
172 if((*i)->get_arg_type()==OPTIONAL_ARG)
173 result += format("[=%s]", (*i)->get_metavar());
174 else if((*i)->get_arg_type()==REQUIRED_ARG)
175 result += format("=%s", (*i)->get_metavar());
183 string GetOpt::generate_help() const
185 bool any_short = false;
186 for(OptionList::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i)
187 any_short = (*i)->get_short();
189 string::size_type maxw = 0;
190 list<string> switches;
191 for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
194 if((*i)->get_short())
196 swtch += format("-%c", (*i)->get_short());
197 if(!(*i)->get_long().empty())
199 else if((*i)->get_arg_type()==OPTIONAL_ARG)
200 swtch += format("[%s]", (*i)->get_metavar());
201 else if((*i)->get_arg_type()==REQUIRED_ARG)
202 swtch += format(" %s", (*i)->get_metavar());
206 if(!(*i)->get_long().empty())
208 swtch += format("--%s", (*i)->get_long());
210 if((*i)->get_arg_type()==OPTIONAL_ARG)
211 swtch += format("[=%s]", (*i)->get_metavar());
212 else if((*i)->get_arg_type()==REQUIRED_ARG)
213 swtch += format("=%s", (*i)->get_metavar());
215 switches.push_back(swtch);
216 maxw = max(maxw, swtch.size());
220 list<string>::const_iterator j = switches.begin();
221 for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j)
222 result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
228 GetOpt::OptionImpl::OptionImpl(char s, const std::string &l, const Store &t, ArgType a):
238 GetOpt::OptionImpl::~OptionImpl()
243 GetOpt::OptionImpl &GetOpt::OptionImpl::set_help(const string &h)
249 GetOpt::OptionImpl &GetOpt::OptionImpl::set_help(const string &h, const string &m)
256 GetOpt::OptionImpl &GetOpt::OptionImpl::bind_seen_count(unsigned &c)
262 void GetOpt::OptionImpl::process()
264 if(arg_type==REQUIRED_ARG)
265 throw usage_error("--"+lng+" requires an argument");
269 *ext_seen_count = seen_count;
275 catch(const exception &e)
277 throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
281 void GetOpt::OptionImpl::process(const string &arg)
284 throw usage_error("--"+lng+" takes no argument");
288 *ext_seen_count = seen_count;
294 catch(const exception &e)
296 throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");