nRF5 SDK v13.0.0
Command line interface library

This module allows to create and handle a simple command line interface (CLI) with a user-defined command set. You can use it in examples where more than button/LED user interaction is required. This module can be considered a simplified Unix-like command line interface with these features:

The module can be connected to any transport. At this point, the following transport layers are implemented:

Use the NRF_CLI_DEF macro to create an instance of the CLI. The following code shows a simple use case of this library:

static const nrf_cli_cmd_t m_cmd_set[] = {
NRF_CLI_CMD("cmd0", "command 0", nrf_cli_cmd0),
NRF_CLI_CMD("cmd1", "command 1", nrf_cli_cmd1),
};
NRF_CLI_DEF(m_cli, "nrf_cli:~$ ", CLI_TRANPORT, m_cmd_set);
int main(void)
{
ret = nrf_cli_init(&m_cli);
ret_code_t ret = nrf_cli_start(&m_cli);
while (true)
{
nrf_cli_process(&m_cli);
}
}

Every user-defined command can have short options and long options:

Every option can be followed by an argument. Macro NRF_CLI_OPT defines the options:

Parsing of options works similarly to the getopt_long library:

The following code shows how command options should be processed:

void nrf_cli_cmd0(nrf_cli_t const * p_cli, size_t argc, char **argv)
{
/*Show help of command.*/
bool verbose = false;
static const nrf_cli_getopt_option_t opt[] = {
"help",
'h',
"show command help"
),
"verbose",
'v',
"show all command help strings"
)
};
nrf_cli_getopt_ctx_init(argc - 1, argv + 1, ARRAY_SIZE(opt), opt, &opt_ctx);
do
{
int32_t c = nrf_cli_getopt(&opt_ctx);
if (c < 0)
{
break;
}
switch (c)
{
case 'v':
verbose = true;
break;
case 'h':
nrf_cli_opt_help_show(p_cli, opt, ARRAY_SIZE(opt));
return;
case '?':
"list: unknown option: %s\r\n",
nrf_cli_optarg_get(&opt_ctx));
break;
default:
break;
}
} while (1);
//Rest of command processing
//...
}

For API documentation of this library, refer to Command Line Interface.

For a usage example, refer to Command Line Interface (CLI) Example.


Documentation feedback | Developer Zone | Subscribe | Updated