Compute Library
 21.05
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 44 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 103 of file CommandLineParser.h.

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

References GemmTuner::args, and 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 110 of file CommandLineParser.h.

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

References GemmTuner::args.

◆ 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 116 of file CommandLineParser.h.

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

References name, and arm_compute::utility::tolower().

◆ 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 217 of file CommandLineParser.h.

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

◆ 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 182 of file CommandLineParser.h.

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

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