This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

cli

Hi,

I am using cli and made my own command like bellow, as you see i have a command with 3 sub commands. But i think my idea is primitive because if i have 1000 sub command so i have to write many functions!!!!

Another problem of using this method is that in putty first i have to write N1 as a sub command and in the next line SEND as a command and then i can see my results. For example :

uart_cli:~$   Do A

uart_cli:~$   DO

But i want  to type like  "SEND N1'' which is contain command and sub command and then get my results. Kindly let me how to do that and let me know if it is possible, how many sub command i can type with that? is there any limitation? For example can i have something like bellow?

uart_cli:~$  DO A L g H Z K

which A, L,H,Z,K are sub commands of DO.

//sub commands
static void cmd_DO_A(nrf_cli_t const * p_cli, size_t argc, char **argv)
      { 
           A = 1;    
      }

static void cmd_DO_B(nrf_cli_t const * p_cli, size_t argc, char **argv)
      { 
          A = 2;
      }

static void cmd_DO_C(nrf_cli_t const * p_cli, size_t argc, char **argv)
      { 
            A = 3;
      }
// Command
static void cmd_DO(nrf_cli_t const * p_cli, size_t argc, char **argv)
{ 
      ASSERT(p_cli);
      //
      //
      //
      //

      switch(A)
      {
        case 1:
                
                break;
         case 2:
             
                break;
         case 3:
                
                break;

  • What you can do is to implement handler for DO command only which will be checking what subcommands were passed to it using argc and argv.

    To achieve that you need to use dynamic commands concept. It can look more less like that:

    static char m_dynamic_cmd_buffer[10][20] = {"A", "B", "c", "z", "d"};
    
    static void cmd_DO(nrf_cli_t const * p_cli, size_t argc, char **argv)
    {
        for (size_t i = 1; i < argc; i++)
        {
        	if (!strcmp(argv[i], m_dynamic_cmd_buffer[i])
        		{
        		 // do something
        		}
        }
    }
    
    
    static void dynamic_cmd_get(size_t idx, nrf_cli_static_entry_t * p_static)
    {
        ASSERT(p_static);
    
        if (idx < m_dynamic_cmd_cnt)
        {
            /* m_dynamic_cmd_buffer must be sorted alphabetically to ensure correct CLI completion */
            p_static->p_syntax = m_dynamic_cmd_buffer[idx];
            p_static->handler  = NULL;
            p_static->p_subcmd = NULL;
            p_static->p_help = NULl;
        }
        else
        {
            /* if there are no more dynamic commands available p_syntax must be set to NULL */
            p_static->p_syntax = NULL;
        }
    }
    
    NRF_CLI_CREATE_DYNAMIC_CMD(m_sub_dynamic_set, dynamic_cmd_get);
    NRF_CLI_CMD_REGISTER(DO,
                         &m_sub_dynamic_set,
                         "Demonstrate dynamic command usage.",
                         cmd_DO);
    

    I did not compile it, it's only a concept.

    Using this approach you will be able to autocomplete all subcommands.

    CLI is not capable to execute more than 1 handler at a time so you do not have much choice here.

  • Thank you for helpful advises. Could you please let me know about A,B,c,z,d?

    I mean this line:

    static char m_dynamic_cmd_buffer[10][20] = {"A", "B", "c", "z", "d"};

  • This is syntax of your subcommands :). It only means that you can autocomplete commands A, B, c, z, d

    This table could look like that

    static char m_dynamic_cmd_buffer[][20] = {"A_subcommand", "B_subcommand", "c_subcommand"};

    Next you can type in the CLI:

    DO A <TAB> and CLI will autocompleate it to:

    DO A_subcommand

    Next you can tab other (or the same) command.

    DO A_subcommand c <tab> will result in:

    DO A_subcommand c_subcommand

  • Hi

    Thank you for answering and explanation:). But putting char inside of array by this way is impossible and i will face issue with the conflicting types for 'm_dynamic_cmd_buffer'. Any other way?

    Kind regards

  • Hi,

    I do not understand why it is impossible and you will have a conflict types? Please let me know what problems you are facing.

Related