args.h file
Command-line walking and declarative argument parsing.
Classes
- struct ProgramArgs
- Cursor over argc and argv.
- struct ProgramArg
- One flag, option, or positional argument.
- struct ProgramCmd
- A command and its direct subcommands.
- struct ArgMatch
- Values captured for one argument.
- struct CmdMatches
- Successful parse of one command.
Enums
- enum ArgAction { BD_ARG_SET = 0, BD_ARG_APPEND, BD_ARG_SET_TRUE, BD_ARG_SET_FALSE, BD_ARG_COUNT, BD_ARG_HELP, BD_ARG_VERSION }
- What an argument does when it appears on the command line.
- enum ArgValueKind { BD_VALUE_STRING = 0, BD_VALUE_BOOL, BD_VALUE_I64, BD_VALUE_U64, BD_VALUE_I32, BD_VALUE_U32, BD_VALUE_U16, BD_VALUE_F64 }
- How a stored value is interpreted by the typed getters.
- enum ArgsStatus { BD_ARGS_OK = 0, BD_ARGS_HELP, BD_ARGS_VERSION, BD_ARGS_MISSING, BD_ARGS_UNKNOWN, BD_ARGS_INVALID, BD_ARGS_CONFLICT, BD_ARGS_EXTRA }
- Result of args_command_parse.
Typedefs
- using ArgAction = enum ArgAction
- What an argument does when it appears on the command line.
- using ArgValueKind = enum ArgValueKind
- How a stored value is interpreted by the typed getters.
- using ArgsStatus = enum ArgsStatus
- Result of args_command_parse.
- using ProgramArg = struct ProgramArg
- One flag, option, or positional argument.
- using ProgramCmd = struct ProgramCmd
- A command and its direct subcommands.
- using ArgMatch = struct ArgMatch
- Values captured for one argument.
- using CmdMatches = struct CmdMatches
- Successful parse of one command.
Functions
- void args_init(ProgramArgs* it, int argc, char** argv)
- Initialize argument walker from argc/argv.
- auto args_done(const ProgramArgs* it) -> bool
- Whether all arguments have been consumed.
- auto args_current(const ProgramArgs* it) -> const char*
- Current argument without advancing.
- auto args_take(ProgramArgs* it) -> const char*
- Take the current argument and advance the index.
- auto args_match(ProgramArgs* it, const char* short_opt, const char* long_opt) -> bool
- If current argv matches short or long option, consume it.
- auto args_is_digits(const char* str) -> bool
- True if str is non-empty and consists only of decimal digits.
- auto args_parse_u16(const char* str, uint16_t* out) -> bool
- Parse an unsigned 16-bit decimal value from str into out.
- auto args_colon_count(const char* str) -> int
- Count ':' characters in str.
- auto args_split_host_port(const char* spec, char* host, size_t host_size, uint16_t* port) -> bool
- Split "host" or "host:port".
- auto args_flag(const char* id, char short_name, const char* long_name, const char* help) -> ProgramArg
- Flag that store true when present.
- auto args_option(const char* id, char short_name, const char* long_name, const char* help, ArgValueKind kind) -> ProgramArg
- Option that stores one value of the given kind.
- auto args_positional(const char* id, const char* value_name, const char* help, ArgValueKind kind) -> ProgramArg
- Positional argument that stores one value.
- auto args_count(const char* id, char short_name, const char* long_name, const char* help) -> ProgramArg
- Flag that counts occurrences.
- void args_required(ProgramArg* arg, bool required)
- Mark an argument required.
- void args_default(ProgramArg* arg, const char* value)
- Set the default text used when the argument is absent.
- void args_possible(ProgramArg* arg, const char*const* values)
- Restrict the value to a NULL-terminated list.
- void args_conflicts(ProgramArg* arg, const char* id)
- Reject a parse where both this argument and id were given.
- void args_requires(ProgramArg* arg, const char* id)
- Require id to be present whenever this argument is given.
- auto args_command_parse(const ProgramCmd* command, int argc, char** argv, CmdMatches* out, char* err, size_t err_size) -> ArgsStatus
- Parse argv against a command.
- void args_command_write_help(const ProgramCmd* command, FILE* out)
- Write usage, arguments, options, and subcommands.
- void args_matches_free(CmdMatches* matches)
- Release match storage.
- auto args_matches_present(const CmdMatches* matches, const char* id) -> bool
- True when the argument was given or a default value is available.
- auto args_matches_count(const CmdMatches* matches, const char* id) -> size_t
- Occurrence count.
- auto args_matches_get(const CmdMatches* matches, const char* id) -> const char*
- Last stored string, or NULL when this id has no string value.
- auto args_matches_values(const CmdMatches* matches, const char* id, size_t* count) -> const char*const*
- All stored strings for an APPEND or SET argument.
- auto args_matches_get_bool(const CmdMatches* matches, const char* id, bool* out) -> bool
- Read a boolean argument.
- auto args_matches_get_i64(const CmdMatches* matches, const char* id, int64_t* out) -> bool
- Parse the last stored string as a signed 64-bit integer.
- auto args_matches_get_u64(const CmdMatches* matches, const char* id, uint64_t* out) -> bool
- Read an unsigned 64-bit argument.
- auto args_matches_get_i32(const CmdMatches* matches, const char* id, int32_t* out) -> bool
- Parse the last stored string as a signed 32-bit integer.
- auto args_matches_get_u32(const CmdMatches* matches, const char* id, uint32_t* out) -> bool
- Parse the last stored string as an unsigned 32-bit integer.
- auto args_matches_get_u16(const CmdMatches* matches, const char* id, uint16_t* out) -> bool
- Parse the last stored string as an unsigned 16-bit integer.
- auto args_matches_get_f64(const CmdMatches* matches, const char* id, double* out) -> bool
- Parse the last stored string as a floating-point value.
- auto args_matches_subcommand(const CmdMatches* matches) -> const ProgramCmd*
- Selected subcommand, or NULL.
- auto args_matches_child(const CmdMatches* matches) -> const CmdMatches*
- Parse of the selected subcommand, or NULL.
Defines
- #define BD_ARG_REQUIRED
- Designated initializer that marks the option required.
- #define BD_ARG_OPTIONAL
- Designated initializer that marks the option optional.
- #define BD_ARG_DEFAULT(value)
- Designated initializer that sets the default text.
- #define BD_ARG_FLAG(arg_id, arg_short, arg_long, arg_help)
- Compound literal for a flag that stores true when present.
- #define BD_ARG_COUNT(arg_id, arg_short, arg_long, arg_help)
- Compound literal for a flag that counts occurrences.
- #define BD_ARG_POSITIONAL(arg_id, arg_value, arg_help, arg_kind, arg_required)
- Compound literal for a positional argument.
- #define BD_ARG_OPTION(arg_id, arg_short, arg_long, arg_help, arg_kind, arg_extra)
- Compound literal for an option that stores one value.
- #define BD_COMMAND(cmd_name, cmd_about, cmd_version, cmd_args)
- Compound literal for a command.
- #define BD_COMMAND_WITH_SUBS(cmd_name, cmd_about, cmd_version, cmd_args, cmd_subs)
- Compound literal for a command and its direct subcommands.
Enum documentation
enum ArgAction
What an argument does when it appears on the command line.
SET stores one value and a later occurrence replaces it. APPEND stores every occurrence. SET_TRUE and SET_FALSE are flags. COUNT counts occurrences, including clustered shorts such as -vvv. HELP and VERSION stop parsing.
Typedef documentation
typedef enum ArgAction ArgAction
What an argument does when it appears on the command line.
SET stores one value and a later occurrence replaces it. APPEND stores every occurrence. SET_TRUE and SET_FALSE are flags. COUNT counts occurrences, including clustered shorts such as -vvv. HELP and VERSION stop parsing.
typedef struct ProgramArg ProgramArg
One flag, option, or positional argument.
The schema is not allocated. Value pointers passed in must outlive parsing. possible is a NULL-terminated list. conflicts and requires name another argument id. short_name 0 and a NULL long_name omit that form.
typedef struct ProgramCmd ProgramCmd
A command and its direct subcommands.
subs points at an array of the same struct, so nesting is recursive. –help is recognized automatically. –version is recognized when version is not NULL, unless the command already defines those names.
typedef struct CmdMatches CmdMatches
Successful parse of one command.
String values point at argv or at default literals. child owns the parse of the selected subcommand. Release the whole tree with args_matches_free.
Function documentation
void args_init(ProgramArgs* it, int argc, char** argv)
Initialize argument walker from argc/argv.
| Parameters | |
|---|---|
| it | Walker state (no-op if NULL). |
| argc | Argument count. |
| argv | Argument vector; argv[0] becomes program name when present. |
bool args_done(const ProgramArgs* it)
Whether all arguments have been consumed.
| Parameters | |
|---|---|
| it | Argument walker. |
| Returns | true if it is NULL/invalid or index is past argc; false otherwise. |
const char* args_current(const ProgramArgs* it)
Current argument without advancing.
| Parameters | |
|---|---|
| it | Argument walker. |
| Returns | argv[index], or NULL when done. |
const char* args_take(ProgramArgs* it)
Take the current argument and advance the index.
| Parameters | |
|---|---|
| it | Argument walker; advanced when an argument is taken. |
| Returns | The taken argument, or NULL when done. |
bool args_match(ProgramArgs* it, const char* short_opt, const char* long_opt)
If current argv matches short or long option, consume it.
| Parameters | |
|---|---|
| it | Argument walker; advanced on match. |
| short_opt | Optional short form (e.g. "-h"); may be NULL. |
| long_opt | Optional long form (e.g. "--help"); may be NULL. |
| Returns | true if matched and consumed; false otherwise. |
bool args_is_digits(const char* str)
True if str is non-empty and consists only of decimal digits.
| Parameters | |
|---|---|
| str | String to test; may be NULL. |
| Returns | true if str is non-empty and digits-only; false otherwise. |
bool args_parse_u16(const char* str, uint16_t* out)
Parse an unsigned 16-bit decimal value from str into out.
| Parameters | |
|---|---|
| str | Digit string to parse; may be NULL. |
| out | Receives the parsed value on success; must not be NULL. |
| Returns | true on success; false on NULL, empty, non-digits, overflow, or NULL out. |
int args_colon_count(const char* str)
Count ':' characters in str.
| Parameters | |
|---|---|
| str | String to scan; may be NULL. |
| Returns | Number of ':' characters, or 0 if str is NULL. |
bool args_split_host_port(const char* spec, char* host, size_t host_size, uint16_t* port)
Split "host" or "host:port".
| Parameters | |
|---|---|
| spec | Input "host" or "host:port". |
| host | Output buffer for host (always NUL-terminated on success). |
| host_size | Capacity of host. |
| port | Optional; set when a valid port is present. |
| Returns | true on success; false on invalid input or if the host does not fit. |
Several colons are the whole host; a single trailing ":digits" with non-zero port is parsed. Rejects empty/leading-dash specs and hosts that do not fit in host_size (including NUL).
ProgramArg args_flag(const char* id, char short_name, const char* long_name, const char* help)
Flag that store true when present.
| Parameters | |
|---|---|
| id | Argument id used by the match getters. |
| short_name | Short option character. 0 omits the short form. |
| long_name | Long option name, without leading dashes. NULL omits it. |
| help | Help text. May be NULL. |
| Returns | A ProgramArg with action BD_ and kind BD_. |
ProgramArg args_option(const char* id, char short_name, const char* long_name, const char* help, ArgValueKind kind)
Option that stores one value of the given kind.
| Parameters | |
|---|---|
| id | Argument id used by the match getters. |
| short_name | Short option character. 0 omits the short form. |
| long_name | Long option name, without leading dashes. NULL omits it. |
| help | Help text. May be NULL. |
| kind | How the stored text is parsed by the typed getters. |
| Returns | A ProgramArg with action BD_. |
ProgramArg args_positional(const char* id, const char* value_name, const char* help, ArgValueKind kind)
Positional argument that stores one value.
| Parameters | |
|---|---|
| id | Argument id used by the match getters. |
| value_name | Name shown in the usage line. NULL falls back to id. |
| help | Help text. May be NULL. |
| kind | How the stored text is parsed by the typed getters. |
| Returns | A positional ProgramArg with action BD_. |
required stays false. Call args_ to change it.
ProgramArg args_count(const char* id, char short_name, const char* long_name, const char* help)
Flag that counts occurrences.
| Parameters | |
|---|---|
| id | Argument id used by the match getters. |
| short_name | Short option character. 0 omits the short form. |
| long_name | Long option name, without leading dashes. NULL omits it. |
| help | Help text. May be NULL. |
| Returns | A ProgramArg with action BD_ and kind BD_. |
void args_required(ProgramArg* arg, bool required)
Mark an argument required.
| Parameters | |
|---|---|
| arg | Argument to update. NULL is ignored. |
| required | true when the parse must see this argument. |
NULL is ignored.
void args_default(ProgramArg* arg, const char* value)
Set the default text used when the argument is absent.
| Parameters | |
|---|---|
| arg | Argument to update. NULL is ignored. |
| value | Default text. Not copied. NULL clears the default. |
void args_possible(ProgramArg* arg, const char*const* values)
Restrict the value to a NULL-terminated list.
| Parameters | |
|---|---|
| arg | Argument to update. NULL is ignored. |
| values | NULL-terminated list of allowed text. Not copied. NULL clears it. |
The list is not copied.
void args_conflicts(ProgramArg* arg, const char* id)
Reject a parse where both this argument and id were given.
| Parameters | |
|---|---|
| arg | Argument to update. NULL is ignored. |
| id | Identifier of the argument that conflicts with arg. Not copied. |
void args_requires(ProgramArg* arg, const char* id)
Require id to be present whenever this argument is given.
| Parameters | |
|---|---|
| arg | Argument to update. NULL is ignored. |
| id | Identifier that must also be present. Not copied. |
ArgsStatus args_command_parse(const ProgramCmd* command, int argc, char** argv, CmdMatches* out, char* err, size_t err_size)
Parse argv against a command.
| Parameters | |
|---|---|
| command | Command to parse. NULL is invalid. |
| argc | Argument count, including the program name. |
| argv | Argument vector. NULL is invalid when argc is positive. |
| out | Receives the matches on success. NULL discards a successful parse. |
| err | Optional buffer for a short error message. |
| err_size | Capacity of err, including the terminating NUL. |
| Returns | BD_ on success. Any other status leaves out zeroed. |
argv[0] is the program name and is not an argument. On BD_ARGS_OK, out owns match storage. On every other status, out is zeroed and nothing is owned. err receives a short message when non-NULL and err_size is non-zero.
void args_command_write_help(const ProgramCmd* command, FILE* out)
Write usage, arguments, options, and subcommands.
| Parameters | |
|---|---|
| command | Command to describe. NULL is ignored. |
| out | Stream to write. NULL is ignored. |
void args_matches_free(CmdMatches* matches)
Release match storage.
| Parameters | |
|---|---|
| matches | Matches to release, including the child parse. NULL is ignored. |
NULL is ignored.
bool args_matches_present(const CmdMatches* matches, const char* id)
True when the argument was given or a default value is available.
| Parameters | |
|---|---|
| matches | Parse to query. NULL is not present. |
| id | Argument id. |
| Returns | true when the argument was given or a stored value is available. |
size_t args_matches_count(const CmdMatches* matches, const char* id)
Occurrence count.
| Parameters | |
|---|---|
| matches | Parse to query. NULL counts as zero. |
| id | Argument id. |
| Returns | How many times a COUNT argument appeared, or how many values another argument stored. 0 when id is absent. |
COUNT arguments return how many times they appeared.
const char* args_matches_get(const CmdMatches* matches, const char* id)
Last stored string, or NULL when this id has no string value.
| Parameters | |
|---|---|
| matches | Parse to query. |
| id | Argument id. |
| Returns | Last stored string, or NULL when this id has no string value. |
const char*const* args_matches_values(const CmdMatches* matches, const char* id, size_t* count)
All stored strings for an APPEND or SET argument.
| Parameters | |
|---|---|
| matches | Parse to query. |
| id | Argument id. |
| count | Receives the number of strings. May be NULL. |
| Returns | The stored strings, or NULL when this id has none. count is 0 then. |
count may be NULL.
bool args_matches_get_bool(const CmdMatches* matches, const char* id, bool* out)
Read a boolean argument.
| Parameters | |
|---|---|
| matches | Parse to query. |
| id | Argument id. |
| out | Receives the value. Must not be NULL. |
| Returns | true on success. false when id is absent, out is NULL, or the text does not parse. |
BD_ stores whether the flag was present. BD_ stores the opposite. Any other action parses the last stored string with bd_.
bool args_matches_get_i64(const CmdMatches* matches, const char* id, int64_t* out)
Parse the last stored string as a signed 64-bit integer.
| Parameters | |
|---|---|
| matches | Parse to query. |
| id | Argument id. |
| out | Receives the value. Must not be NULL. |
| Returns | true on success. false when there is no string, out is NULL, or the text does not parse. |
bool args_matches_get_u64(const CmdMatches* matches, const char* id, uint64_t* out)
Read an unsigned 64-bit argument.
| Parameters | |
|---|---|
| matches | Parse to query. |
| id | Argument id. |
| out | Receives the value. Must not be NULL. |
| Returns | true on success. false when id is absent, out is NULL, or the text does not parse. |
A COUNT argument stores how many times it appeared. Any other action parses the last stored string with bd_.
bool args_matches_get_i32(const CmdMatches* matches, const char* id, int32_t* out)
Parse the last stored string as a signed 32-bit integer.
| Parameters | |
|---|---|
| matches | Parse to query. |
| id | Argument id. |
| out | Receives the value. Must not be NULL. |
| Returns | true on success. false when there is no string, out is NULL, or the text does not parse. |
bool args_matches_get_u32(const CmdMatches* matches, const char* id, uint32_t* out)
Parse the last stored string as an unsigned 32-bit integer.
| Parameters | |
|---|---|
| matches | Parse to query. |
| id | Argument id. |
| out | Receives the value. Must not be NULL. |
| Returns | true on success. false when there is no string, out is NULL, or the text does not parse. |
bool args_matches_get_u16(const CmdMatches* matches, const char* id, uint16_t* out)
Parse the last stored string as an unsigned 16-bit integer.
| Parameters | |
|---|---|
| matches | Parse to query. |
| id | Argument id. |
| out | Receives the value. Must not be NULL. |
| Returns | true on success. false when there is no string, out is NULL, or the text does not parse. |
bool args_matches_get_f64(const CmdMatches* matches, const char* id, double* out)
Parse the last stored string as a floating-point value.
| Parameters | |
|---|---|
| matches | Parse to query. |
| id | Argument id. |
| out | Receives the value. Must not be NULL. |
| Returns | true on success. false when there is no string, out is NULL, or the text does not parse. |
const ProgramCmd* args_matches_subcommand(const CmdMatches* matches)
Selected subcommand, or NULL.
| Parameters | |
|---|---|
| matches | Parse to query. NULL has no subcommand. |
| Returns | Selected subcommand, or NULL. |
const CmdMatches* args_matches_child(const CmdMatches* matches)
Parse of the selected subcommand, or NULL.
| Parameters | |
|---|---|
| matches | Parse to query. NULL has no child. |
| Returns | Parse of the selected subcommand, or NULL. |
Define documentation
#define BD_COMMAND(cmd_name, cmd_about, cmd_version, cmd_args)
Compound literal for a command.
| Parameters | |
|---|---|
| cmd_name | Command name. |
| cmd_about | One-line description. May be NULL. |
| cmd_version | Version string. NULL hides automatic --version. |
| cmd_args | Array of ProgramArg. |
cmd_args must be an array. The macro counts it with sizeof.
#define BD_COMMAND_WITH_SUBS(cmd_name, cmd_about, cmd_version, cmd_args, cmd_subs)
Compound literal for a command and its direct subcommands.
| Parameters | |
|---|---|
| cmd_name | Command name. |
| cmd_about | One-line description. May be NULL. |
| cmd_version | Version string. NULL hides automatic --version. |
| cmd_args | Array of ProgramArg. |
| cmd_subs | Array of ProgramCmd. |
cmd_args and cmd_subs must be arrays. The macro counts each with sizeof.