|
htils 1
A small set of utilities for C programming.
|
#include <stdio.h>#include <string.h>#include <htils/arena.h>#include <htils/cli.h>#include <htils/darray.h>Functions | |
| static void | cli_usage (htils_cli_t *cli, cstr **argv) |
| Print the usage of the cli, automatically generated from the options in the cli. | |
| static string * | cli_create_short (arena_t *arena, const string *name) |
| Generate a short cli arg for an option. | |
| static string * | cli_create_long (arena_t *arena, const string *name) |
| Generate a long cli arg for an option. | |
| static htils_cli_option_t * | cli_find_short (htils_cli_t *cli, u8 c) |
| Find a short cli arg for an option. | |
| static htils_cli_option_t * | cli_find_long (htils_cli_t *cli, const cstr *name, const u64 name_len) |
| Find a long cli arg for an option. | |
| void | cli_add (htils_cli_t *cli, const string *name, const string *desc, b32 requires_arg) |
| Add an option to the cli. | |
| htils_cli_t * | cli_new (arena_t *arena, i64 argc, cstr **argv, const string *name, const string *desc) |
| Create a new cli. | |
| i16 | parse_cli (htils_cli_t *cli) |
| Parse the next CLI argument. | |
| void cli_add | ( | htils_cli_t * | cli, |
| const string * | name, | ||
| const string * | desc, | ||
| b32 | requires_arg ) |
Add an option to the cli.
This will generate short and long option based on the name, and provides a description for the option in the usage command, allowing marking a option as having a required arg.
| cli | The cli to add the option to. |
| name | The name of the option. |
| desc | The description of the option. |
| requires_arg | Whether the option requires another argument afterwards. |
cli, name, and desc must be valid and not null. Generate a long cli arg for an option.
This will generate a long cli arg for an option, based on the name, by simply prefacing the name with '–'.
| arena | The arena to use for allocating memory in the cli parser. |
| name | The name of the option. |
arena and name must be valid and not null.Generate a short cli arg for an option.
This will generate a short cli arg for an option, based on the name, by simply getting the first letter of the name, if the name contains '_' or '-' the short name will be 2 letters.
arena and name must be valid and not null.| arena | The arena to use for allocating memory in the cli parser. |
| name | The name of the option. |
|
static |
Find a long cli arg for an option.
| cli | The cli to find the long cli arg for. |
| name | The name of the option to find the long cli arg for. |
| name_len | The length of the name. |
cli and name must be valid and not null.
|
static |
Find a short cli arg for an option.
| cli | The cli to find the short cli arg for. |
| c | The character to find the short cli arg for. |
cli must be valid and not null.c must be a valid character.| htils_cli_t * cli_new | ( | arena_t * | arena, |
| i64 | argc, | ||
| cstr ** | argv, | ||
| const string * | name, | ||
| const string * | desc ) |
Create a new cli.
Allocates a new htils_cli_t struct and initializes default values.
arena, argv, name, and desc must be valid and not null.argc must be greater than 1.| arena | The arena to use for allocating memory in the cli parser. |
| argc | The number of arguments |
| argv | The arguments |
| name | The name of the cli |
| desc | The description of the cli |
|
static |
Print the usage of the cli, automatically generated from the options in the cli.
This will print the name, description, and usage of the cli, each option being provided.
cli and argv must be valid and not null.| cli | The cli to print the usage of. |
| argv | The argv of the cli, for simply referencing argv[0]. |
| i16 parse_cli | ( | htils_cli_t * | cli | ) |
Parse the next CLI argument.
Intended to be called in a while loop with a switch on the return. Returns the matched argument's code, or one of the error codes: '?' for an unknown argument, ':' when a required argument was missing, and ';' when an argument unexpectedly needs one.
cli must be valid and not null.| cli | The cli to parse. |