3 This file is part of libmspcore
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
14 Generates a single line that gives an overview about the known options.
16 @param argv0 The program name to be used in the usage string
18 @return The generated usage string
20 string GetOpt::generate_usage(const string &argv0) const
25 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
30 line<<'-'<<(*i)->get_short();
31 if(!(*i)->get_long().empty())
33 else if((*i)->get_arg_type()==OPTIONAL_ARG)
35 else if((*i)->get_arg_type()==REQUIRED_ARG)
38 if(!(*i)->get_long().empty())
40 line<<"--"<<(*i)->get_long();
42 if((*i)->get_arg_type()==OPTIONAL_ARG)
44 else if((*i)->get_arg_type()==REQUIRED_ARG)
54 Generates help for known options in tabular format, one option per line.
55 The returned string will have a linefeed at the end.
57 string GetOpt::generate_help() const
60 for(list<OptBase *>::const_iterator i=opts.begin(); i!=opts.end(); ++i)
66 line<<'-'<<(*i)->get_short();
67 if(!(*i)->get_long().empty())
69 else if((*i)->get_arg_type()==OPTIONAL_ARG)
71 else if((*i)->get_arg_type()==REQUIRED_ARG)
74 if(!(*i)->get_long().empty())
76 line<<"--"<<(*i)->get_long();
78 if((*i)->get_arg_type()==OPTIONAL_ARG)
80 else if((*i)->get_arg_type()==REQUIRED_ARG)
84 line<<" "<<(*i)->get_help()<<'\n';
92 void GetOpt::operator()(unsigned argc, const char *const *argv)
104 i+=process_long(argv+i);
107 i+=process_short(argv+i);
110 args.push_back(argv[i++]);
114 args.push_back(argv[i]);
119 for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++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]);