#include <CmdLineParser.h>
Inheritance diagram for Tools::CommandLineParser:
Public Member Functions | |
CommandLineParser (void) | |
virtual String * | className (void) const |
Answer receiver class name. | |
template<class TReceiver> | |
void | addMandatoryArgument (char *longArg, TReceiver *receiver, void(TReceiver::*execFunc)(void)) |
template<class TReceiver> | |
void | addMandatoryArgument (char *longArg, char shortArg, TReceiver *receiver, void(TReceiver::*execFunc)(void)) |
template<class TReceiver, class TParam> | |
void | addMandatoryArgument (char *longArg, char shortArg, TReceiver *receiver, void(TReceiver::*execFunc)(TParam), TParam userData) |
template<class TReceiver> | |
void | addOptionalArgument (char *longArg, TReceiver *receiver, void(TReceiver::*execFunc)(void)) |
template<class TReceiver> | |
void | addOptionalArgument (char *longArg, char shortArg, TReceiver *receiver, void(TReceiver::*execFunc)(void)) |
template<class TReceiver, class TParam> | |
void | addOptionalArgument (char *longArg, char shortArg, TReceiver *receiver, void(TReceiver::*execFunc)(TParam), TParam userData) |
template<class TReceiver> | |
void | addMandatoryArgumentWithParameter (char *longArg, TReceiver *receiver, void(TReceiver::*execFunc)(String *)) |
template<class TReceiver> | |
void | addMandatoryArgumentWithParameter (char *longArg, char shortArg, TReceiver *receiver, void(TReceiver::*execFunc)(String *)) |
template<class TReceiver> | |
void | addOptionalArgumentWithParameter (char *longArg, TReceiver *receiver, void(TReceiver::*execFunc)(String *)) |
template<class TReceiver> | |
void | addOptionalArgumentWithParameter (char *longArg, char shortArg, TReceiver *receiver, void(TReceiver::*execFunc)(String *)) |
template<class TReceiver> | |
void | setExtraParametersHandler (TReceiver *receiver, void(TReceiver::*execFunc)(Array *)) |
virtual void | parametersMandatory (bool isMandatory) |
virtual void | process (int argc, char **argv) |
virtual void | addArgument (String *longArg, Character *shortArg, bool mandatory, Callback< void > *callback, Object *userData=nil) |
virtual String * | collectShortOptions (void) |
virtual struct option * | collectLongOptions (void) |
virtual void | checkMissingArguments (void) |
Protected Attributes | |
OrderedCollection * | _arguments |
Callback< void > * | extra_argument_handler |
bool | parameters_mandatory |
|
Definition at line 123 of file CmdLineParser.cc. References _arguments, extra_argument_handler, nil, and parameters_mandatory. 00124 { 00125 _arguments = new OrderedCollection; 00126 extra_argument_handler = nil; 00127 parameters_mandatory = false; 00128 }
|
|
Definition at line 199 of file CmdLineParser.cc. References _arguments, and Core::OrderedCollection::add(). Referenced by addMandatoryArgument(), addMandatoryArgumentWithParameter(), addOptionalArgument(), and addOptionalArgumentWithParameter(). 00202 { 00203 CommandLineArgument *argument; 00204 argument = new CommandLineArgument(longArg, shortArg, mandatory, 00205 callback, userData); 00206 _arguments->add(argument); 00207 }
|
|
Definition at line 73 of file CmdLineParser.h. References addArgument(). 00076 { 00077 Callback1<TReceiver,void,TParam> *callback; 00078 callback = new Callback1<TReceiver,void,TParam>(receiver, execFunc); 00079 addArgument(new String(longArg), Character::value(shortArg), false, 00080 callback, userData); 00081 }
|
|
Definition at line 63 of file CmdLineParser.h. References addArgument(). 00065 { 00066 Callback0<TReceiver,void> *callback; 00067 callback = new Callback0<TReceiver,void>(receiver, execFunc); 00068 addArgument(new String(longArg), Character::value(shortArg), 00069 true, callback); 00070 }
|
|
Definition at line 54 of file CmdLineParser.h. References addArgument(), and nil. 00056 { 00057 Callback0<TReceiver,void> *callback; 00058 callback = new Callback0<TReceiver,void>(receiver, execFunc); 00059 addArgument(new String(longArg), nil, true, callback); 00060 }
|
|
Definition at line 123 of file CmdLineParser.h. References addArgument(). 00125 { 00126 Callback1<TReceiver,void,String*> *callback; 00127 callback = new Callback1<TReceiver,void,String*>(receiver, execFunc); 00128 addArgument(new String(longArg), Character::value(shortArg), 00129 false, callback); 00130 }
|
|
Definition at line 114 of file CmdLineParser.h. References addArgument(), and nil. 00116 { 00117 Callback1<TReceiver,void,String*> *callback; 00118 callback = new Callback1<TReceiver,void,String*>(receiver, execFunc); 00119 addArgument(new String(longArg), nil, false, callback); 00120 }
|
|
Definition at line 103 of file CmdLineParser.h. References addArgument(). 00106 { 00107 Callback1<TReceiver,void,TParam> *callback; 00108 callback = new Callback1<TReceiver,void,TParam>(receiver, execFunc); 00109 addArgument(new String(longArg), Character::value(shortArg), false, 00110 callback, userData); 00111 }
|
|
Definition at line 93 of file CmdLineParser.h. References addArgument(). 00095 { 00096 Callback0<TReceiver,void> *callback; 00097 callback = new Callback0<TReceiver,void>(receiver, execFunc); 00098 addArgument(new String(longArg), Character::value(shortArg), 00099 false, callback); 00100 }
|
|
Definition at line 84 of file CmdLineParser.h. References addArgument(), and nil. 00086 { 00087 Callback0<TReceiver,void> *callback; 00088 callback = new Callback0<TReceiver,void>(receiver, execFunc); 00089 addArgument(new String(longArg), nil, false, callback); 00090 }
|
|
Definition at line 142 of file CmdLineParser.h. References addArgument(). 00144 { 00145 Callback1<TReceiver,void,String*> *callback; 00146 callback = new Callback1<TReceiver,void,String*>(receiver, execFunc); 00147 addArgument(new String(longArg), Character::value(shortArg), 00148 false, callback); 00149 }
|
|
Definition at line 133 of file CmdLineParser.h. References addArgument(), and nil. 00135 { 00136 Callback1<TReceiver,void,String*> *callback; 00137 callback = new Callback1<TReceiver,void,String*>(receiver, execFunc); 00138 addArgument(new String(longArg), nil, false, callback); 00139 }
|
|
Definition at line 242 of file CmdLineParser.cc. References _arguments, Core::Iterator::finished(), Tools::CommandLineArgument::isMandatory(), Core::Collection::iterator(), Core::Iterator::next(), Core::Iterator::value(), and Tools::CommandLineArgument::wasEvaluated(). Referenced by process(). 00243 { 00244 CommandLineArgument *argument; 00245 Iterator *i; 00246 00247 for (i = _arguments->iterator(); !i->finished(); i->next()) { 00248 argument = dynamic_cast<CommandLineArgument *>(i->value()); 00249 if (argument->isMandatory() && !argument->wasEvaluated()) { 00250 (new CommandLineMissingArgument(__PRETTY_FUNCTION__, argument)) 00251 ->raiseFrom(this); 00252 } 00253 } 00254 }
|
|
Answer receiver class name. Because there isn't any standard way to obtain class name this method comes to place. Every class should rewrite this method but many didn't (yet). Reimplemented from Core::Object. Definition at line 130 of file CmdLineParser.cc. 00131 { 00132 return new String("CommandLineParser"); 00133 }
|
|
Definition at line 222 of file CmdLineParser.cc. References _arguments, Tools::CommandLineArgument::fillGetOptLongOption(), Core::Iterator::finished(), Core::Collection::iterator(), Core::Iterator::next(), Core::OrderedCollection::size(), and Core::Iterator::value(). Referenced by process(). 00223 { 00224 struct option *longOptions; 00225 CommandLineArgument *argument; 00226 Iterator *i; 00227 int index; 00228 00229 longOptions = (struct option *) GC_malloc((_arguments->size() + 1) * sizeof(struct option)); 00230 for (i = _arguments->iterator(), index = 0; !i->finished(); i->next(), index++) { 00231 argument = dynamic_cast<CommandLineArgument *>(i->value()); 00232 argument->fillGetOptLongOption(&longOptions[index]); 00233 } 00234 /* last item must by zero-filled */ 00235 longOptions[index].name = NULL; 00236 longOptions[index].has_arg = 0; 00237 longOptions[index].flag = NULL; 00238 longOptions[index].val = 0; 00239 return longOptions; 00240 }
|
|
Definition at line 209 of file CmdLineParser.cc. References _arguments, Core::Stream::contents(), Core::Iterator::finished(), Core::Collection::iterator(), Core::Iterator::next(), Core::OrderedCollection::size(), and Core::Iterator::value(). Referenced by process(). 00210 { 00211 Iterator *i; 00212 Stream *stream; 00213 stream = (new String(2 * _arguments->size()))->writeStream(); 00214 00215 for (i = _arguments->iterator(); !i->finished(); i->next()) 00216 { 00217 dynamic_cast<CommandLineArgument *>(i->value())->printGetOptShortOn(stream); 00218 } 00219 return dynamic_cast<String *>(stream->contents()); 00220 }
|
|
Definition at line 136 of file CmdLineParser.cc. References parameters_mandatory. 00137 { 00138 parameters_mandatory = isMandatory; 00139 }
|
|
Definition at line 142 of file CmdLineParser.cc. References _arguments, Tools::CommandLineArgument::acceptsParameter(), Core::OrderedCollection::at(), checkMissingArguments(), collectLongOptions(), collectShortOptions(), Core::Callback< void >::executeWith(), extra_argument_handler, Core::Iterator::finished(), Core::Collection::iterator(), Tools::CommandLineArgument::matchShortArgument(), Core::Iterator::next(), nil, parameters_mandatory, Tools::CommandLineArgument::performCallback(), Tools::CommandLineArgument::performCallbackWith(), Core::Array::put(), Core::OrderedCollection::size(), and Core::Iterator::value(). Referenced by Tools::Application::initialize(). 00143 { 00144 int option_index = 0; 00145 char *shortOptions = collectShortOptions()->asCString(); 00146 struct option *longOptions = collectLongOptions(); 00147 CommandLineArgument *argument; 00148 00149 optind = 0; /* to force getopt start from beginning */ 00150 while (1) { 00151 int c; 00152 c = getopt_long(argc, argv, shortOptions, longOptions, &option_index); 00153 if (c == -1) break; 00154 00155 if (c == 0) { /* long argument found */ 00156 if (option_index >= _arguments->size()) { 00157 (new CommandLineUnknownArgument(__PRETTY_FUNCTION__, new String(argv[optind-1]))) 00158 ->raiseFrom(this); 00159 } 00160 argument = dynamic_cast<CommandLineArgument *>(_arguments->at(option_index)); 00161 } else { /* short argument found */ 00162 Iterator *i = _arguments->iterator(); 00163 while (!i->finished()) { 00164 argument = dynamic_cast<CommandLineArgument *>(i->value()); 00165 if (argument->matchShortArgument(c)) break; 00166 i->next(); 00167 } 00168 if (i->finished()) { /* argument not found */ 00169 (new CommandLineUnknownArgument(__PRETTY_FUNCTION__, new String(argv[optind-1]))) 00170 ->raiseFrom(this); 00171 } 00172 } 00173 00174 if (argument->acceptsParameter()) { 00175 argument->performCallbackWith(new String(optarg)); 00176 } else { 00177 argument->performCallback(); 00178 } 00179 } 00180 00181 /* handle parameters */ 00182 if (optind < argc) { 00183 if (extra_argument_handler != nil) { 00184 Array *extras = new Array(argc - optind); 00185 for (int i = 0; optind + i < argc; i++) { 00186 extras->put(i, new String(argv[optind])); 00187 } 00188 extra_argument_handler->executeWith(extras); 00189 } 00190 } else if (parameters_mandatory) { 00191 (new CommandLineUnsufficientParameters(__PRETTY_FUNCTION__)) 00192 ->raiseFrom(this); 00193 } 00194 00195 checkMissingArguments(); 00196 }
|
|
Definition at line 152 of file CmdLineParser.h. References extra_argument_handler. 00154 { 00155 Callback1<TReceiver,void,Array*> *callback; 00156 callback = new Callback1<TReceiver,void,Array*>(receiver, execFunc); 00157 extra_argument_handler = callback; 00158 }
|
|
Definition at line 42 of file CmdLineParser.h. Referenced by addArgument(), checkMissingArguments(), collectLongOptions(), collectShortOptions(), CommandLineParser(), and process(). |
|
Definition at line 43 of file CmdLineParser.h. Referenced by CommandLineParser(), process(), and setExtraParametersHandler(). |
|
Definition at line 44 of file CmdLineParser.h. Referenced by CommandLineParser(), parametersMandatory(), and process(). |