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::get_option(char s)
22 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
23 if((*i)->get_short()==s)
25 throw usage_error(string("Unknown option -")+s);
28 GetOpt::OptBase &GetOpt::get_option(const string &l)
30 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
31 if((*i)->get_long()==l)
33 throw usage_error(string("Unknown option --")+l);
36 void GetOpt::operator()(unsigned argc, const char *const *argv)
50 i += process_long(argv+i);
53 i += process_short(argv+i);
56 args.push_back(argv[i++]);
60 args.push_back(argv[i]);
62 catch(const usage_error &e)
64 throw usage_error(e.what(), "Usage: "+generate_usage(argv[0]));
68 throw usage_error(string("Help for ")+argv[0]+":", generate_help());
71 unsigned GetOpt::process_long(const char *const *argp)
74 const char *arg = argp[0]+2;
76 // See if the argument contains an =
78 for(; arg[equals] && arg[equals]!='='; ++equals) ;
80 OptBase &opt = get_option(string(arg, equals));
83 // Process the part after the = as option argument
84 opt.process(arg+equals+1);
85 else if(opt.get_arg_type()==REQUIRED_ARG)
88 throw usage_error("--"+string(arg)+" requires an argument");
90 // Process the next argument as option argument
100 unsigned GetOpt::process_short(const char *const *argp)
103 const char *arg = argp[0]+1;
105 // Loop through all characters in the argument
108 OptBase &opt = get_option(*arg);
110 if(arg[1] && opt.get_arg_type()!=NO_ARG)
112 // Need an option argument and we have characters left - use them
116 else if(opt.get_arg_type()==REQUIRED_ARG)
119 throw usage_error("-"+string(1, *arg)+" requires an argument");
121 // Use the next argument as option argument
122 opt.process(argp[1]);
132 string GetOpt::generate_usage(const string &argv0) const
134 string result = argv0;
135 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
138 if((*i)->get_short())
140 result += format("-%c", (*i)->get_short());
141 if(!(*i)->get_long().empty())
143 else if((*i)->get_arg_type()==OPTIONAL_ARG)
144 result += format("[%s]", (*i)->get_metavar());
145 else if((*i)->get_arg_type()==REQUIRED_ARG)
146 result += format(" %s", (*i)->get_metavar());
148 if(!(*i)->get_long().empty())
150 result += format("--%s", (*i)->get_long());
152 if((*i)->get_arg_type()==OPTIONAL_ARG)
153 result += format("[=%s]", (*i)->get_metavar());
154 else if((*i)->get_arg_type()==REQUIRED_ARG)
155 result += format("=%s", (*i)->get_metavar());
163 string GetOpt::generate_help() const
165 bool any_short = false;
166 for(list<OptBase *>::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i)
167 any_short = (*i)->get_short();
169 string::size_type maxw = 0;
170 list<string> switches;
171 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
174 if((*i)->get_short())
176 swtch += format("-%c", (*i)->get_short());
177 if(!(*i)->get_long().empty())
179 else if((*i)->get_arg_type()==OPTIONAL_ARG)
180 swtch += format("[%s]", (*i)->get_metavar());
181 else if((*i)->get_arg_type()==REQUIRED_ARG)
182 swtch += format(" %s", (*i)->get_metavar());
186 if(!(*i)->get_long().empty())
188 swtch += format("--%s", (*i)->get_long());
190 if((*i)->get_arg_type()==OPTIONAL_ARG)
191 swtch += format("[=%s]", (*i)->get_metavar());
192 else if((*i)->get_arg_type()==REQUIRED_ARG)
193 swtch += format("=%s", (*i)->get_metavar());
195 switches.push_back(swtch);
196 maxw = max(maxw, swtch.size());
200 list<string>::const_iterator j = switches.begin();
201 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j)
202 result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
208 GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a):
216 throw invalid_argument("empty long option name");
219 GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h)
225 GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h, const string &m)
232 void GetOpt::OptBase::process()
234 if(arg_type==REQUIRED_ARG)
235 throw usage_error("--"+lng+" requires an argument");
241 void GetOpt::OptBase::process(const string &arg)
244 throw usage_error("--"+lng+" takes no argument");