MCPcopy Create free account
hub / github.com/RangeNetworks/openbts / cliParse

Function cliParse

CLI/CLI.cpp:235–269  ·  view source on GitHub ↗

Parse options in optstring out of argc,argv. The optstring is a space separated list of options. The options need not start with '-'. To recognize just "-" or "--" just add it in. Return a map containing the options found; if option in optstring was followed by ':', map value will be the next argv argument, otherwise "true". Leave argc,argv pointing at the first argument after the options, ie, o

Source from the content-addressed store, hash-verified

233// Leave argc,argv pointing at the first argument after the options, ie, on return argc is the number of non-option arguments remaining in argv.
234// This routine does not allow combining options, ie, -a -b != -ab
235static map<string,string> cliParse(int &argc, char **&argv, ostream &os, const char *optstring)
236{
237 map<string,string> options; // The result
238 // Skip the command name.
239 argc--, argv++;
240 // Parse args.
241 for ( ; argc > 0; argc--, argv++ ) {
242 char *arg = argv[0];
243 // The argv to match may not contain ':' to prevent the pathological case, for example, where optionlist contains "-a:" and command line arg is "-a:"
244 if (strchr(arg,':')) { return options; } // Can't parse this, too dangerous.
245 const char *op = strstr(optstring,arg);
246 if (op && (op == optstring || op[-1] == ' ')) {
247 const char *ep = op + strlen(arg);
248 if (*ep == ':') {
249 // This valid option requires an argument.
250 argc--, argv++;
251 if (argc <= 0) { throw CLIParseError(format("expected argument after: %s",arg)); }
252 options[arg] = string(argv[0]);
253 continue;
254 } else if (*ep == 0 || *ep == ' ') {
255 // This valid option does not require an argument.
256 options[arg] = string("true");
257 continue;
258 } else {
259 // Partial match of something in optstring; drop through to treat it like any other argument.
260 }
261 } else {
262 break; // Return when we find the first non-option.
263 }
264 // An argument beginning with - and not in optstring is an unrecognized option and is an error.
265 if (*arg == '-') { throw CLIParseError(format("unrecognized argument: %s",arg)); }
266 return options;
267 }
268 return options;
269}
270
271
272/**@name Commands for the CLI. */

Callers 5

tmsisFunction · 0.85
callsFunction · 0.85
transactionsFunction · 0.85
chansFunction · 0.85
neighborsFunction · 0.85

Calls 1

CLIParseErrorClass · 0.85

Tested by

no test coverage detected