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>
#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_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.
 
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_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 cli through looping.
 

Function Documentation

◆ _cli_create_long()

static 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()

static 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 '-' it the short name will be 2 letters.

Note
TODO: make it generate short arguments for option names longer than 2 words.
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 short cli arg.

◆ 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_find_long()

static 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.
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()

static 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.

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
Precondition
arena, argc, argv, name, and desc must be valid and not null.
Returns
A new htils_cli_t struct.

◆ cli_usage()

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.

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

◆ parse_cli()

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

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