3 This file is part of libmspcore
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
15 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
20 Generates a single line that gives an overview about the known options.
22 @param argv0 The program name to be used in the usage string
24 @return The generated usage string
26 string GetOpt::generate_usage(const string &argv0) const
31 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
36 line<<'-'<<(*i)->get_short();
37 if(!(*i)->get_long().empty())
39 else if((*i)->get_arg_type()==OPTIONAL_ARG)
40 line<<'['<<(*i)->get_metavar()<<']';
41 else if((*i)->get_arg_type()==REQUIRED_ARG)
42 line<<' '<<(*i)->get_metavar();
44 if(!(*i)->get_long().empty())
46 line<<"--"<<(*i)->get_long();
48 if((*i)->get_arg_type()==OPTIONAL_ARG)
49 line<<"[="<<(*i)->get_metavar()<<']';
50 else if((*i)->get_arg_type()==REQUIRED_ARG)
51 line<<'='<<(*i)->get_metavar();
60 Generates help for known options in tabular format, one option per line.
61 The returned string will have a linefeed at the end.
63 string GetOpt::generate_help() const
66 for(list<OptBase *>::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i)
67 any_short=(*i)->get_short();
69 string::size_type maxw=0;
70 list<string> switches;
71 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
76 swtch<<'-'<<(*i)->get_short();
77 if(!(*i)->get_long().empty())
79 else if((*i)->get_arg_type()==OPTIONAL_ARG)
80 swtch<<'['<<(*i)->get_metavar()<<']';
81 else if((*i)->get_arg_type()==REQUIRED_ARG)
82 swtch<<' '<<(*i)->get_metavar();
86 if(!(*i)->get_long().empty())
88 swtch<<"--"<<(*i)->get_long();
90 if((*i)->get_arg_type()==OPTIONAL_ARG)
91 swtch<<"[="<<(*i)->get_metavar()<<']';
92 else if((*i)->get_arg_type()==REQUIRED_ARG)
93 swtch<<'='<<(*i)->get_metavar();
95 switches.push_back(swtch.str());
96 maxw=max(maxw, switches.back().size());
100 list<string>::const_iterator j=switches.begin();
101 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j)
104 result+=string(maxw+2-j->size(), ' ');
105 result+=(*i)->get_help();
112 void GetOpt::operator()(unsigned argc, const char *const *argv)
124 i+=process_long(argv+i);
127 i+=process_short(argv+i);
130 args.push_back(argv[i++]);
134 args.push_back(argv[i]);
137 GetOpt::OptBase &GetOpt::get_option(char s)
139 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
140 if((*i)->get_short()==s)
142 throw UsageError(string("Unknown option -")+s);
145 GetOpt::OptBase &GetOpt::get_option(const string &l)
147 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
148 if((*i)->get_long()==l)
150 throw UsageError(string("Unknown option --")+l);
154 Processes the given argument as a long option.
156 @param argp Pointer to the argument
158 @return The number of arguments eaten (1 or 2)
160 unsigned GetOpt::process_long(const char *const *argp)
163 const char *arg=argp[0]+2;
165 // See if the argument contains an =
167 for(; arg[equals] && arg[equals]!='='; ++equals) ;
169 OptBase &opt=get_option(string(arg, equals));
172 // Process the part after the = as option argument
173 opt.process(arg+equals+1);
174 else if(opt.get_arg_type()==REQUIRED_ARG)
177 throw UsageError("Premature end of arguments");
179 // Process the next argument as option argument
180 opt.process(argp[1]);
190 Processes short options from the given argument.
192 @param argp Pointer to the argument
194 @return The number of arguments eaten (1 or 2)
196 unsigned GetOpt::process_short(const char *const *argp)
199 const char *arg=argp[0]+1;
201 // Loop through all characters in the argument
204 OptBase &opt=get_option(*arg);
206 if(arg[1] && opt.get_arg_type()!=NO_ARG)
208 // Need an option argument and we have characters left - use them
212 else if(opt.get_arg_type()==REQUIRED_ARG)
215 throw UsageError("Premature end of arguments");
217 // Use the next argument as option argument
218 opt.process(argp[1]);
229 GetOpt::OptBase::OptBase(char s, const std::string &l, ArgType a):
237 GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h)
243 GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h, const string &m)
250 void GetOpt::OptBase::process()
252 if(arg_type==REQUIRED_ARG)
253 throw UsageError("--"+lng+" requires an argument");
259 void GetOpt::OptBase::process(const string &arg)
262 throw UsageError("--"+lng+" takes no argument");