CLI options processor module

Contents

Opts is a CLI options parser, it can parse flags, switches and arguments from command line and offers an easy way to express input errors as well as the ability to display help screen.

Classes

struct zpl_opts_entry
struct zpl_opts_err
struct zpl_opts

Functions

auto zpl_opts_init(zpl_opts* opts, zpl_allocator allocator, char const* app) -> void
Initializes options parser.
auto zpl_opts_free(zpl_opts* opts) -> void
Releases the resources used by options parser.
auto zpl_opts_add(zpl_opts* opts, char const* name, char const* lname, const char* desc, zpl_u8 type) -> void
Registers an option.
auto zpl_opts_positional_add(zpl_opts* opts, char const* name) -> void
Registers option as positional.
auto zpl_opts_compile(zpl_opts* opts, int argc, char** argv) -> zpl_b32
Compiles CLI arguments.
auto zpl_opts_print_help(zpl_opts* opts) -> void
Prints out help screen.
auto zpl_opts_print_errors(zpl_opts* opts) -> void
Prints out parsing errors.
auto zpl_opts_string(zpl_opts* opts, char const* name, char const* fallback) -> zpl_string
Fetches a string from an option.
auto zpl_opts_real(zpl_opts* opts, char const* name, zpl_f64 fallback) -> zpl_f64
Fetches a real number from an option.
auto zpl_opts_integer(zpl_opts* opts, char const* name, zpl_i64 fallback) -> zpl_i64
Fetches an integer number from an option.
auto zpl_opts_has_arg(zpl_opts* opts, char const* name) -> zpl_b32
Checks whether an option was used.
auto zpl_opts_positionals_filled(zpl_opts* opts) -> zpl_b32
Checks whether all positionals have been passed in.

Function documentation

void zpl_opts_init(zpl_opts* opts, zpl_allocator allocator, char const* app)

Initializes options parser.

Parameters
opts Options parser to initialize.
allocator Memory allocator to use. (ex. zpl_heap())
app Application name displayed in help screen.

Initializes CLI options parser using specified memory allocator and provided application name.

void zpl_opts_add(zpl_opts* opts, char const* name, char const* lname, const char* desc, zpl_u8 type)

Registers an option.

Parameters
opts Options parser to add to.
name
lname
desc Description shown in the help screen.
type Option's type (see zpl_opts_types)

Registers an option with its short and long name, specifies option's type and its description.

void zpl_opts_positional_add(zpl_opts* opts, char const* name)

Registers option as positional.

Parameters
opts
name Name of already registered option.

Registers added option as positional, so that we can pass it anonymously. Arguments are expected on the command input in the same order they were registered as.

zpl_b32 zpl_opts_compile(zpl_opts* opts, int argc, char** argv)

Compiles CLI arguments.

Parameters
opts
argc Argument count in an array.
argv Array of arguments.

void zpl_opts_print_help(zpl_opts* opts)

Prints out help screen.

Prints out help screen with example usage of application as well as with all the flags available.

void zpl_opts_print_errors(zpl_opts* opts)

Prints out parsing errors.

Prints out possible errors caused by CLI input.

zpl_string zpl_opts_string(zpl_opts* opts, char const* name, char const* fallback)

Fetches a string from an option.

Parameters
opts
name Name of an option.
fallback Fallback string we return if option wasn't found.

zpl_f64 zpl_opts_real(zpl_opts* opts, char const* name, zpl_f64 fallback)

Fetches a real number from an option.

Parameters
opts
name Name of an option.
fallback Fallback real number we return if option was not found.

zpl_i64 zpl_opts_integer(zpl_opts* opts, char const* name, zpl_i64 fallback)

Fetches an integer number from an option.

Parameters
opts
name Name of an option.
fallback Fallback integer number we return if option was not found.

zpl_b32 zpl_opts_has_arg(zpl_opts* opts, char const* name)

Checks whether an option was used.

Parameters
opts
name Name of an option.