ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnc::cli::CommandLine Class Reference

this class describes a common command line interface More...

#include <CommandLine.h>

Public Member Functions

 CommandLine (const CommandLine &)=delete
 
CommandLineoperator= (const CommandLine &)=delete
 
void parseCommandLine (int argc, const char **argv, bool check_nonoptional=true)
 parse command line option
 
void registerOption (IOption *opt)
 register option for parser
 

Static Public Member Functions

static CommandLinegetParser ()
 singleton method
 

Detailed Description

this class describes a common command line interface

Definition at line 304 of file CommandLine.h.

Constructor & Destructor Documentation

◆ CommandLine()

nnc::cli::CommandLine::CommandLine ( const CommandLine )
delete

Member Function Documentation

◆ getParser()

CommandLine * nnc::cli::CommandLine::getParser ( )
static

singleton method

Definition at line 106 of file CommandLine.cpp.

107{
108 static CommandLine Parser;
109
110 return &Parser;
111
112} // getParser
CommandLine(const CommandLine &)=delete

Referenced by main(), main(), nnc::cli::Option< T >::Option(), and TEST().

◆ operator=()

CommandLine & nnc::cli::CommandLine::operator= ( const CommandLine )
delete

◆ parseCommandLine()

void nnc::cli::CommandLine::parseCommandLine ( int  argc,
const char **  argv,
bool  check_nonoptional = true 
)

parse command line option

Parameters
argc- number of command line arguments
argv- command line arguments
check_nonoptional- if true then check that all non optional declared options are presented

Definition at line 466 of file CommandLine.cpp.

467{
468 std::set<std::string> cmd_args;
469 IOption *opt;
470 const char *arg_val = nullptr;
471
472 _prog_name = argv[0];
473 _args_num = argc;
474
475 if (argc == 1)
476 {
477 // empty command line
478 usage();
479 }
480
481 // search help option and print help if this option is passed
482 for (int i = 1; i < argc; i++)
483 {
484 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
485 {
486 usage("", EXIT_SUCCESS);
487 }
488 }
489
490 for (int i = 1; i < argc; i += (argv[i + 1] == arg_val) ? 2 : 1)
491 {
492 if (argv[i][0] != '-')
493 {
494 std::string err_msg(std::string("invalid command line argument: ") + argv[i]);
495 usage(err_msg);
496 }
497
498 // find registered option
499 try
500 {
501 opt = findOption(argv[i]);
502 }
503 catch (BadOption &e)
504 {
505 std::string err_msg(std::string("invalid option: ") + e.getName());
506 usage(err_msg);
507 }
508
509 // figure out value for option
510 try
511 {
512 if (opt->canHaveSeveralVals())
513 {
514 int j = i + 1;
515 for (arg_val = findValueForMultOption(opt, argv[i], argv, j); arg_val;
516 arg_val = findValueForMultOption(opt, argv[i], argv, j))
517 {
518 // set value for option
519 opt->setValue(arg_val);
520 j++;
521 }
522
523 i = j - 1;
524 }
525 else
526 {
527 arg_val = findOptionValue(opt, argv, i);
528
529 // set value for option
530 opt->setValue(arg_val);
531 }
532 }
533 catch (BadOption &e)
534 {
535 std::string optname = e.getName();
536 optname = optname.empty() ? argv[i] : optname;
537 std::string err_msg(std::string("invalid value: ") + e.getValue() +
538 std::string(" for option: ") + optname);
539 usage(err_msg);
540 }
541
542 // we can't just put argv[i] because option can have separators
543 cmd_args.insert(opt->getNames()[0]);
544 }
545
546 if (check_nonoptional)
547 {
548 // check that all registered options are present in command line
549 checkRegisteredOptions(cmd_args);
550 }
551
552 // verify options
553 checkOptions(cmd_args);
554
555} // parseCommandLine
std::vector< std::string > optname(const char *names)
convert option names for Option constructor

References nnc::cli::IOption::canHaveSeveralVals(), nnc::cli::BadOption::getName(), nnc::cli::IOption::getNames(), nnc::cli::BadOption::getValue(), nnc::cli::optname(), and nnc::cli::IOption::setValue().

Referenced by main(), main(), and TEST().

◆ registerOption()

void nnc::cli::CommandLine::registerOption ( IOption opt)

register option for parser

Parameters
opt- option

Definition at line 199 of file CommandLine.cpp.

200{
201 for (const auto &n : opt->getNames())
202 {
203 auto i = _options_name.emplace(n, opt);
204
205 if (!i.second)
206 {
207 std::cerr << "option name must be unique: `" << n << "'" << std::endl;
208 exit(EXIT_FAILURE);
209 }
210 }
211
212 _options.push_back(opt);
213
214 if (opt->isGrouped())
215 {
216 auto it = _grouped_options.find(opt->getGroup());
217
218 if (it == _grouped_options.end())
219 _grouped_options.emplace(opt->getGroup(), std::vector<IOption *>{opt});
220 else
221 it->second.push_back(opt);
222 }
223
224} // registerOption

References nnc::cli::IOption::getGroup(), nnc::cli::IOption::getNames(), and nnc::cli::IOption::isGrouped().

Referenced by nnc::cli::Option< T >::Option().


The documentation for this class was generated from the following files: