Command-line Interface (CLI)#
The libxnvme_cli.h module provides functionality for creating command-line interfaces for xNVMe related applications. Most xNVMe examples, tools, and tests use this module to offer a coherent command-line interface, complete with bash completion scripts and man pages.
Usage Example#
Below is a brief example using excerpts from the source code of
the xnvme CLI tool. Notice how the
sub-command (sub_info) is implemented as a function, added to the g_subs
array, integrated into the CLI setup in g_cli, and utilized when running the
CLI with xnvme_cli_run().
Note
Setting the XNVME_CLI_INIT_DEV_OPEN option will automatically open a device handle.
#include <libxnvme.h>
static int
scan_cb(const struct xnvme_ident *ident, void *cb_args)
{
uint32_t *count_ref = cb_args;
if (*count_ref == 0) {
fprintf(stdout, "\n");
}
fprintf(stdout, " - {");
xnvme_ident_yaml(stdout, ident, 0, ", ", 0);
fprintf(stdout, "}\n");
*count_ref = *count_ref + 1;
return 0;
}
static int
sub_library_info(struct xnvme_cli *XNVME_UNUSED(cli))
{
bool idr = cli->args.idr;
int err;
xnvme_cli_pinf("xNVMe DSM");
if (!cli->given[XNVME_CLI_OPT_NSID]) {
nsid = xnvme_dev_get_nsid(cli->args.dev);
}
dsm_range = xnvme_buf_alloc(dev, sizeof(*dsm_range));
if (!dsm_range) {
err = -errno;
xnvme_cli_perr("xnvme_buf_alloc()", err);
{XNVME_CLI_OPT_CDW00, XNVME_CLI_LOPT},
{XNVME_CLI_OPT_CDW01, XNVME_CLI_LOPT},
{XNVME_CLI_OPT_CDW02, XNVME_CLI_LOPT},
{XNVME_CLI_OPT_CDW03, XNVME_CLI_LOPT},
{XNVME_CLI_OPT_CDW04, XNVME_CLI_LOPT},
{XNVME_CLI_OPT_CDW04, XNVME_CLI_LOPT},
{XNVME_CLI_OPT_CDW05, XNVME_CLI_LOPT},
{XNVME_CLI_OPT_CDW06, XNVME_CLI_LOPT},
{XNVME_CLI_OPT_CDW07, XNVME_CLI_LOPT},
{XNVME_CLI_OPT_CDW08, XNVME_CLI_LOPT},
{XNVME_CLI_OPT_CDW09, XNVME_CLI_LOPT},
{XNVME_CLI_OPT_CDW10, XNVME_CLI_LOPT},
{XNVME_CLI_OPT_CDW11, XNVME_CLI_LOPT},
{XNVME_CLI_OPT_CDW12, XNVME_CLI_LOPT},