1 #include <msp/strings/format.h>
11 add_option("help", help, NO_ARG).set_help("Displays this help");
16 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
20 GetOpt::OptBase &GetOpt::add_option(OptBase *opt)
22 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); )
24 if((opt->get_short()!=0 && (*i)->get_short()==opt->get_short()) || (*i)->get_long()==opt->get_long())
37 GetOpt::OptBase &GetOpt::get_option(char s)
39 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
40 if((*i)->get_short()==s)
42 throw usage_error(string("Unknown option -")+s);
45 GetOpt::OptBase &GetOpt::get_option(const string &l)
47 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
48 if((*i)->get_long()==l)
50 throw usage_error(string("Unknown option --")+l);
53 void GetOpt::operator()(unsigned argc, const char *const *argv)
67 i += process_long(argv+i);
70 i += process_short(argv+i);
73 args.push_back(argv[i++]);
77 args.push_back(argv[i]);
79 catch(const usage_error &e)
81 throw usage_error(e.what(), "Usage: "+generate_usage(argv[0]));
85 throw usage_error(string("Help for ")+argv[0]+":", generate_help());
88 unsigned GetOpt::process_long(const char *const *argp)
91 const char *arg = argp[0]+2;
93 // See if the argument contains an =
95 for(; arg[equals] && arg[equals]!='='; ++equals) ;
97 OptBase &opt = get_option(string(arg, equals));
100 // Process the part after the = as option argument
101 opt.process(arg+equals+1);
102 else if(opt.get_arg_type()==REQUIRED_ARG)
105 throw usage_error("--"+string(arg)+" requires an argument");
107 // Process the next argument as option argument
108 opt.process(argp[1]);
117 unsigned GetOpt::process_short(const char *const *argp)
120 const char *arg = argp[0]+1;
122 // Loop through all characters in the argument
125 OptBase &opt = get_option(*arg);
127 if(arg[1] && opt.get_arg_type()!=NO_ARG)
129 // Need an option argument and we have characters left - use them
133 else if(opt.get_arg_type()==REQUIRED_ARG)
136 throw usage_error("-"+string(1, *arg)+" requires an argument");
138 // Use the next argument as option argument
139 opt.process(argp[1]);
149 string GetOpt::generate_usage(const string &argv0) const
151 string result = argv0;
152 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
155 if((*i)->get_short())
157 result += format("-%c", (*i)->get_short());
158 if(!(*i)->get_long().empty())
160 else if((*i)->get_arg_type()==OPTIONAL_ARG)
161 result += format("[%s]", (*i)->get_metavar());
162 else if((*i)->get_arg_type()==REQUIRED_ARG)
163 result += format(" %s", (*i)->get_metavar());
165 if(!(*i)->get_long().empty())
167 result += format("--%s", (*i)->get_long());
169 if((*i)->get_arg_type()==OPTIONAL_ARG)
170 result += format("[=%s]", (*i)->get_metavar());
171 else if((*i)->get_arg_type()==REQUIRED_ARG)
172 result += format("=%s", (*i)->get_metavar());
180 string GetOpt::generate_help() const
182 bool any_short = false;
183 for(list<OptBase *>::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i)
184 any_short = (*i)->get_short();
186 string::size_type maxw = 0;
187 list<string> switches;
188 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
191 if((*i)->get_short())
193 swtch += format("-%c", (*i)->get_short());
194 if(!(*i)->get_long().empty())
196 else if((*i)->get_arg_type()==OPTIONAL_ARG)
197 swtch += format("[%s]", (*i)->get_metavar());
198 else if((*i)->get_arg_type()==REQUIRED_ARG)
199 swtch += format(" %s", (*i)->get_metavar());
203 if(!(*i)->get_long().empty())
205 swtch += format("--%s", (*i)->get_long());
207 if((*i)->get_arg_type()==OPTIONAL_ARG)
208 swtch += format("[=%s]", (*i)->get_metavar());
209 else if((*i)->get_arg_type()==REQUIRED_ARG)
210 swtch += format("=%s", (*i)->get_metavar());
212 switches.push_back(swtch);
213 maxw = max(maxw, swtch.size());
217 list<string>::const_iterator j = switches.begin();
218 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j)
219 result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
225 GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a):
234 throw invalid_argument("empty long option name");
237 GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h)
243 GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h, const string &m)
250 GetOpt::OptBase &GetOpt::OptBase::bind_seen_count(unsigned &c)
256 void GetOpt::OptBase::process()
258 if(arg_type==REQUIRED_ARG)
259 throw usage_error("--"+lng+" requires an argument");
262 *ext_seen_count = seen_count;
267 void GetOpt::OptBase::process(const string &arg)
270 throw usage_error("--"+lng+" takes no argument");
273 *ext_seen_count = seen_count;