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)
 

Friends

class 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 168 of file arser.h.

Constructor & Destructor Documentation

◆ Argument() [1/3]

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

Definition at line 171 of file arser.h.

171: _long_name{arg_name}, _names{arg_name} {}

◆ Argument() [2/3]

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

Definition at line 172 of file arser.h.

173 : _short_name{short_name}, _long_name{long_name}, _names{short_name, long_name}
174 {
175 }

◆ Argument() [3/3]

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

Definition at line 176 of file arser.h.

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

Member Function Documentation

◆ accumulated() [1/2]

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

Definition at line 248 of file arser.h.

249 {
250 _is_accumulated = value;
251 return *this;
252 }

◆ accumulated() [2/2]

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

Definition at line 242 of file arser.h.

243 {
244 _is_accumulated = true;
245 return *this;
246 }

◆ default_value() [1/2]

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

Definition at line 275 of file arser.h.

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

References arser::internal::to_string().

Referenced by default_value().

◆ default_value() [2/2]

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

Definition at line 291 of file arser.h.

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

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

◆ exit_with()

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

Definition at line 269 of file arser.h.

270 {
271 _func = func;
272 return *this;
273 }

◆ help() [1/2]

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

Definition at line 260 of file arser.h.

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

◆ help() [2/2]

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

Definition at line 254 of file arser.h.

255 {
256 _help_message.emplace_back(help_message);
257 return *this;
258 }

◆ nargs()

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

Definition at line 189 of file arser.h.

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

◆ required() [1/2]

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

Definition at line 236 of file arser.h.

237 {
238 _is_required = value;
239 return *this;
240 }

◆ required() [2/2]

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

Definition at line 230 of file arser.h.

231 {
232 _is_required = true;
233 return *this;
234 }

◆ type()

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

Definition at line 199 of file arser.h.

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

References type().

Referenced by type().

Friends And Related Symbol Documentation

◆ Arser

friend class Arser
friend

Definition at line 325 of file arser.h.

◆ operator<<

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

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