This example demonstrates the use of command parameters in a command-line interface application. Parameters are essential for executing various functions within a command. For instance, this example includes a command that can set either a high or low value based on the parameter provided. This flexibility allows the same command to perform different actions depending on the input. Additionally, parameters can be used to print specific information. For example, by passing a particular parameter, the command can output detailed status reports or configuration settings.

Example Code:

#include "CLI_command.h"

#define CONVERT_ASCII_DEC(ascii) (ascii - 48)  /* Macro to convert ASCII numbers to Decimal */

static const uint32_t Pin[] = { GPIO_PIN_0, GPIO_PIN_1, GPIO_PIN_2, GPIO_PIN_3, GPIO_PIN_4, GPIO_PIN_5, GPIO_PIN_6, GPIO_PIN_7 }; /* Variable that stores each pin information */

/* command definitions */
static const CLI_Command_Definition_t GPIO_Write = {
    "write",                                                                        /* Command string */
    "write: Set a value to a GPIO pin\r\nExpect 2 parameters, (1-7) (S/R)\r\n",     /* Details of command */
    WritePins,                                                                      /* Function to execute with command */
    2                                                                               /* 2 parameters are expected */
};

static const CLI_Command_Definition_t GPIO_Read = {
    "read",                                                                         /* Command string */
    "read: Read the value of a GPIO pin\r\nExpect 1 parameters, ( 1 - 7)\r\n",      /* Details of command */
    ReadPins,                                                                       /* Function to execute with command */
    1                                                                               /* 1 parameters are expected */
};

/* Command Functions */
BaseType_t WritePins(char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString)
{
    const char *Parameter1, *Parameter2;        /* Variables to receive the parameters spected */
    BaseType_t lenght_param1, lenght_param2;    /* Variable to store the lenght of the parameters */
    uint8_t DecParameter;                       /* Variable to store the convertion of the Ascii number to Decimal number */

    Parameter1 = FreeRTOS_CLIGetParameter(pcCommandString, 1, &lenght_param1);  /* Function to get parameter 1 */
    DecParameter = CONVERT_ASCII_DEC( *Parameter1 );                            /* Convert the data received to decimal number */
    if( DecParameter <= 7 )                                                     /* Check if the data is correct */
    {
        Parameter2 = FreeRTOS_CLIGetParameter(pcCommandString, 2, &lenght_param2);  /* Get the parameter 2 */
        if( (*Parameter2 == 'S') || (*Parameter2 == 's') )                          /* Check if parameter data match with the 's' letter */
        {
            HAL_GPIO_WritePin( GPIOC, Pin[ DecParameter ], SET );                   /* Set a high value in pin */
            strncpy( pcWriteBuffer, "PIN SET", xWriteBufferLen );                   /* Send a message to the Terminal */
        }
        else if( (*Parameter2 == 'R') || (*Parameter2 == 'r') )                     /* Check if parameter match with the 'r' letter */
        {
            HAL_GPIO_WritePin( GPIOC, Pin[ DecParameter ], RESET );                 /* Set a low value in pin */
            strncpy( pcWriteBuffer, "PIN RESET", xWriteBufferLen );                 /* Send a message to the Terminal */
        }
        else                                                                        /* If second parameter no match */
        {
            strncpy( pcWriteBuffer, "Invalid Second Parameter", xWriteBufferLen );  /* Send a message error to the terminal */
        }
    }
    else                                                                        /* If the first parameter is incorrect */
    {
        strncpy( pcWriteBuffer, "Invalid First Parameter", xWriteBufferLen );   /* Send a message error to the terminal */
    }

    return pdFALSE;                                                             /* Finish after execution */
}

BaseType_t ReadPins(char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString)
{
    const char *Parameter1;     /* Variables to receive the parameters spected */
    BaseType_t lenght_param1;   /* Variable to store the lenght of the parameters */
    GPIO_PinState PinState;     /* Variable that receive the pin state info */
    uint8_t DecParameter;       /* Variable to store the convertion of the Ascii number to Decimal number */

    Parameter1 = FreeRTOS_CLIGetParameter(pcCommandString, 1, &lenght_param1);  /* Function to get parameter 1 */
    DecParameter = CONVERT_ASCII_DEC( Parameter1[0] );                          /* Convert the data received to a Decimal number */

    if( DecParameter <= 7 )                                                 /* Check if the pin received is correct */
    {
        PinState = HAL_GPIO_ReadPin( GPIOC, Pin[ DecParameter ] );          /* Read the pin and store the pin state */
        if( PinState == GPIO_PIN_SET )                                      /* Compare if the pin state is high */
        {
            strncpy( pcWriteBuffer, "Pin State High", xWriteBufferLen );    /* Send to terminal the pin state of high */
        }
        else                                                                /* If the pin state is low */
        {
            strncpy( pcWriteBuffer, "Pin State Low", xWriteBufferLen );     /* Send to terminal that the pinstate is low */
        }
    }
    else                                                                    /* If the pin data of parameter is incorrect */
    {
        strncpy( pcWriteBuffer, "Invalid Pin", xWriteBufferLen );           /* Send a message error to terminal */
    }

    return pdFALSE;                                                         /* Finish after execution */
}

/* Register the command */
void vRegisterCLICommands(void)
{
    FreeRTOS_CLIRegisterCommand( &GPIO_Write );     /* Register the command "write" */
    FreeRTOS_CLIRegisterCommand( &GPIO_Read );      /* Register the command "read" */
}

Two commands are configured, write that expects 2 parameters (Pin number) and (Status of pin), and read that expects 1 parameter (Pin number). The 2 commands are designed with different purposes, the first command write to set a value in a specific pin, this command function can implement different actions depending on the parameters provided. The read command is used to print information about a specific pin, this command function just prints information to the user without modifying the application.

CLI application:

Document

To start, the help command is executed, observe that the command brief description includes the expected parameters, is recommended to add the parameters description.
"write" command expects 2 parameters, the first parameter is a value between 1 and 7, which is the number of pins that can be controlled by the app, the second parameter is a letter to set in the pin output a value of Set or Reset.
"read" command expects just 1 command, Parameter is a value between 1-7, as equal to the first parameter of command write is a PIN.

First, the write command is executed, observe that the parameters indicate that the user wants to set a high value on pin 1, when the command finishes, it prints a message indicating that the pin is in the Set state (high state).
After, the same command is written to set a Reset value (low state) in the same pin 1, again a message is printed indicating that now pin 1 is in a low state

This image shows the read command. First, the write command is written to set in high pin 4, then the read command is executed to print the current state of the pin, indicating that the pin state is high.
After, the command "write" is used to set in low pin 4 to execute again the "read" command, observe that now the message indicates that the pin is in a low state.