Introduction
noch/args is a library for parsing the command line arguments and flags.
Functions
Function noch_is_arg_flag
bool noch_is_arg_flag(const char *arg);
Checks if the specified argument is a flag.
Parameters
Name | Description |
arg | The null-terminated argument string. |
Return value
Returns true if the argument is a flag but its not equal to -- (flags end), otherwise returns false.
Function noch_is_arg_long_flag
bool noch_is_arg_long_flag(const char *arg);
Checks if the specified argument is a long flag.
Parameters
Name | Description |
arg | The null-terminated argument string. |
Return value
Returns true if the argument is a long flag (prefixed with --) but its not equal to -- (flags end), otherwise returns false.
Function noch_is_arg_flags_end
bool noch_is_arg_flags_end(const char *arg);
Checks if the specified argument is a flags end.
Parameters
Name | Description |
arg | The null-terminated argument string. |
Return value
Returns true if the argument is equal to -- (flags end), otherwise returns false.
Function noch_flag_str
void noch_flag_str(const char *short_name, const char *long_name, const char *desc, char **var);
Registers a char* flag with the specified name, description and value variable.
Parameters
Name | Description |
short_name | The null-terminated short name of the flag. |
long_name | The null-terminated long name of the flag. |
desc | The null-terminated description of the flag. |
var | A pointer to the variable used for the flag value. The current value of the variable is used as the default value of the flag. |
Function noch_flag_char
void noch_flag_char(const char *short_name, const char *long_name, const char *desc, char *var);
Registers a char flag with the specified name, description and value variable.
Parameters
Name | Description |
short_name | The null-terminated short name of the flag. |
long_name | The null-terminated long name of the flag. |
desc | The null-terminated description of the flag. |
var | A pointer to the variable used for the flag value. The current value of the variable is used as the default value of the flag. |
Function noch_flag_int
void noch_flag_int(const char *short_name, const char *long_name, const char *desc, int *var);
Registers an int flag with the specified name, description and value variable.
Parameters
Name | Description |
short_name | The null-terminated short name of the flag. |
long_name | The null-terminated long name of the flag. |
desc | The null-terminated description of the flag. |
var | A pointer to the variable used for the flag value. The current value of the variable is used as the default value of the flag. |
Function noch_flag_size
void noch_flag_size(const char *short_name, const char *long_name, const char *desc, size_t *var);
Registers a size_t flag with the specified name, description and value variable.
Parameters
Name | Description |
short_name | The null-terminated short name of the flag. |
long_name | The null-terminated long name of the flag. |
desc | The null-terminated description of the flag. |
var | A pointer to the variable used for the flag value. The current value of the variable is used as the default value of the flag. |
Function noch_flag_num
void noch_flag_num(const char *short_name, const char *long_name, const char *desc, double *var);
Registers a double flag with the specified name, description and value variable.
Parameters
Name | Description |
short_name | The null-terminated short name of the flag. |
long_name | The null-terminated long name of the flag. |
desc | The null-terminated description of the flag. |
var | A pointer to the variable used for the flag value. The current value of the variable is used as the default value of the flag. |
Function noch_flag_bool
void noch_flag_bool(const char *short_name, const char *long_name, const char *desc, bool *var);
Registers a bool flag with the specified name, description and value variable.
Parameters
Name | Description |
short_name | The null-terminated short name of the flag. |
long_name | The null-terminated long name of the flag. |
desc | The null-terminated description of the flag. |
var | A pointer to the variable used for the flag value. The current value of the variable is used as the default value of the flag. |
Function noch_args_err_to_str
const char *noch_args_err_to_str(noch_args_err_t err);
Gives a string description of the error.
Parameters
Name | Description |
err | The error value. |
Return value
Returns the null-terminated string description.
Function noch_args_new
noch_args_t noch_args_new(int argc, const char **argv);
Creates a new noch_args_t struct from the passed arguments.
Parameters
Name | Description |
argc | The arguments count. Assigned to .c. |
argv | The arguments list. Assigned to .v and .base. |
Return value
Returns the created noch_args_t.
Function noch_args_shift
const char *noch_args_shift(noch_args_t *args);
Extracts the first argument and shifts the arguments by incrementing .v and decrementing .c.
Parameters
Name | Description |
args | Pointer to the arguments to shift. |
Return value
Returns the extracted null-terminated argument.
Function noch_args_parse_flags
noch_args_err_t noch_args_parse_flags(noch_args_t *args, size_t *where, size_t *end, noch_args_t *stripped);
Parses the flags in the passed arguments and saves their values.
Parameters
Name | Description |
args | Pointer to the arguments to parse. |
where | Pointer to the variable where the error position will be saved. Ignored if it is NULL. |
end | Pointer to the variable where the position of the last flag (or of the flags end) will be saved. Ignored if it is NULL. |
stripped | Pointer to where the non-flag arguments will be allocated with NOCH_ALLOC. Ignored if it is NULL. |
Return value
If an error happened, the error value is returned. Otherwise, NOCH_ARGS_OK is returned.
Function noch_fprint_flags_usage
void noch_fprint_flags_usage(FILE *file);
Prints the short names, long names, descriptions and default values of registered flags. Descriptions are alligned and offset by 4 spaces. Default values dont get printed if the flag is of type bool and its default value is false, or if the flag is of type char* and its default value is NULL.
Parameters
Name | Description |
file | The file stream to output into. |
Function noch_fprint_usage
void noch_fprint_usage(FILE *file, const char *name, const char **usages, size_t usages_count, const char *desc, bool print_flags);
Prints program usage.
Parameters
Name | Description |
file | The file stream to output into. |
name | The app name. |
usages | List of the apps usages. Not printed if NULL. |
usages_count | Length of the usages list. |
desc | Description of the app. Not printed if NULL. |
print_flags | Registered flags printing toggle. |
Types
Type noch_args_err_t
Error values returned by noch_args_parse_flags.
Values
Value | Description |
NOCH_ARGS_OK | No error (Success). |
NOCH_ARGS_UNKNOWN_FLAG | An unknown unregistered flag was encountered. |
NOCH_ARGS_MISSING | Flag value is missing. |
NOCH_ARGS_EXTRA | Extra arguments after or in between flags. |
NOCH_ARGS_OUT_OF_MEM | Out of memory. |
NOCH_ARGS_EXPECTED_STR | Expected string value for char* flag. |
NOCH_ARGS_EXPECTED_CHAR | Expected character value for char flag. |
NOCH_ARGS_EXPECTED_SIZE | Expected size value for size_t flag. |
NOCH_ARGS_EXPECTED_NUM | Expected floating point number value for double flag. |
NOCH_ARGS_EXPECTED_BOOL | Expected boolean value for bool flag. |
Type noch_args_t
typedef struct { size_t c; const char **v; char **base; } noch_args_t;
The command line arguments structure. .c is the arguments count, .v is the arguments list and .base is the base of the arguments list (used for freeing when allocated on the heap).
Fields
Name | Description |
c | The arguments count. |
v | The arguments list. |
base | The base (start) of the arguments list. Used for freeing when allocated on the heap. |