10 add_option("help", help, NO_ARG).set_help("Displays this help");
15 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
19 GetOpt::OptBase &GetOpt::get_option(char s)
21 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
22 if((*i)->get_short()==s)
24 throw usage_error(string("Unknown option -")+s);
27 GetOpt::OptBase &GetOpt::get_option(const string &l)
29 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
30 if((*i)->get_long()==l)
32 throw usage_error(string("Unknown option --")+l);
35 void GetOpt::operator()(unsigned argc, const char *const *argv)
49 i += process_long(argv+i);
52 i += process_short(argv+i);
55 args.push_back(argv[i++]);
59 args.push_back(argv[i]);
61 catch(const usage_error &e)
63 throw usage_error(e.what(), "Usage: "+generate_usage(argv[0]));
67 throw usage_error(string("Help for ")+argv[0]+":", generate_help());
70 unsigned GetOpt::process_long(const char *const *argp)
73 const char *arg = argp[0]+2;
75 // See if the argument contains an =
77 for(; arg[equals] && arg[equals]!='='; ++equals) ;
79 OptBase &opt = get_option(string(arg, equals));
82 // Process the part after the = as option argument
83 opt.process(arg+equals+1);
84 else if(opt.get_arg_type()==REQUIRED_ARG)
87 throw usage_error("--"+string(arg)+" requires an argument");
89 // Process the next argument as option argument
99 unsigned GetOpt::process_short(const char *const *argp)
102 const char *arg = argp[0]+1;
104 // Loop through all characters in the argument
107 OptBase &opt = get_option(*arg);
109 if(arg[1] && opt.get_arg_type()!=NO_ARG)
111 // Need an option argument and we have characters left - use them
115 else if(opt.get_arg_type()==REQUIRED_ARG)
118 throw usage_error("-"+string(1, *arg)+" requires an argument");
120 // Use the next argument as option argument
121 opt.process(argp[1]);
131 string GetOpt::generate_usage(const string &argv0) const
136 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
139 if((*i)->get_short())
141 line<<'-'<<(*i)->get_short();
142 if(!(*i)->get_long().empty())
144 else if((*i)->get_arg_type()==OPTIONAL_ARG)
145 line<<'['<<(*i)->get_metavar()<<']';
146 else if((*i)->get_arg_type()==REQUIRED_ARG)
147 line<<' '<<(*i)->get_metavar();
149 if(!(*i)->get_long().empty())
151 line<<"--"<<(*i)->get_long();
153 if((*i)->get_arg_type()==OPTIONAL_ARG)
154 line<<"[="<<(*i)->get_metavar()<<']';
155 else if((*i)->get_arg_type()==REQUIRED_ARG)
156 line<<'='<<(*i)->get_metavar();
164 string GetOpt::generate_help() const
166 bool any_short = false;
167 for(list<OptBase *>::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i)
168 any_short = (*i)->get_short();
170 string::size_type maxw = 0;
171 list<string> switches;
172 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
175 if((*i)->get_short())
177 swtch<<'-'<<(*i)->get_short();
178 if(!(*i)->get_long().empty())
180 else if((*i)->get_arg_type()==OPTIONAL_ARG)
181 swtch<<'['<<(*i)->get_metavar()<<']';
182 else if((*i)->get_arg_type()==REQUIRED_ARG)
183 swtch<<' '<<(*i)->get_metavar();
187 if(!(*i)->get_long().empty())
189 swtch<<"--"<<(*i)->get_long();
191 if((*i)->get_arg_type()==OPTIONAL_ARG)
192 swtch<<"[="<<(*i)->get_metavar()<<']';
193 else if((*i)->get_arg_type()==REQUIRED_ARG)
194 swtch<<'='<<(*i)->get_metavar();
196 switches.push_back(swtch.str());
197 maxw = max(maxw, switches.back().size());
201 list<string>::const_iterator j = switches.begin();
202 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j)
205 result += string(maxw+2-j->size(), ' ');
206 result += (*i)->get_help();
214 GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a):
222 GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h)
228 GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h, const string &m)
235 void GetOpt::OptBase::process()
237 if(arg_type==REQUIRED_ARG)
238 throw usage_error("--"+lng+" requires an argument");
244 void GetOpt::OptBase::process(const string &arg)
247 throw usage_error("--"+lng+" takes no argument");