args.c file
Argument parser implementation.
Functions
- static auto args_eq(const char* a, const char* b) -> bool
- 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".
- 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.
- static auto arg_blank(void) -> ProgramArg
- 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.
- static void set_err(char* err, const size_t err_size, const char* fmt, ...)
- static auto takes_value(const ProgramArg* arg) -> bool
- static auto user_has_long(const ProgramCmd* command, const char* name) -> bool
- static auto user_has_short(const ProgramCmd* command, const char name) -> bool
- static auto find_long(const ProgramCmd* command, const char* name, const size_t length) -> const ProgramArg*
- static auto find_short(const ProgramCmd* command, const char name) -> const ProgramArg*
- static auto find_sub(const ProgramCmd* command, const char* name) -> const ProgramCmd*
- static auto match_of(const CmdMatches* matches, const char* id) -> const ArgMatch*
- static auto arg_label(const ProgramArg* arg, char* buf, const size_t buf_size) -> const char*
- static auto value_allowed(const ProgramArg* arg, const char* text) -> bool
- static auto value_typed(const ProgramArg* arg, const char* text) -> bool
- static auto push_value(ArgMatch* match, const char* text, const bool replace, const bool explicit) -> bool
- static auto accept_value(ArgMatch* match, const char* text, char* err, const size_t err_size) -> ArgsStatus
- static auto note_presence(ArgMatch* match) -> ArgsStatus
- static auto parse_command(const ProgramCmd* command, int argc, char** argv, int index, CmdMatches* matches, char* err, size_t err_size) -> ArgsStatus
- static auto finish_command(const CmdMatches* matches, char* err, const size_t err_size) -> ArgsStatus
- 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.
- static void upper_copy(const char* text, char* buf, const size_t buf_size)
- static void option_syntax(const ProgramArg* arg, char* buf, const size_t buf_size)
- static void write_row(FILE* out, const char* left, const char* help, const char* extra)
- 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.
Function documentation
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).
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. |
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. |