3 This file is part of libmspcore
4 Copyright © 2006-2009, 2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
17 add_option("help", help, NO_ARG).set_help("Displays this help");
22 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
26 GetOpt::OptBase &GetOpt::get_option(char s)
28 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
29 if((*i)->get_short()==s)
31 throw usage_error(string("Unknown option -")+s);
34 GetOpt::OptBase &GetOpt::get_option(const string &l)
36 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
37 if((*i)->get_long()==l)
39 throw usage_error(string("Unknown option --")+l);
42 void GetOpt::operator()(unsigned argc, const char *const *argv)
56 i += process_long(argv+i);
59 i += process_short(argv+i);
62 args.push_back(argv[i++]);
66 args.push_back(argv[i]);
68 catch(const usage_error &e)
70 throw usage_error(e.what(), "Usage: "+generate_usage(argv[0]));
74 throw usage_error(string("Help for ")+argv[0]+":", generate_help());
77 unsigned GetOpt::process_long(const char *const *argp)
80 const char *arg = argp[0]+2;
82 // See if the argument contains an =
84 for(; arg[equals] && arg[equals]!='='; ++equals) ;
86 OptBase &opt = get_option(string(arg, equals));
89 // Process the part after the = as option argument
90 opt.process(arg+equals+1);
91 else if(opt.get_arg_type()==REQUIRED_ARG)
94 throw usage_error("--"+string(arg)+" requires an argument");
96 // Process the next argument as option argument
106 unsigned GetOpt::process_short(const char *const *argp)
109 const char *arg = argp[0]+1;
111 // Loop through all characters in the argument
114 OptBase &opt = get_option(*arg);
116 if(arg[1] && opt.get_arg_type()!=NO_ARG)
118 // Need an option argument and we have characters left - use them
122 else if(opt.get_arg_type()==REQUIRED_ARG)
125 throw usage_error("-"+string(1, *arg)+" requires an argument");
127 // Use the next argument as option argument
128 opt.process(argp[1]);
138 string GetOpt::generate_usage(const string &argv0) const
143 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
146 if((*i)->get_short())
148 line<<'-'<<(*i)->get_short();
149 if(!(*i)->get_long().empty())
151 else if((*i)->get_arg_type()==OPTIONAL_ARG)
152 line<<'['<<(*i)->get_metavar()<<']';
153 else if((*i)->get_arg_type()==REQUIRED_ARG)
154 line<<' '<<(*i)->get_metavar();
156 if(!(*i)->get_long().empty())
158 line<<"--"<<(*i)->get_long();
160 if((*i)->get_arg_type()==OPTIONAL_ARG)
161 line<<"[="<<(*i)->get_metavar()<<']';
162 else if((*i)->get_arg_type()==REQUIRED_ARG)
163 line<<'='<<(*i)->get_metavar();
171 string GetOpt::generate_help() const
173 bool any_short = false;
174 for(list<OptBase *>::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i)
175 any_short = (*i)->get_short();
177 string::size_type maxw = 0;
178 list<string> switches;
179 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
182 if((*i)->get_short())
184 swtch<<'-'<<(*i)->get_short();
185 if(!(*i)->get_long().empty())
187 else if((*i)->get_arg_type()==OPTIONAL_ARG)
188 swtch<<'['<<(*i)->get_metavar()<<']';
189 else if((*i)->get_arg_type()==REQUIRED_ARG)
190 swtch<<' '<<(*i)->get_metavar();
194 if(!(*i)->get_long().empty())
196 swtch<<"--"<<(*i)->get_long();
198 if((*i)->get_arg_type()==OPTIONAL_ARG)
199 swtch<<"[="<<(*i)->get_metavar()<<']';
200 else if((*i)->get_arg_type()==REQUIRED_ARG)
201 swtch<<'='<<(*i)->get_metavar();
203 switches.push_back(swtch.str());
204 maxw = max(maxw, switches.back().size());
208 list<string>::const_iterator j = switches.begin();
209 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j)
212 result += string(maxw+2-j->size(), ' ');
213 result += (*i)->get_help();
221 GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a):
229 GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h)
235 GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h, const string &m)
242 void GetOpt::OptBase::process()
244 if(arg_type==REQUIRED_ARG)
245 throw usage_error("--"+lng+" requires an argument");
251 void GetOpt::OptBase::process(const string &arg)
254 throw usage_error("--"+lng+" takes no argument");