piconomix / px-fwlib

open source bare-metal C firmware and documentation for microcontrollers
Other
281 stars 27 forks source link

Feature request: CLI groups that are also commands #1

Closed sslupsky closed 5 years ago

sslupsky commented 5 years ago

Hi, I came across your firmware library for the command line interface. It looks quite interesting and well coded.

One question has come to mind as I have pondered the implementation. Is there a way to interpret a command group as a command ?

Using your led [on | off] example, please let me explain. You have a "group" called "led" and two "commands" which are "on" and "off". What if the "led" group can have a third calling style that is the command "led"? For instance, you type "led" with no arguments and you want the cli to interpret this as a command to display the current state of the led. So,

"led on" - turns the led on "led off" - turns the led off "led" - displays the current state of the led

Is this functionality with the CLI library currently possible?

pieterconradie commented 5 years ago

Hi @sslupsky,

Unfortunately it is not currently possible. The command list item uses the command handler function pointer to decide if it is a command or a group item:

/// Command list item declaration
typedef struct px_cli_cmd_list_item_s
{
    px_cli_handler_t   handler;         ///< Function to be called when command is executed
    union
    {
        const px_cli_cmd_t *   cmd;     ///< pointer to command (if handler != NULL)
        const px_cli_group_t * group;   ///< pointer to group   (if handler == NULL)
    };
} px_cli_cmd_list_item_t;

So if the pointer to the handler function is NULL, it is a group item. This means that a handler function can not be assigned to a group item (in your example the "led" command to display the current state).

I used this trick to make the CLI group / command declaration as compact as possible, because the cmd/group pointer shares the same memory location using the union keyword.

Best regards, Pieter