This module allows to create and handle a 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 Unix-like command line interface with these features:
clear
, colors
, history
, and resize
commands.The module can be connected to any transport. At this point, the following transport layers are implemented:
For API documentation of this library, refer to Command Line Interface.
Use the NRF_CLI_DEF macro to create an instance of the CLI. The following code shows a simple use case of this library:
Users may use the Tab key to complete a command/subcommand or to see the available subcommands for the currently entered command level. For example, when the cursor is positioned at the beginning of the command line and the Tab key is pressed, the user will see all root (level 0) commands:
These commands are registered by various modules:
clear
, colors
, history
, and resize
are built-in commands which have been registered by nrf_cli.c
,counter
has been registered in example code above by main.c
,log
has been registered by nrf_log_frontend.c
.Then, if a user types a counter
command and presses the Tab key, the console will only print the subcommands registered for this command:
CLI commands are organized in a tree structure and grouped into the following types:
Use the following macros for adding CLI commands:
Each command or subcommand on each level can either have or not have a handler. The CLI executes the handler that is found deepest in the command tree and further subcommands (without a handler) are passed as arguments. Chars within parentheses are treated as one argument. Each command or subcommand without a handler can be treated as an argument, or it can still have a subcommand with a handler and there is no problem for the user to execute it.
clear
- Clears the screen.colors
- Toggles colored syntax. This might be useful in case of Bluetooth CLI to limit the amount of transferred bytes.history
- Shows the recently entered commands. By default, the CLI has 8 memory blocks of 32 bytes each reserved for history. A command longer than 32 bytes will take more than one block, and history will contain less than 8 commands.resize
- Must be executed when terminal width is different than 80 chars or after each change of terminal width. It ensures proper multiline text display and Left/Right/End/Home buttons handling. It can be also called with a parameter (subcommand): resize default
. When run, the CLI will send terminal width = 80 to the terminal and assume successful delivery.Every user-defined command, subcommand, or option can have its own help description. The help for commands and subcommands can be created with respective macros: NRF_CLI_CMD_REGISTER, NRF_CLI_CMD. In addition, you can define options for commands or subcommands using the macro NRF_CLI_OPT. By default, each and every command or subcommand has these two options implemented: "-h" and "--help".
In order to add help functionality to a command or subcommand, you must implement the help handler nrf_cli_help_print inside of a function.
The following code is an example of how commands can be processed:
It is recommended to use subcommands. Options apply mainly in the case when an argument with '-' or "--" is requested. The main benefit of using subcommands is that they can be prompted or completed with the Tab key. In addition, subcommands can have their own handler, which limits the usage of "if - else if" statements combination with the strcmp function.
A strongly recommended terminal to use with the CLI module is PuTTY. It can be easily configured to send escape codes interpreted by the console. If other terminal is used, configure it according to the following PuTTY settings. See the below figures.
If window width is set to a value different than 80, the user must execute the resize
command to ensure correct text display.
Apply these settings when using UART or USB as the transport layer for the CLI module.
Apply these settings when using RTT as the transport layer for the CLI module.
For usage examples of this library, see: Experimental: ATT_MTU Throughput Example, Experimental: Console over Bluetooth Application, Command Line Interface (CLI) Example, Capacitive Sensor Low-level Example, Flash Write Example, and TWIS Slave and TWI Master Mode Drivers Example.