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)
41 else if((*i)->get_arg_type()==REQUIRED_ARG)
44 if(!(*i)->get_long().empty())
46 line<<"--"<<(*i)->get_long();
48 if((*i)->get_arg_type()==OPTIONAL_ARG)
50 else if((*i)->get_arg_type()==REQUIRED_ARG)
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(); i!=opts.end(); ++i)
72 line<<'-'<<(*i)->get_short();
73 if(!(*i)->get_long().empty())
75 else if((*i)->get_arg_type()==OPTIONAL_ARG)
77 else if((*i)->get_arg_type()==REQUIRED_ARG)
80 if(!(*i)->get_long().empty())
82 line<<"--"<<(*i)->get_long();
84 if((*i)->get_arg_type()==OPTIONAL_ARG)
86 else if((*i)->get_arg_type()==REQUIRED_ARG)
90 line<<" "<<(*i)->get_help()<<'\n';
98 void GetOpt::operator()(unsigned argc, const char *const *argv)
110 i+=process_long(argv+i);
113 i+=process_short(argv+i);
116 args.push_back(argv[i++]);
120 args.push_back(argv[i]);
123 GetOpt::OptBase &GetOpt::get_option(char s)
125 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
126 if((*i)->get_short()==s)
128 throw UsageError(string("Unknown option -")+s);
131 GetOpt::OptBase &GetOpt::get_option(const string &l)
133 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
134 if((*i)->get_long()==l)
136 throw UsageError(string("Unknown option --")+l);
140 Processes the given argument as a long option.
142 @param argp Pointer to the argument
144 @return The number of arguments eaten (1 or 2)
146 unsigned GetOpt::process_long(const char *const *argp)
149 const char *arg=argp[0]+2;
151 // See if the argument contains an =
153 for(; arg[equals] && arg[equals]!='='; ++equals) ;
155 OptBase &opt=get_option(string(arg, equals));
158 // Process the part after the = as option argument
159 opt.process(arg+equals+1);
160 else if(opt.get_arg_type()==REQUIRED_ARG)
163 throw UsageError("Premature end of arguments");
165 // Process the next argument as option argument
166 opt.process(argp[1]);
176 Processes short options from the given argument.
178 @param argp Pointer to the argument
180 @return The number of arguments eaten (1 or 2)
182 unsigned GetOpt::process_short(const char *const *argp)
185 const char *arg=argp[0]+1;
187 // Loop through all characters in the argument
190 OptBase &opt=get_option(*arg);
192 if(arg[1] && opt.get_arg_type()!=NO_ARG)
194 // Need an option argument and we have characters left - use them
198 else if(opt.get_arg_type()==REQUIRED_ARG)
201 throw UsageError("Premature end of arguments");
203 // Use the next argument as option argument
204 opt.process(argp[1]);