Own Shell Commands?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Own Shell Commands?

640 Views
NeraPhil
Contributor I

Hi

 

I am looking for a description on how to implement my own shell commands:

  • the standard shell commands (like Shell_cd etc.) work fine (based on the rtcs shell demo application)
  • I now want to provide my own shell commands

I have added my command to the SHELL_COMMAND_STRUCT. But now:

  • How can I access the shell attributes for my command?
  • How and where can I define the help text for my command?

My MQX version is 3.8

 

Any advice?

Cheers NeraPhil

0 Kudos
2 Replies

358 Views
NeraPhil
Contributor I

OK - that's what I found out so far:

 

  • Example code for my own command (i.e. entry in SHELL_COMMAND_STRUCT is { "debug", my_command })
  • my_command function defined like below
  • Shell_check_help_request returns true if argc=3 && argv[1]== "help" and sets shorthelp to TRUE if argc=3 && argv[1]== "help" && argv[2]=="short" - but this is just a detail

 

int_32 my_command(int_32 argc, char_ptr argv[])
{
   boolean print_usage = FALSE;
   boolean shorthelp = FALSE;
   int_32  return_code = MQX_OK;
   
   print_usage = Shell_check_help_request(argc, argv, &shorthelp);

   if (!print_usage)  {
      int_32 idx = 0;
      for (idx = 0; idx < argc; idx++) {
         printf("argv[%d] = %s\n", idx, argv[idx]);
      }
      // add here implementation of this command
      printf("G3_debug has been called!\n");
   } else if (print_usage) {
      if (shorthelp)  {
         printf("%s <arg1> [<arg2>]\n", argv[0]);
      } else  {
         printf("Usage: %s <arg1> [<arg2>]\n", argv[0]);
      }
   }
   return return_code;
}

 

So I guess I found out myself how to use this function - nonetheless: If somebody could point me to some documentation, that could be of value for me.

 

Cheers

NeraPhil

0 Kudos

358 Views
c0170
Senior Contributor III

hi NeraPhil,

 

Just as a recap, the structure with new command my_command and function to be invoked if this command is typed on the shell.

 

const SHELL_COMMAND_STRUCT Shell_commands[] = {
        {"my_command", my_command_function},
        {"exit", Shell_exit},
        {"help", Shell_help},
        {NULL, NULL}
}

 

Explanation how the shell is used in application:

print_usage purpose is to represent boolean value to print the help or to execute the command. If it's FALSE, then command was typed, otherwise (TRUE) help was requested.

 

If help is typed without any following argument , number of arguments argc = 1 and argv[0] is help command. Value TRUE is stored in shorthelp.

Help with more arguments (help my_command), shorthelp holds FALSE.

 

Check an example which is using shell like HVAC.

 

Regards,

MartinK

0 Kudos