Skip to content

<util/args.h>

Small command-line argument helpers.

#include <util/args.h>

Structures

ArgSpec

One accepted option; an array of these ends with an entry whose name is NULL.

Field Type Description
name const char * Long name without dashes, e.g. "verbose".
shortname char Short flag, e.g. 'v'; 0 if none.
has_arg int 0 for a flag, 1 if the option takes a value.
help const char * One-line description for ArgsUsage().

Args

Result of ArgsParse().

Field Type Description
argc int Argument count, including the program name.
argv char ** The argument strings.
positionals const char * Arguments that are not options or option values; at most 16 kept.
positional_count int Number of entries in positionals.

Functions

ArgsParse

int ArgsParse(Args *out, int argc, char **argv, const ArgSpec *spec);

Collects the positional arguments, skipping options and their values.

Options are recognised (so their values are not taken as positionals) but not stored; query them with ArgsHas() / ArgsGet(). Unknown long options are ignored.

Parameter Direction Description
out out Filled with argc/argv and the positionals.
argc in Argument count.
argv in Argument strings.
spec in Accepted options, ending with a NULL name.

Returns: 0, or -1 if an option that takes a value is the last argument.

ArgsHas

int ArgsHas(const Args *args, const char *name);

Tells whether an argument string appears on the command line.

Compares whole argv entries, so pass the option as typed, e.g. "--verbose" or "-v".

Parameter Direction Description
args in Parsed arguments.
name in Exact argument string to look for.

Returns: 1 if present, 0 otherwise.

ArgsGet

const char * ArgsGet(const Args *args, const char *name);

Returns the argument that follows name on the command line.

Compares whole argv entries, so pass the option as typed, e.g. "--out".

Parameter Direction Description
args in Parsed arguments.
name in Exact argument string to look for.

Returns: The next argument, or NULL if name is absent or last.

ArgsUsage

void ArgsUsage(const char *progname, const ArgSpec *spec);

Prints a usage line and one line per option to stdout.

Parameter Direction Description
progname in Program name, usually argv[0].
spec in Accepted options, ending with a NULL name.