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

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.

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.

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