htils 1
A small set of utilities for C programming.
Loading...
Searching...
No Matches
cli.h File Reference
#include <htils/arena.h>
#include <htils/basictypes.h>
#include <htils/darray.h>
#include <htils/string.h>

Go to the source code of this file.

Data Structures

struct  cli_option
 A cli option. More...
struct  cli
 The cli state thats used during parsing. More...

Typedefs

typedef struct cli_option htils_cli_option_t
 A cli option.
typedef struct cli htils_cli_t
 The cli state thats used during parsing.

Functions

htils_cli_tcli_new (arena_t *arena, i64 argc, cstr **argv, const string *name, const string *desc)
 Create a new cli.
void cli_add (htils_cli_t *cli, const string *name, const string *desc, b32 requires_arg)
 Add an option to the cli.
i16 parse_cli (htils_cli_t *cli)
 Parse the next CLI argument.

Typedef Documentation

◆ htils_cli_option_t

A cli option.

Parameters
nameThe name of the option
descThe description of the option
cli_shortThe short cli arg of the option
cli_longThe long cli arg of the option
cli_short_shortThe short short cli arg of the option
has_argWhether the option has another argument

◆ htils_cli_t

typedef struct cli htils_cli_t

The cli state thats used during parsing.

Parameters
argcThe number of arguments
argvThe arguments
cli_nameThe name of the cli
cli_descThe description of the cli
optionsThe options of the cli
arenaThe arena to use for allocating memory in the cli parser.
usageThe usage function to use when printing the usage of the cli.
optidxThe index of the current option being parsed.
next_charThe next character to parse.
optargThe argument of the current option being parsed.
optoptThe option of the current option being parsed.

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

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