|
Banjo API 0.0.1
C99 game development API
|
Data Structures | |
| struct | bj_cli_argument |
| struct | bj_cli |
Typedefs | |
| typedef bj_bool(* | bj_cli_action_fn) (const struct bj_cli *parser, const struct bj_cli_argument *arg, const char *value, void *dest, struct bj_error **error) |
Functions | |
| bj_bool | bj_cli_parse (struct bj_cli *parser, int argc, char *argv[], struct bj_error **error) |
| bj_bool | bj_cli_validate (const struct bj_cli *parser, struct bj_error **error) |
| void | bj_cli_print_help (const struct bj_cli *parser) |
| size_t | bj_cli_get_help_string (const struct bj_cli *parser, char *buffer, size_t buffer_size) |
| bj_bool | bj_cli_store_cstring (const struct bj_cli *parser, const struct bj_cli_argument *arg, const char *value, void *dest, struct bj_error **error) |
| bj_bool | bj_cli_store_double (const struct bj_cli *parser, const struct bj_cli_argument *arg, const char *value, void *dest, struct bj_error **error) |
| bj_bool | bj_cli_store_int (const struct bj_cli *parser, const struct bj_cli_argument *arg, const char *value, void *dest, struct bj_error **error) |
| bj_bool | bj_cli_store_uint (const struct bj_cli *parser, const struct bj_cli_argument *arg, const char *value, void *dest, struct bj_error **error) |
| bj_bool | bj_cli_store_bool (const struct bj_cli *parser, const struct bj_cli_argument *arg, const char *value, void *dest, struct bj_error **error) |
| bj_bool | bj_cli_print_help_action (const struct bj_cli *parser, const struct bj_cli_argument *arg, const char *value, void *dest, struct bj_error **error) |
Command-line argument parsing utilities.
This module provides a robust, lightweight command-line argument parser supporting short/long options, flags, positional arguments, chained flags, and automatic help generation.
Usage involves defining a bj_cli instance and calling bj_cli_parse.
Each argument is described by a bj_cli_argument, defining parsing and storage behavior.
By default, the parser does not allocate heap memory.
| struct bj_cli_argument |
Descriptor for a single command line argument.
Defines how an argument is parsed and stored by bj_cli_parse.
Most arguments will set .dest and either .name (long option) or .shortname (short option).
Named arguments have .name (e.g., --file) or .shortname (e.g., -f). They can appear anywhere in the command line, multiple times, and are optional unless .required is set.
If the argument expects a value, .action and .dest must be set. The .action callback validates, converts, and stores the value.
Flags are named arguments with no value expected. To create a flag, set .dest but leave .action as 0.
The integer pointed by .dest is set to 1 when the flag is present.
Positional arguments have no .name or .shortname. They are identified by their order relative to other positional arguments.
They can be declared anywhere in the argument list, mixed with named args.
Example:

| Data Fields | ||
|---|---|---|
| bj_cli_action_fn | action |
Callback to parse/store argument value. If 0, .dest is set as int flag. |
| void * | dest |
Pointer to store parsed value. If 0, program exits after argument is found (e.g., --help). |
| const char * | help |
Help message shown in usage output. Can be 0 if no help needed. |
| const char * | metavar |
Placeholder for argument value in help output. Must not be 0 for flag arguments. |
| const char * | name |
Long option name (e.g., "file" for --file). Can be 0 if positional or only shortname used. |
| int | required | Set to 1 if positional argument is mandatory. |
| char | shortname |
Short option name (e.g., 'o' for -o). Can be 0 if positional or only long name used. |
| struct bj_cli |
Parser context and argument list descriptor.
Contains global parser settings, program info, and list of argument descriptors.

| Data Fields | ||
|---|---|---|
| struct bj_cli_argument * | arguments | Pointer to array of argument descriptors. |
| size_t | arguments_len | Number of arguments in the parser. |
| const char * | description | Description text displayed before argument list. |
| const char * | epilog | Text displayed after argument list. |
| const char * | prog | Program name; if 0, argv[0] is used. |
| typedef bj_bool(* bj_cli_action_fn) (const struct bj_cli *parser, const struct bj_cli_argument *arg, const char *value, void *dest, struct bj_error **error) |
Callback function prototype for argument value processing.
Set in each bj_cli_argument to parse, validate and store the argument's string value extracted from argv.
Predefined callbacks include:
| parser | Pointer to the bj_cli instance |
| arg | Pointer to the argument descriptor |
| value | Argument string value from argv |
| dest | Pointer to storage location |
| error | Error output location (may be NULL to ignore errors) |
| size_t bj_cli_get_help_string | ( | const struct bj_cli * | parser, |
| char * | buffer, | ||
| size_t | buffer_size ) |
Get help message as a string for custom output.
Generates formatted help text into the provided buffer.
This function can be called with buffer set to NULL and buffer_size set to 0, in which case it returns the required buffer size without writing anything.
| parser | Pointer to the argument parser configuration. |
| buffer | Buffer to write help text to (may be NULL). |
| buffer_size | Size of buffer in bytes. |
Parse command-line arguments according to parser configuration.
Parses the provided argument vector (argv) according to the argument descriptors in parser, storing results in the destinations specified by each argument.
This function validates the parser configuration, processes all arguments, and reports any errors through the error system.
| parser | Pointer to the argument parser configuration. |
| argc | Argument count (typically from main). |
| argv | Argument vector (typically from main). |
| error | Error output location (may be NULL to ignore errors). |
| void bj_cli_print_help | ( | const struct bj_cli * | parser | ) |
Print help message using Banjo's logging system.
Generates and prints formatted help text showing program description, usage syntax, and all argument descriptions using bj_info().
| parser | Pointer to the argument parser configuration. |
| bj_bool bj_cli_print_help_action | ( | const struct bj_cli * | parser, |
| const struct bj_cli_argument * | arg, | ||
| const char * | value, | ||
| void * | dest, | ||
| struct bj_error ** | error ) |
Print help argument action.
Special action function to trigger printing help and exiting. Use this as the .action for a --help argument.
| parser | Parser instance. |
| arg | Argument descriptor. |
| value | Argument string (usually NULL). |
| dest | Destination (if 0, program exits after printing help). |
| error | Error output location. |
| bj_bool bj_cli_store_bool | ( | const struct bj_cli * | parser, |
| const struct bj_cli_argument * | arg, | ||
| const char * | value, | ||
| void * | dest, | ||
| struct bj_error ** | error ) |
Store boolean argument value.
Accepts true, false, 1, 0, yes, no (case-insensitive).
| parser | Parser instance. |
| arg | Argument descriptor. |
| value | String representation of bool value. |
| dest | Destination pointer (int*). |
| error | Error output location. |
| bj_bool bj_cli_store_cstring | ( | const struct bj_cli * | parser, |
| const struct bj_cli_argument * | arg, | ||
| const char * | value, | ||
| void * | dest, | ||
| struct bj_error ** | error ) |
Store string argument value.
Stores value as a null-terminated string pointer. The pointer is stored directly (no copy is made).
| parser | Parser instance. |
| arg | Argument descriptor. |
| value | C-string value. |
| dest | Destination pointer (const char**). |
| error | Error output location. |
| bj_bool bj_cli_store_double | ( | const struct bj_cli * | parser, |
| const struct bj_cli_argument * | arg, | ||
| const char * | value, | ||
| void * | dest, | ||
| struct bj_error ** | error ) |
Store double argument value.
Parses and stores a double floating point value.
| parser | Parser instance. |
| arg | Argument descriptor. |
| value | String representation of float value. |
| dest | Destination pointer (double*). |
| error | Error output location. |
| bj_bool bj_cli_store_int | ( | const struct bj_cli * | parser, |
| const struct bj_cli_argument * | arg, | ||
| const char * | value, | ||
| void * | dest, | ||
| struct bj_error ** | error ) |
Store int argument value.
Parses and stores a signed integer value.
| parser | Parser instance. |
| arg | Argument descriptor. |
| value | String representation of int value. |
| dest | Destination pointer (int*). |
| error | Error output location. |
| bj_bool bj_cli_store_uint | ( | const struct bj_cli * | parser, |
| const struct bj_cli_argument * | arg, | ||
| const char * | value, | ||
| void * | dest, | ||
| struct bj_error ** | error ) |
Store unsigned int argument value.
Parses and stores an unsigned integer value.
| parser | Parser instance. |
| arg | Argument descriptor. |
| value | String representation of unsigned int value. |
| dest | Destination pointer (unsigned int*). |
| error | Error output location. |
Check for configuration errors in the parser.
Ensures no conflicts or inconsistencies in argument definitions. Reports configuration errors such as:
| parser | Pointer to the argument parser configuration. |
| error | Error output location (may be NULL to ignore errors). |