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(). The xnvme front-end handles its own commands in cli_main() first, and leaves the rest to xnvme_cli_run().

Note

Setting the XNVME_CLI_INIT_DEV_OPEN option will automatically open a device handle.

#include <libxnvme.h>
static int
sub_info(struct xnvme_cli *cli)
{
	struct xnvme_dev *dev = cli->args.dev;
	int err;

	err = xnvme_dev_derive_geo(dev);
	if (err) {
		XNVME_DEBUG("FAILED: xnvme_dev_derive_geo(), err: %d", err);
		return err;
	}

	xnvme_dev_pr(dev, XNVME_PR_DEF);

	return 0;
}

static struct xnvme_cli_sub g_subs[] = {
	{
		"list",
		"List devices on the system",
		"List devices on the system",
		sub_listing,
		{
			{XNVME_CLI_OPT_NON_POSA_TITLE, XNVME_CLI_SKIP},
			{XNVME_CLI_OPT_SYS_URI, XNVME_CLI_LOPT},
			{XNVME_CLI_OPT_FLAGS, XNVME_CLI_LOPT},

			XNVME_CLI_CORE_OPTS,
		},
	},
	{
		"enum",
		"Enumerate devices on the system",
		"Enumerate devices on the system",
		sub_enumerate,
		{
			{XNVME_CLI_OPT_NON_POSA_TITLE, XNVME_CLI_SKIP},
			{XNVME_CLI_OPT_SYS_URI, XNVME_CLI_LOPT},
			{XNVME_CLI_OPT_FLAGS, XNVME_CLI_LOPT},

			XNVME_CLI_CORE_OPTS,
		},
	},
	{
		"scan",
		"Scan for devices without opening them",
		"Scan for devices without opening them",
		sub_scan,
		{
			{XNVME_CLI_OPT_NON_POSA_TITLE, XNVME_CLI_SKIP},
			{XNVME_CLI_OPT_SYS_URI, XNVME_CLI_LOPT},
			{XNVME_CLI_OPT_FLAGS, XNVME_CLI_LOPT},

			XNVME_CLI_CORE_OPTS,
		},
	},
	{
		"info",
		"Retrieve derived information for given device",
		"Retrieve derived information for given device",
		sub_info,
		{
			{XNVME_CLI_OPT_POSA_TITLE, XNVME_CLI_SKIP},
			{XNVME_CLI_OPT_URI, XNVME_CLI_POSA},

			XNVME_CLI_ASYNC_OPTS,
		},
	},
};
static struct xnvme_cli g_cli = {
	.title = "xNVMe - Cross-platform NVMe utility",
	.vcs = XNVME_VCS_TAG,
	.descr_short =
		"Find NVMe devices, inspect and benchmark them, and manage userspace drivers",
	.descr_long = "",
	.subs = g_subs,
	.nsubs = sizeof g_subs / sizeof(*g_subs),
};
int
main(int argc, char **argv)
{
	int err = cli_main(&g_cli, argc, argv);

	if (err >= 0) {
		return err;
	}

	return xnvme_cli_run(&g_cli, argc, argv, XNVME_CLI_INIT_DEV_OPEN);
}