|
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>#include <htils/string.h>Functions | |
| 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. | |
| void | cli_usage (htils_cli_t *cli, cstr **argv) |
| Print the usage of the cli, auttomatically generated from the options in 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 cli through looping. | |
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 '-' it the short name will be 2 letters.
| 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.| 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.
|
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. |
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 | 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 |
arena, argc, argv, name, and desc must be valid and not null.| void cli_usage | ( | htils_cli_t * | cli, |
| cstr ** | argv | ||
| ) |
Print the usage of the cli, auttomatically generated from the options in the cli.
This will print the name, description, and usage of the cli, each option being provided.
| cli | The cli to print the usage of. |
| argv | The argv of the cli, for simply referencing argv[0]. |
cli and argv must be valid and not null. | i16 parse_cli | ( | htils_cli_t * | cli | ) |
Parse the cli through looping.
This will loop through the arguments and parse the cli meant to be used in a while loop, with a switch for the return. If an unknown arg is met it will return '?', if an argument requires another argument, and it wasn't provided, it will return with ':', and if an argument without a required argument suddenly requires one, then it will return with ';'.
| cli | The cli to parse. |
cli must be valid and not null.