Command line interface library

nRF5 SDK v13.0.0

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:

  • Command completion support with the Tab key
  • Built-in list , clear , and history commands
  • Viewing the recently executed commands using the Up/Down keys
  • Support for ANSI escape codes for cursor control and color printing (VT100)

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

  • USB CDC ACM
  • UART
  • RTT

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);
while ( true )
{
}
}

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

  • Short option format: "-x", where x is a letter.
  • Long option format: "--xyz", where xyz is a word (without whitespaces).

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

  • Syntax long
  • Syntax short
  • Following arguments
  • Help string

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" ,
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 .