1. Home
  2. Docs
  3. Code Overview
  4. User-defined CLI Commands

User-defined CLI Commands

User-defined commands are pre-defined CLI commands that you create for your own applications. It’s like defining a button callback with your own code inside.

When you create a user command, you give it a name (which must be unique and not in conflict with any other modules and BOS commands). User commands can also accept parameters (arguments) on the command line, which lets you pass through parameters to external functions that your user command might invoke.

To create a new user command, follow these steps, please note that all your definition and steps take place in main.c

1. Near the top of main.c file, define-command function prototype.

/* Private variables ---------------------------------------------------------*/
static portBASE_TYPE UserCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );

In our example, we refer to our command by UserCommand which you should give it a unique name.

2. Add the command structure in order to define the command text and parameter number:

/* CLI command structure : user-command */
const CLI_Command_Definition_t UserCommandDefinition =
	( const int8_t * ) "user-command", /* The command string to type. */
	( const int8_t * ) "user-command:\r\n Execute a user command\r\n\r\n",
	UserCommand, /* The function to run. */
	0 /* No parameters are expected. */

The first string is the command string you use in the CLI. The second string is the help string that shows up for this command when you type help. If your command accepts parameters, you should change the number of parameters from 0 to the required number of them. Use -1 if the expected number of parameters is not fixed.

3. Define command function (after UserTask). This function will be executed when the command is called.

static portBASE_TYPE UserCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString )
  strcpy( ( char * ) pcWriteBuffer, "This is my command!\n");

 /* There is no more data to return after this single string, so return pdFALSE. */

  return pdFALSE;

4. Define RegisterUserCLICommands() function and register your user commands

/* --- Register user CLI Commands */

void RegisterUserCLICommands(void)
  FreeRTOS_CLIRegisterCommand(&UserCommandDefinition );

After you finish your definition and compile your code, you may see your command in the CLI by typing help. You should see your command at the bottom of the help list as shown below.


How can we help?