htils 1
A small set of utilities for C programming.
Loading...
Searching...
No Matches
cli.c File Reference
#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 stringcli_create_short (arena_t *arena, const string *name)
 Generate a short cli arg for an option.
static stringcli_create_long (arena_t *arena, const string *name)
 Generate a long cli arg for an option.
static htils_cli_option_tcli_find_short (htils_cli_t *cli, u8 c)
 Find a short cli arg for an option.
static htils_cli_option_tcli_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_tcli_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.

Function Documentation

◆ cli_add()

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.

Parameters
cliThe cli to add the option to.
nameThe name of the option.
descThe description of the option.
requires_argWhether the option requires another argument afterwards.
Precondition
cli, name, and desc must be valid and not null.

◆ cli_create_long()

string * cli_create_long ( arena_t * arena,
const string * name )
static

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 '–'.

Parameters
arenaThe arena to use for allocating memory in the cli parser.
nameThe name of the option.
Precondition
arena and name must be valid and not null.
Returns
A new string containing the long cli arg.

◆ cli_create_short()

string * cli_create_short ( arena_t * arena,
const string * name )
static

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.

Note
TODO: make it generate short arguments for option names longer than 2 words.
Precondition
arena and name must be valid and not null.
Parameters
arenaThe arena to use for allocating memory in the cli parser.
nameThe name of the option.
Returns
A new string containing the short cli arg.

◆ cli_find_long()

htils_cli_option_t * cli_find_long ( htils_cli_t * cli,
const cstr * name,
const u64 name_len )
static

Find a long cli arg for an option.

Parameters
cliThe cli to find the long cli arg for.
nameThe name of the option to find the long cli arg for.
name_lenThe length of the name.
Precondition
cli and name must be valid and not null.
Returns
The htils_cli_option_t that contains the long cli arg, or null.

◆ cli_find_short()

htils_cli_option_t * cli_find_short ( htils_cli_t * cli,
u8 c )
static

Find a short cli arg for an option.

Parameters
cliThe cli to find the short cli arg for.
cThe character to find the short cli arg for.
Precondition
  • cli must be valid and not null.
  • c must be a valid character.
Returns
The htils_cli_option_t that contains the short cli arg, or null.

◆ cli_new()

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.

Precondition
  • arena, argv, name, and desc must be valid and not null.
  • argc must be greater than 1.
Parameters
arenaThe arena to use for allocating memory in the cli parser.
argcThe number of arguments
argvThe arguments
nameThe name of the cli
descThe description of the cli
Returns
A new htils_cli_t struct.

◆ cli_usage()

void cli_usage ( htils_cli_t * cli,
cstr ** argv )
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.

Note
TODO: add footer customization.
Precondition
cli and argv must be valid and not null.
Parameters
cliThe cli to print the usage of.
argvThe argv of the cli, for simply referencing argv[0].

◆ parse_cli()

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.

Precondition
cli must be valid and not null.
Parameters
cliThe cli to parse.
Returns
The matched argument, '?', ':', or ';'.