INGOR
Loading...
Searching...
No Matches
Public Member Functions | List of all members
ytParseArgs Class Reference

Structure for parsing command line arguments. More...

#include <util/ytParseArgs.h>

Public Member Functions

ytParseArgsytParseArgs_new ()
 Creates a new ytParseArgs instance.
 
void ytParseArgs_delete (ytParseArgs *this)
 Deletes the ytParseArgs instance.
 
void ytParseArgs_addString (ytParseArgs *this, const char *key, char **arg)
 Adds a key that takes a string (char *) value.
 
void ytParseArgs_addStringOpt (ytParseArgs *this, const char *key, char **arg)
 Adds a key that takes a string (char *) value. The value can be omitted.
 
void ytParseArgs_addInt (ytParseArgs *this, const char *key, int *arg)
 Adds a key that takes an int value.
 
void ytParseArgs_addDouble (ytParseArgs *this, const char *key, double *arg)
 Adds a key that takes a double value.
 
void ytParseArgs_addOn (ytParseArgs *this, const char *key, int *arg)
 Adds a key that sets an int value of 1 if it exists.
 
void ytParseArgs_addOff (ytParseArgs *this, const char *key, int *arg)
 Adds a key that sets an int value of 0 if it exists.
 
void ytParseArgs_addOnOff (ytParseArgs *this, const char *key, int *arg)
 Adds a key that sets an int value of 0 or 1.
 
void ytParseArgs_addIntArray (ytParseArgs *this, const char *key, ytIntArray **arg)
 Adds a key that sets an integer array to a ytIntArray instance.
 
void ytParseArgs_addStrArray (ytParseArgs *this, const char *key, ytStrArray **arg)
 Adds a key that sets a string array to a ytStrArray instance.
 
void ytParseArgs_addStrArrayAdd (ytParseArgs *this, const char *key, ytStrArray **arg)
 Adds a key that adds a string to a ytStrArray instance.
 
void ytParseArgs_addDoubleArray (ytParseArgs *this, const char *key, ytDoubleArray **arg)
 Adds a key that sets a double array to a ytDoubleArray instance.
 
void ytParseArgs_addKeyValues (ytParseArgs *this, const char *key, ytKeyValues **arg)
 Adds a key that accepts key=value style values as ytKeyValues.
 
void ytParseArgs_addKeyValuesArray (ytParseArgs *this, const char *key, ytArray **arg)
 Adds a key that accepts key=value style values as a ytKeyValues instance and adds them to the ytArray instance.
 
void ytParseArgs_addFunc (ytParseArgs *this, const char *key, void(*f)())
 Adds a key that calls the specified function.
 
void ytParseArgs_addFunc3 (ytParseArgs *this, const char *key, void(*f)(const char *, const char *, void *), void *arg)
 Adds a key that calls the specified function with its argument.
 
void ytParseArgs_addNone (ytParseArgs *this, const char *key)
 Adds a key to ignore.
 
int ytParseArgs_set (ytParseArgs *parser, int argc, char **argv)
 Parses command line arguments.
 
int ytParseArgs_setKV (ytParseArgs *parser, const ytKeyValues *kv, int opt)
 Sets values in the variables from the ytKeyValues instance.
 
ytKeyValuesytParseArgs_genKV (int argc, char **argv, const char *defKey)
 Generates ytKeyValues instance from command line arguments.
 
void ytParseArgs_verbose (ytParseArgs *this, int e)
 Sets the verbose level.
 
#define ytParseArgs_DEFAULT_KEY
 Default key for an argument without a key.
 
#define ytParseArgs_OPT_ERROR
 Specifies to stop if the action is not registered.
 
#define ytParseArgs_OPT_WARNING
 Specifies to put a warning message if the action is not registered.
 
#define ytParseArgs_OPT_IGNORE
 Specifies to ignore if the action is not registered.
 
#define ytPA_int(this, key, arg)   ytParseArgs_addInt(this, key, arg)
 Short Macro.
 
#define ytPA_double(this, key, arg)   ytParseArgs_addDouble(this, key, arg)
 Short Macro.
 
#define ytPA_str(this, key, arg)   ytParseArgs_addString(this, key, arg)
 Short Macro.
 

Detailed Description

Structure for parsing command line arguments.

Specify the array of this structure as the first argument of ytParseArgs_set(). The field ParseArgs::key of the last element of the array has to be NULL to represent the end of the array.

Example

int main(int argc, char * argv[]){
int v = 0;
char * s = "not set";
ytParseArgs_addInt(parser, "-v", &v);
ytParseArgs_addString(parser, "-s", &c};
int r = ytParseArgs_set(parser, argc, argv);
if(r > 0){
printf("Parsing Error: %s\n", argv[r]);
}
// If "-v 10" is specified, 10 is set to "v".
// Otherwise, v == 0.
printf("v = %d\n", v);
// If "-s foobar" is specified, "foobar" is set to "s".
// Otherwise, s == "not set".
printf("s = %s\n", s);
// Releases the resources.
return 0;
}
Structure for parsing command line arguments.
int ytParseArgs_set(ytParseArgs *parser, int argc, char **argv)
Parses command line arguments.
Definition ytParseArgs.c:1018
ytParseArgs * ytParseArgs_new()
Creates a new ytParseArgs instance.
Definition ytParseArgs.c:178
void ytParseArgs_delete(ytParseArgs *this)
Deletes the ytParseArgs instance.
Definition ytParseArgs.c:195
void ytParseArgs_addInt(ytParseArgs *this, const char *key, int *arg)
Adds a key that takes an int value.
Definition ytParseArgs.c:296
void ytParseArgs_addString(ytParseArgs *this, const char *key, char **arg)
Adds a key that takes a string (char *) value.
Definition ytParseArgs.c:278

ytLib Programming API Document

Member Function Documentation

◆ ytParseArgs_addDouble()

void ytParseArgs_addDouble ( ytParseArgs this,
const char *  key,
double *  arg 
)

Adds a key that takes a double value.

Parameters
this
keykey of this argument.
argpointer to the variable to be set by this argument.

◆ ytParseArgs_addDoubleArray()

void ytParseArgs_addDoubleArray ( ytParseArgs this,
const char *  key,
ytDoubleArray **  arg 
)

Adds a key that sets a double array to a ytDoubleArray instance.

If the arg points to NULL, then it automatically allocates a new ytDoubleArray instance.

Parameters
this
keykey of this argument.
argpointer to the variable to be set by this argument.

◆ ytParseArgs_addFunc()

void ytParseArgs_addFunc ( ytParseArgs this,
const char *  key,
void(*)()  f 
)

Adds a key that calls the specified function.

Parameters
this
key
f

◆ ytParseArgs_addFunc3()

void ytParseArgs_addFunc3 ( ytParseArgs this,
const char *  key,
void(*)(const char *, const char *, void *)  f,
void *  arg 
)

Adds a key that calls the specified function with its argument.

key is copied.

Parameters
this
key
f

◆ ytParseArgs_addInt()

void ytParseArgs_addInt ( ytParseArgs this,
const char *  key,
int *  arg 
)

Adds a key that takes an int value.

Parameters
this
keykey of this argument.
argpointer to the variable to be set by this argument.

◆ ytParseArgs_addIntArray()

void ytParseArgs_addIntArray ( ytParseArgs this,
const char *  key,
ytIntArray **  arg 
)

Adds a key that sets an integer array to a ytIntArray instance.

If the arg points to NULL, then it automatically allocates a new ytIntArray instance.

Parameters
this
keykey of this argument.
argpointer to the variable to be set by this argument.

◆ ytParseArgs_addKeyValues()

void ytParseArgs_addKeyValues ( ytParseArgs this,
const char *  key,
ytKeyValues **  arg 
)

Adds a key that accepts key=value style values as ytKeyValues.

Parameters
this
key
arg

◆ ytParseArgs_addKeyValuesArray()

void ytParseArgs_addKeyValuesArray ( ytParseArgs this,
const char *  key,
ytArray **  arg 
)

Adds a key that accepts key=value style values as a ytKeyValues instance and adds them to the ytArray instance.

Parameters
this
key
arg

◆ ytParseArgs_addNone()

void ytParseArgs_addNone ( ytParseArgs this,
const char *  key 
)

Adds a key to ignore.

Parameters
this
key

◆ ytParseArgs_addOff()

void ytParseArgs_addOff ( ytParseArgs this,
const char *  key,
int *  arg 
)

Adds a key that sets an int value of 0 if it exists.

Parameters
this
keykey of this argument.
argpointer to the variable to be set by this argument.

◆ ytParseArgs_addOn()

void ytParseArgs_addOn ( ytParseArgs this,
const char *  key,
int *  arg 
)

Adds a key that sets an int value of 1 if it exists.

Parameters
this
keykey of this argument.
argpointer to the variable to be set by this argument.

◆ ytParseArgs_addOnOff()

void ytParseArgs_addOnOff ( ytParseArgs this,
const char *  key,
int *  arg 
)

Adds a key that sets an int value of 0 or 1.

If a value on, 1, true, t, or yes is given, an integer 1 is set to the given variable.

If a value off, 0, false, f, or no is given, an integer 0 is set to the given variable.

Parameters
this
keykey of this argument.
argpointer to the variable to be set by this argument.

◆ ytParseArgs_addStrArray()

void ytParseArgs_addStrArray ( ytParseArgs this,
const char *  key,
ytStrArray **  arg 
)

Adds a key that sets a string array to a ytStrArray instance.

If the arg points to NULL, then it automatically allocates a new ytStrArray instance.

Parameters
this
keykey of this argument.
argpointer to the variable to be set by this argument.

◆ ytParseArgs_addStrArrayAdd()

void ytParseArgs_addStrArrayAdd ( ytParseArgs this,
const char *  key,
ytStrArray **  arg 
)

Adds a key that adds a string to a ytStrArray instance.

If the arg points to NULL, then it automatically allocates a new ytStrArray instance.

Parameters
this
keykey of this argument.
argpointer to the variable to be set by this argument.

◆ ytParseArgs_delete()

void ytParseArgs_delete ( ytParseArgs this)

Deletes the ytParseArgs instance.

This releases all memory managed by this instance.

Parameters
thisytParserArgs to delete.

◆ ytParseArgs_genKV()

ytKeyValues * ytParseArgs_genKV ( int  argc,
char **  argv,
const char *  defKey 
)

Generates ytKeyValues instance from command line arguments.

This is a simple parser for command line arguments. This converts "-x y" or "--x y" in the command line arguments into a key-value pair of "x" and "y".

Parameters
argcthe number of arguments in argv.
argvarray of command line arguments.
Returns
ytKeyValues instance containing keys and their values.

◆ ytParseArgs_set()

int ytParseArgs_set ( ytParseArgs parser,
int  argc,
char **  argv 
)

Parses command line arguments.

Command line arguments are generally tokenized or split into particular arguments by white space characters. However, this parser treats tokens or arguments ending with a comma (,) and/or an equal letter (=) and its subsequent arguments as a single token or argument.

Parameters
parserytParseArgs instance.
argcNumber of arguments in argv, which is passed by main().
argvArray of arguments passed by main().
Returns
0 if succeeded. Otherwise, the index of argv that caused the error.
Go to Top

◆ ytParseArgs_setKV()

int ytParseArgs_setKV ( ytParseArgs parser,
const ytKeyValues kv,
int  opt 
)

Sets values in the variables from the ytKeyValues instance.

Parameters
parser
kv
optytParseArgs_OPT_ERROR, ytParseArgs_OPT_IGNORE, or ytParseArgs_OPT_WARNING.
Returns
-1 if succeeded, otherwise the index of the key-value pair in kv that caused the error.

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