ONE - On-device Neural Engine
Loading...
Searching...
No Matches
arser::Argument Class Reference

#include <arser.h>

Public Member Functions

 Argument (const std::string &arg_name)
 
 Argument (const std::string &short_name, const std::string &long_name)
 
 Argument (const std::string &short_name, const std::string &long_name, const std::vector< std::string > &names)
 
Argumentnargs (uint32_t num)
 
Argumenttype (DataType type)
 
Argumentrequired (void)
 
Argumentrequired (bool value)
 
Argumentaccumulated (void)
 
Argumentaccumulated (bool value)
 
Argumenthelp (std::string help_message)
 
Argumenthelp (std::initializer_list< std::string > help_messages)
 
Argumentexit_with (const std::function< void(void)> &func)
 
template<typename T >
Argumentdefault_value (const T value)
 
template<typename T , typename... Ts>
Argumentdefault_value (const T value, const Ts... values)
 
 Argument (const std::string &arg_name)
 
 Argument (const std::string &short_name, const std::string &long_name)
 
 Argument (const std::string &short_name, const std::string &long_name, const std::vector< std::string > &names)
 
Argumentnargs (uint32_t num)
 
Argumenttype (DataType type)
 
Argumentrequired (void)
 
Argumentrequired (bool value)
 
Argumentaccumulated (void)
 
Argumentaccumulated (bool value)
 
Argumenthelp (std::string help_message)
 
Argumenthelp (std::initializer_list< std::string > help_messages)
 
Argumentexit_with (const std::function< void(void)> &func)
 
template<typename T >
Argumentdefault_value (const T value)
 
template<typename T , typename... Ts>
Argumentdefault_value (const T value, const Ts... values)
 

Friends

class Arser
 
std::ostream & operator<< (std::ostream &, const Arser &)
 
std::ostream & operator<< (std::ostream &, const Arser &)
 

Detailed Description

Argument ├── positional argument └── optioanl argument [ dash at the beginning of the string ] ├── long option [ two or more dashes ] └── short option [ one dash ]

Argument has two types - positional argument, optional argument.

The way to distinguish the two types is whether there is a dash('-') at the beginning of the string.

And, optional argument has two types as well - long option, short option, which is distinguished by the number of dash.

Definition at line 170 of file arser.h.

Constructor & Destructor Documentation

◆ Argument() [1/6]

arser::Argument::Argument ( const std::string &  arg_name)
inlineexplicit

Definition at line 173 of file arser.h.

173: _long_name{arg_name}, _names{arg_name} {}

◆ Argument() [2/6]

arser::Argument::Argument ( const std::string &  short_name,
const std::string &  long_name 
)
inlineexplicit

Definition at line 174 of file arser.h.

175 : _short_name{short_name}, _long_name{long_name}, _names{short_name, long_name}
176 {
177 }

◆ Argument() [3/6]

arser::Argument::Argument ( const std::string &  short_name,
const std::string &  long_name,
const std::vector< std::string > &  names 
)
inlineexplicit

Definition at line 178 of file arser.h.

180 : _short_name{short_name}, _long_name{long_name}, _names{names}
181 {
182 // 'names' must have 'short_name' and 'long_name'.
183 auto it = std::find(names.begin(), names.end(), short_name);
184 assert(it != names.end());
185 it = std::find(names.begin(), names.end(), long_name);
186 assert(it != names.end());
187 // for avoiding unused warning.
188 (void)it;
189 }

◆ Argument() [4/6]

arser::Argument::Argument ( const std::string &  arg_name)
inlineexplicit

Definition at line 173 of file arser.h.

173: _long_name{arg_name}, _names{arg_name} {}

◆ Argument() [5/6]

arser::Argument::Argument ( const std::string &  short_name,
const std::string &  long_name 
)
inlineexplicit

Definition at line 174 of file arser.h.

175 : _short_name{short_name}, _long_name{long_name}, _names{short_name, long_name}
176 {
177 }

◆ Argument() [6/6]

arser::Argument::Argument ( const std::string &  short_name,
const std::string &  long_name,
const std::vector< std::string > &  names 
)
inlineexplicit

Definition at line 178 of file arser.h.

180 : _short_name{short_name}, _long_name{long_name}, _names{names}
181 {
182 // 'names' must have 'short_name' and 'long_name'.
183 auto it = std::find(names.begin(), names.end(), short_name);
184 assert(it != names.end());
185 it = std::find(names.begin(), names.end(), long_name);
186 assert(it != names.end());
187 // for avoiding unused warning.
188 (void)it;
189 }

Member Function Documentation

◆ accumulated() [1/4]

Argument & arser::Argument::accumulated ( bool  value)
inline

Definition at line 250 of file arser.h.

251 {
252 _is_accumulated = value;
253 return *this;
254 }

◆ accumulated() [2/4]

Argument & arser::Argument::accumulated ( bool  value)
inline

Definition at line 250 of file arser.h.

251 {
252 _is_accumulated = value;
253 return *this;
254 }

◆ accumulated() [3/4]

Argument & arser::Argument::accumulated ( void  )
inline

Definition at line 244 of file arser.h.

245 {
246 _is_accumulated = true;
247 return *this;
248 }

◆ accumulated() [4/4]

Argument & arser::Argument::accumulated ( void  )
inline

Definition at line 244 of file arser.h.

245 {
246 _is_accumulated = true;
247 return *this;
248 }

◆ default_value() [1/4]

template<typename T >
Argument & arser::Argument::default_value ( const T  value)
inline

Definition at line 277 of file arser.h.

278 {
279 if ((_nargs <= 1 && TypeName<T>::Get() == _type) ||
280 (_nargs > 1 && TypeName<std::vector<T>>::Get() == _type))
281 _values.emplace_back(internal::to_string(value));
282 else
283 {
284 throw std::runtime_error("Type mismatch. "
285 "You called default_value() method with a type different "
286 "from the one you specified. "
287 "Please check the type of what you specified in "
288 "add_argument() method.");
289 }
290 return *this;
291 }
std::string to_string(const T value)
Definition arser.h:62
static const char * Get()
Definition arser.h:102

References arser::internal::to_string().

Referenced by default_value().

◆ default_value() [2/4]

template<typename T >
Argument & arser::Argument::default_value ( const T  value)
inline

Definition at line 277 of file arser.h.

278 {
279 if ((_nargs <= 1 && TypeName<T>::Get() == _type) ||
280 (_nargs > 1 && TypeName<std::vector<T>>::Get() == _type))
281 _values.emplace_back(internal::to_string(value));
282 else
283 {
284 throw std::runtime_error("Type mismatch. "
285 "You called default_value() method with a type different "
286 "from the one you specified. "
287 "Please check the type of what you specified in "
288 "add_argument() method.");
289 }
290 return *this;
291 }

References arser::internal::to_string().

◆ default_value() [3/4]

template<typename T , typename... Ts>
Argument & arser::Argument::default_value ( const T  value,
const Ts...  values 
)
inline

Definition at line 293 of file arser.h.

294 {
295 if ((_nargs <= 1 && TypeName<T>::Get() == _type) ||
296 (_nargs > 1 && TypeName<std::vector<T>>::Get() == _type))
297 {
298 _values.emplace_back(internal::to_string(value));
299 default_value(values...);
300 }
301 else
302 {
303 throw std::runtime_error("Type mismatch. "
304 "You called default_value() method with a type different "
305 "from the one you specified. "
306 "Please check the type of what you specified in "
307 "add_argument() method.");
308 }
309 return *this;
310 }
Argument & default_value(const T value)
Definition arser.h:277

References default_value(), and arser::internal::to_string().

◆ default_value() [4/4]

template<typename T , typename... Ts>
Argument & arser::Argument::default_value ( const T  value,
const Ts...  values 
)
inline

Definition at line 293 of file arser.h.

294 {
295 if ((_nargs <= 1 && TypeName<T>::Get() == _type) ||
296 (_nargs > 1 && TypeName<std::vector<T>>::Get() == _type))
297 {
298 _values.emplace_back(internal::to_string(value));
299 default_value(values...);
300 }
301 else
302 {
303 throw std::runtime_error("Type mismatch. "
304 "You called default_value() method with a type different "
305 "from the one you specified. "
306 "Please check the type of what you specified in "
307 "add_argument() method.");
308 }
309 return *this;
310 }

References default_value(), and arser::internal::to_string().

◆ exit_with() [1/2]

Argument & arser::Argument::exit_with ( const std::function< void(void)> &  func)
inline

Definition at line 271 of file arser.h.

272 {
273 _func = func;
274 return *this;
275 }

◆ exit_with() [2/2]

Argument & arser::Argument::exit_with ( const std::function< void(void)> &  func)
inline

Definition at line 271 of file arser.h.

272 {
273 _func = func;
274 return *this;
275 }

◆ help() [1/4]

Argument & arser::Argument::help ( std::initializer_list< std::string >  help_messages)
inline

Definition at line 262 of file arser.h.

263 {
264 if (help_messages.size() == 0)
265 throw std::runtime_error("Empty help message list");
266
267 _help_message = help_messages;
268 return *this;
269 }

◆ help() [2/4]

Argument & arser::Argument::help ( std::initializer_list< std::string >  help_messages)
inline

Definition at line 262 of file arser.h.

263 {
264 if (help_messages.size() == 0)
265 throw std::runtime_error("Empty help message list");
266
267 _help_message = help_messages;
268 return *this;
269 }

◆ help() [3/4]

Argument & arser::Argument::help ( std::string  help_message)
inline

Definition at line 256 of file arser.h.

257 {
258 _help_message.emplace_back(help_message);
259 return *this;
260 }

◆ help() [4/4]

Argument & arser::Argument::help ( std::string  help_message)
inline

Definition at line 256 of file arser.h.

257 {
258 _help_message.emplace_back(help_message);
259 return *this;
260 }

◆ nargs() [1/2]

Argument & arser::Argument::nargs ( uint32_t  num)
inline

Definition at line 191 of file arser.h.

192 {
193 if (num == 0)
194 {
195 _type = "bool";
196 }
197 _nargs = num;
198 return *this;
199 }

◆ nargs() [2/2]

Argument & arser::Argument::nargs ( uint32_t  num)
inline

Definition at line 191 of file arser.h.

192 {
193 if (num == 0)
194 {
195 _type = "bool";
196 }
197 _nargs = num;
198 return *this;
199 }

◆ required() [1/4]

Argument & arser::Argument::required ( bool  value)
inline

Definition at line 238 of file arser.h.

239 {
240 _is_required = value;
241 return *this;
242 }

◆ required() [2/4]

Argument & arser::Argument::required ( bool  value)
inline

Definition at line 238 of file arser.h.

239 {
240 _is_required = value;
241 return *this;
242 }

◆ required() [3/4]

Argument & arser::Argument::required ( void  )
inline

Definition at line 232 of file arser.h.

233 {
234 _is_required = true;
235 return *this;
236 }

◆ required() [4/4]

Argument & arser::Argument::required ( void  )
inline

Definition at line 232 of file arser.h.

233 {
234 _is_required = true;
235 return *this;
236 }

◆ type() [1/2]

Argument & arser::Argument::type ( DataType  type)
inline

Definition at line 201 of file arser.h.

202 {
203 switch (type)
204 {
205 case DataType::INT32:
206 _type = "int";
207 break;
208 case DataType::INT32_VEC:
209 _type = "vector<int>";
210 break;
211 case DataType::FLOAT:
212 _type = "float";
213 break;
214 case DataType::FLOAT_VEC:
215 _type = "vector<float>";
216 break;
217 case DataType::BOOL:
218 _type = "bool";
219 break;
220 case DataType::STR:
221 _type = "string";
222 break;
223 case DataType::STR_VEC:
224 _type = "vector<string>";
225 break;
226 default:
227 throw std::runtime_error("NYI DataType");
228 }
229 return *this;
230 }
Argument & type(DataType type)
Definition arser.h:201

References type().

Referenced by type().

◆ type() [2/2]

Argument & arser::Argument::type ( DataType  type)
inline

Definition at line 201 of file arser.h.

202 {
203 switch (type)
204 {
205 case DataType::INT32:
206 _type = "int";
207 break;
208 case DataType::INT32_VEC:
209 _type = "vector<int>";
210 break;
211 case DataType::FLOAT:
212 _type = "float";
213 break;
214 case DataType::FLOAT_VEC:
215 _type = "vector<float>";
216 break;
217 case DataType::BOOL:
218 _type = "bool";
219 break;
220 case DataType::STR:
221 _type = "string";
222 break;
223 case DataType::STR_VEC:
224 _type = "vector<string>";
225 break;
226 default:
227 throw std::runtime_error("NYI DataType");
228 }
229 return *this;
230 }

References type().

Friends And Related Symbol Documentation

◆ Arser

Arser
friend

Definition at line 327 of file arser.h.

◆ operator<< [1/2]

std::ostream & operator<< ( std::ostream &  ,
const Arser  
)
friend

◆ operator<< [2/2]

std::ostream & operator<< ( std::ostream &  ,
const Arser  
)
friend

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