Use this class to define program options and parse command line arguments.
More...
#include <anyarg.h>
|
| Anyarg () |
| Construct a Anyarg object. More...
|
|
bool | parse_argv (int argc, char **argv) |
|
int | get_argc () const |
|
const char * | get_arg (int i) const |
|
const char * | auto_usage () |
|
|
bool | add_flag (const char *name, char letter, const char *desc) |
|
bool | add_flag (char letter, const char *desc) |
|
|
bool | add_option_str (const char *name, char letter, const char *v0, const char *desc) |
|
bool | add_option_str (char letter, const char *v0, const char *desc) |
|
bool | add_option_int (char letter, int v0, const char *desc) |
|
bool | add_option_int (const char *name, char letter, int v0, const char *desc) |
|
bool | add_option_double (char letter, double v0, const char *desc) |
|
bool | add_option_double (const char *name, char letter, double v0, const char *desc) |
|
|
bool | is_true (const char *name) const |
|
bool | is_true (char letter) const |
|
|
const char * | get_value_str (const char *name) const |
|
const char * | get_value_str (char letter) const |
|
int | get_value_int (const char *name) const |
|
int | get_value_int (char letter) const |
|
double | get_value_double (const char *name) const |
|
double | get_value_double (char letter) const |
|
Use this class to define program options and parse command line arguments.
◆ Anyarg()
◆ add_flag() [1/2]
bool Anyarg::add_flag |
( |
const char * |
name, |
|
|
char |
letter, |
|
|
const char * |
desc |
|
) |
| |
Add a boolean flag to a program.
- Parameters
-
name | Long name of a flag. Using hypen to connect multiple words, such as "buffer-size". |
letter | Single-letter label of a flag, such as 'i'. Set letter to 0 (not '0') if you do not need a short label. |
desc | Description of a flag, which will be used to generate usage. Set desc as "" if nothing to say. |
- Returns
true
if success, false
if the flag has been defined.
- Note
- The default value of flag is
false
.
◆ add_flag() [2/2]
bool Anyarg::add_flag |
( |
char |
letter, |
|
|
const char * |
desc |
|
) |
| |
◆ add_option_double() [1/2]
bool Anyarg::add_option_double |
( |
char |
letter, |
|
|
double |
v0, |
|
|
const char * |
desc |
|
) |
| |
◆ add_option_double() [2/2]
bool Anyarg::add_option_double |
( |
const char * |
name, |
|
|
char |
letter, |
|
|
double |
v0, |
|
|
const char * |
desc |
|
) |
| |
◆ add_option_int() [1/2]
bool Anyarg::add_option_int |
( |
char |
letter, |
|
|
int |
v0, |
|
|
const char * |
desc |
|
) |
| |
◆ add_option_int() [2/2]
bool Anyarg::add_option_int |
( |
const char * |
name, |
|
|
char |
letter, |
|
|
int |
v0, |
|
|
const char * |
desc |
|
) |
| |
◆ add_option_str() [1/2]
bool Anyarg::add_option_str |
( |
const char * |
name, |
|
|
char |
letter, |
|
|
const char * |
v0, |
|
|
const char * |
desc |
|
) |
| |
Add a taking-value option to a program.
- Parameters
-
name | Long name of a flag. Using hypen to connect multiple words, such as "buffer-size". |
letter | Single-letter label of a flag, such as 'i'. Set letter to 0 if you do not need a short label. |
v0 | The default value of this option. |
desc | Description of a flag, which will be used to generate usage. Set desc as "" if nothing to say. If description starts with an = (like: "=FILE Name of output file"), the word followed will be treated as a META word, which will be used in the generation of option usage, e.g.: "-f FILE, --filename=FILE". |
- Returns
true
if success, false
if the flag has been defined.
◆ add_option_str() [2/2]
bool Anyarg::add_option_str |
( |
char |
letter, |
|
|
const char * |
v0, |
|
|
const char * |
desc |
|
) |
| |
◆ auto_usage()
const char* Anyarg::auto_usage |
( |
| ) |
|
Generate well-formatted usage information for options defined for the program.
- Returns
- Usage information as a c-string.
◆ get_arg()
const char* Anyarg::get_arg |
( |
int |
i | ) |
const |
Get a non-option argument by index
- Parameters
-
i | The index of a non-option arguments, ranging from 0 to get_argc() - 1. Non-option arguments are in the same order as they appeared in command line. |
◆ get_argc()
int Anyarg::get_argc |
( |
| ) |
const |
Get the number of non-option arguments.
- Returns
- The count of non-option arguments.
◆ get_value_double() [1/2]
double Anyarg::get_value_double |
( |
const char * |
name | ) |
const |
◆ get_value_double() [2/2]
double Anyarg::get_value_double |
( |
char |
letter | ) |
const |
◆ get_value_int() [1/2]
int Anyarg::get_value_int |
( |
const char * |
name | ) |
const |
◆ get_value_int() [2/2]
int Anyarg::get_value_int |
( |
char |
letter | ) |
const |
◆ get_value_str() [1/2]
const char* Anyarg::get_value_str |
( |
const char * |
name | ) |
const |
Get option value.
- Parameters
-
name | The same name used in add_option_xxx(). |
letter | Single-letter label of a flag, such as 'i'. |
- Returns
- The value of an option. If the value is not assigned in command line, the default value will be returned.
- Note
- get_value functions should match with add_option functions. Use get_value_str(), get_value_int(), get_value_double() to get the value of an option whose value is of type string, int and double, respectively.
◆ get_value_str() [2/2]
const char* Anyarg::get_value_str |
( |
char |
letter | ) |
const |
◆ is_true() [1/2]
bool Anyarg::is_true |
( |
const char * |
name | ) |
const |
Check whether a flag is set to true in the command line.
- Parameters
-
name | The same name used in add_flag(). |
letter | Single-letter label of a flag. |
- Returns
true
if a flag is set in command line, false
if not.
◆ is_true() [2/2]
bool Anyarg::is_true |
( |
char |
letter | ) |
const |
◆ parse_argv()
bool Anyarg::parse_argv |
( |
int |
argc, |
|
|
char ** |
argv |
|
) |
| |
Parse command line arguments.
- Precondition
- Flags and options have to be defined by add_flag() and add_option_xxx() before the parsing of command line.
- Parameters
-
argc | The number of arguments in command line. |
argv | Array including all command line arugments. |
- Returns
true
if success, false
if fail.
- Note
- The frist elements of
argv
MUST be the name of the program.
The documentation for this class was generated from the following file: