Compute Library
 22.08
CommandLineParser Class Referencefinal

Class to parse command line arguments. More...

#include <CommandLineParser.h>

Public Member Functions

 CommandLineParser ()=default
 Default constructor. More...
 
template<typename T , typename... As>
T * add_option (const std::string &name, As &&... args)
 Function to add a new option to the parser. More...
 
template<typename T , typename... As>
T * add_positional_option (As &&... args)
 Function to add a new positional argument to the parser. More...
 
void parse (int argc, char **argv)
 Parses the command line arguments and updates the options accordingly. More...
 
bool validate () const
 Validates the previously parsed command line arguments. More...
 
void print_help (const std::string &program_name) const
 Prints a help message for all configured options. More...
 

Detailed Description

Class to parse command line arguments.

Definition at line 45 of file CommandLineParser.h.

Constructor & Destructor Documentation

◆ CommandLineParser()

CommandLineParser ( )
default

Default constructor.

Member Function Documentation

◆ add_option()

T * add_option ( const std::string &  name,
As &&...  args 
)
inline

Function to add a new option to the parser.

Parameters
[in]nameName of the option. Will be available under –name=VALUE.
[in]argsOption specific configuration arguments.
Returns
Pointer to the option. The option is owned by the parser.

Definition at line 104 of file CommandLineParser.h.

References GemmTuner::args.

Referenced by CommonGemmExampleOptions::CommonGemmExampleOptions(), CommonGraphOptions::CommonGraphOptions(), CommonOptions::CommonOptions(), main(), and arm_compute::utils::run_example().

105 {
106  auto result = _options.emplace(name, std::make_unique<T>(name, std::forward<As>(args)...));
107  return static_cast<T *>(result.first->second.get());
108 }
const char * name

◆ add_positional_option()

T * add_positional_option ( As &&...  args)
inline

Function to add a new positional argument to the parser.

Parameters
[in]argsOption specific configuration arguments.
Returns
Pointer to the option. The option is owned by the parser.

Definition at line 111 of file CommandLineParser.h.

References GemmTuner::args.

Referenced by main().

112 {
113  _positional_options.emplace_back(std::make_unique<T>(std::forward<As>(args)...));
114  return static_cast<T *>(_positional_options.back().get());
115 }

◆ parse()

void parse ( int  argc,
char **  argv 
)
inline

Parses the command line arguments and updates the options accordingly.

Parameters
[in]argcNumber of arguments.
[in]argvArguments.

Definition at line 117 of file CommandLineParser.h.

References name, arm_compute::test::validation::success, and arm_compute::utility::tolower().

Referenced by GraphValidateExample< DepthwiseConvolutionLayer, DepthConvolutionOptions, DepthConvolutionVerifyAccessor >::do_setup(), main(), and arm_compute::utils::run_example().

118 {
119  const std::regex option_regex{ "--((?:no-)?)([^=]+)(?:=(.*))?" };
120 
121  const auto set_option = [&](const std::string & option, const std::string & name, const std::string & value)
122  {
123  if(_options.find(name) == _options.end())
124  {
125  _unknown_options.push_back(option);
126  return;
127  }
128 
129  const bool success = _options[name]->parse(value);
130 
131  if(!success)
132  {
133  _invalid_options.push_back(option);
134  }
135  };
136 
137  unsigned int positional_index = 0;
138 
139  for(int i = 1; i < argc; ++i)
140  {
141  std::string mixed_case_opt{ argv[i] };
142  int equal_sign = mixed_case_opt.find('=');
143  int pos = (equal_sign == -1) ? strlen(argv[i]) : equal_sign;
144 
145  const std::string option = arm_compute::utility::tolower(mixed_case_opt.substr(0, pos)) + mixed_case_opt.substr(pos);
146  std::smatch option_matches;
147 
148  if(std::regex_match(option, option_matches, option_regex))
149  {
150  // Boolean option
151  if(option_matches.str(3).empty())
152  {
153  set_option(option, option_matches.str(2), option_matches.str(1).empty() ? "true" : "false");
154  }
155  else
156  {
157  // Can't have "no-" and a value
158  if(!option_matches.str(1).empty())
159  {
160  _invalid_options.emplace_back(option);
161  }
162  else
163  {
164  set_option(option, option_matches.str(2), option_matches.str(3));
165  }
166  }
167  }
168  else
169  {
170  if(positional_index >= _positional_options.size())
171  {
172  _invalid_options.push_back(mixed_case_opt);
173  }
174  else
175  {
176  _positional_options[positional_index]->parse(mixed_case_opt);
177  ++positional_index;
178  }
179  }
180  }
181 }
std::string tolower(std::string string)
Convert string to lower case.
Definition: Utility.h:205
const char * name

◆ print_help()

void print_help ( const std::string &  program_name) const
inline

Prints a help message for all configured options.

Parameters
[in]program_nameName of the program to be used in the help message.

Definition at line 218 of file CommandLineParser.h.

Referenced by GraphValidateExample< DepthwiseConvolutionLayer, DepthConvolutionOptions, DepthConvolutionVerifyAccessor >::do_setup(), main(), and arm_compute::utils::run_example().

219 {
220  std::cout << "usage: " << program_name << " \n";
221 
222  for(const auto &option : _options)
223  {
224  std::cout << option.second->help() << "\n";
225  }
226 
227  for(const auto &option : _positional_options)
228  {
229  std::string help_to_print;
230 
231  // Extract help sub-string
232  const std::string help_str = option->help();
233  const size_t help_pos = help_str.find(" - ");
234  if(help_pos != std::string::npos)
235  {
236  help_to_print = help_str.substr(help_pos);
237  }
238 
239  std::cout << option->name() << help_to_print << "\n";
240  }
241 }

◆ validate()

bool validate ( ) const
inline

Validates the previously parsed command line arguments.

Validation fails if not all required options are provided. Additionally warnings are generated for options that have illegal values or unknown options.

Returns
True if all required options have been provided.

Definition at line 183 of file CommandLineParser.h.

Referenced by main().

184 {
185  bool is_valid = true;
186 
187  for(const auto &option : _options)
188  {
189  if(option.second->is_required() && !option.second->is_set())
190  {
191  is_valid = false;
192  std::cerr << "ERROR: Option '" << option.second->name() << "' is required but not given!\n";
193  }
194  }
195 
196  for(const auto &option : _positional_options)
197  {
198  if(option->is_required() && !option->is_set())
199  {
200  is_valid = false;
201  std::cerr << "ERROR: Option '" << option->name() << "' is required but not given!\n";
202  }
203  }
204 
205  for(const auto &option : _unknown_options)
206  {
207  std::cerr << "WARNING: Skipping unknown option '" << option << "'!\n";
208  }
209 
210  for(const auto &option : _invalid_options)
211  {
212  std::cerr << "WARNING: Skipping invalid option '" << option << "'!\n";
213  }
214 
215  return is_valid;
216 }

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