CBuild wiki
Parser for command-line flags with GNU-style format.
License: GPL-3.0-or-later.
-- to list of long options (for
custom parsing semantics).-)
but only when terminator reached.Some of these functions takes pointers. They expect this pointers
either to be statically allocated or be dynamically allocated and then
leaked. This memory will live trough full app lifetime, so this leakage
has no problems. But if you use valgrind you should probably not use
reachable in --errors-for-leak-kinds (because
flag context live as global variable).
This parses uses something that roughly mimics “GNU” style of flags.
This means that -- precedes long versions of flags and
- short versions. Also, should versions can be grouped
together and each character will then be parsed as a separate flag.
= is supported for both long and short flag versions so
things like --flag=argument or -f=argument can
be used. Even things like -abc=argument will be correctly
parsed to 3 flags - a, b and c,
with last one having 1 argument - argument. Support for
-- is implemented and it stops searching for flags.
Positional arguments are supported too.
struct cbuild_flag_new_opts_t {
char short_option;
union {
uint8_t __flags;
struct {
uint8_t optional : 1;
uint8_t repeat : 1;
uint8_t : 6;
};
};
int8_t num_arguments; // No one need more than 127 arguments (at least then fixed count becomes meaningless).
// uint8_t __padding[1];
const char* group;
const char* terminator;
const char* desc;
const char* argument_desc;
};Options for a new flag
char short_option Character for short options.
If '\0' (0) then disabled.uint8_t:1
optional Arguments to this flag are
optional. This augments num_arguments field by allowing
another option of passing no arguments (in addition to one provided by
num_arguments).uint8_t:1
repeat If ‘false’ repeated flag
invocation result in error. If ‘true’ each invocation of flag just
append argument to shared list (if flag has argument limit it is treated
as a per-invocation limit).int8_t num_arguments Number of arguments
required. -1 means >0.const char*
group Name of arguments group. Used
only for help message. Can be NULL.const char*
terminator Terminator argument. Can
be NULL (unset, non-terminated
argument list).const char*
desc Description. Can be NULL, then nothing will be printed.const char*
argument_desc Description for
argumentt. Can be NULL, then default
‘ARGUMENT’ will be used. Used only if num_arguments != 0.Register new flag. Semi-internal function.
Register a new flag.
const char*
option Long flag name. Will be used as
its internal “ID’....cbuild_flag_new_opts_t
… Fields of configuration structure in
initializer-list form.enum cbuild_flag_options_t {
CBUILD_FLAG_PASS_SEPARATOR,
CBUILD_FLAG_HELP_HOOK,
CBUILD_FLAG_VERSION_HOOK,
};Options for a flag parser
-- inside of pargs. No arguments.--help. Argument - cbuild_flag_print_func_t--version. Argument - cbuild_flag_print_func_tFunction used for print-type hooks. Takes argv[0]
as input.
Set some option for a flag parser.
Options can take some flags, they are passed via variadic arguments.
List of arguments
Get element from argument list using its index.
const cbuild_arglist_t*
arglist Argument list.size_t idx Element index.char** Pointer to an
element or NULL on overflow.
Parse flags.
This function prints error and aborts on invalid flags and when
parsed argument count differs from required (uses
exit(1)).
Print help for all flags via __CBUILD_PRINT
Get list of positional arguments.
cbuild_arglist_t* List
of positional arguments (pointer to a global state).
Get arguments for a specific flag.
cbuild_arglist_t* List
of arguments for that flag (pointer to a global state) or NULL if flag
was not provided.
Get current app name (argv[0]).