comparison cmd.c @ 14:a01899a6e4bb

Extend help command to show usage information for commands
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Mon, 07 Aug 2017 18:16:47 +0200
parents cf81eb0c2d5a
children 3380c8fd9776
comparison
equal deleted inserted replaced
13:cf81eb0c2d5a 14:a01899a6e4bb
167 cmd_creategroup }, 167 cmd_creategroup },
168 { "rg", "removegroup", "removegroup name", "Delete empty group", 168 { "rg", "removegroup", "removegroup name", "Delete empty group",
169 cmd_removegroup }, 169 cmd_removegroup },
170 { "ch", "changepassword", "changepassword", "Change password", 170 { "ch", "changepassword", "changepassword", "Change password",
171 cmd_changepassword }, 171 cmd_changepassword },
172 { "h", "help", "help", "Show help text", cmd_help }, 172 { "h", "help", "help [command]", "Show help text", cmd_help },
173 { "w", "write", "write", "Write the database", cmd_write }, 173 { "w", "write", "write", "Write the database", cmd_write },
174 { "q", "quit", "quit", "Quit", cmd_quit }, 174 { "q", "quit", "quit", "Quit", cmd_quit },
175 { "Q", "Quit", "Quit", "Quit without checking", cmd_quit }, 175 { "Q", "Quit", "Quit", "Quit without checking", cmd_quit },
176 { 0 } 176 { 0 }
177 }; 177 };
880 static enum cmd_return 880 static enum cmd_return
881 cmd_help(struct pwm_ctx *ctx, int argc, char *argv[]) 881 cmd_help(struct pwm_ctx *ctx, int argc, char *argv[])
882 { 882 {
883 struct cmd *cmd; 883 struct cmd *cmd;
884 884
885 if (argc != 1) { 885 if (argc > 2) {
886 return (CMD_USAGE); 886 return (CMD_USAGE);
887 } 887 }
888 888
889 printf("Commands:\n"); 889 if (argc == 2) {
890 for (cmd = cmds; cmd->cmd_func != NULL; cmd++) { 890 for (cmd = cmds; cmd->cmd_func != NULL; cmd++) {
891 printf("%-2s %-16s %s\n", cmd->abbrev_cmd, cmd->full_cmd, 891 if ((strcmp(argv[1], cmd->abbrev_cmd) == 0) ||
892 cmd->description); 892 (strcmp(argv[1], cmd->full_cmd) == 0)) {
893 printf("%s\n", cmd->usage);
894 break;
895 }
896 }
897 } else {
898 printf("Commands:\n");
899 for (cmd = cmds; cmd->cmd_func != NULL; cmd++) {
900 printf("%-2s %-16s %s\n", cmd->abbrev_cmd,
901 cmd->full_cmd, cmd->description);
902 }
893 } 903 }
894 904
895 return (CMD_OK); 905 return (CMD_OK);
896 } 906 }
897 907