Add option for "listcmd" to list all registered commands

This commit is contained in:
Tilen Majerle 2021-11-21 18:37:56 +01:00
parent 6f475afcae
commit 270c926c3b
3 changed files with 29 additions and 2 deletions

View File

@ -39,5 +39,6 @@
#include "windows.h"
#define LWSHELL_CFG_USE_OUTPUT 1
#define LWSHELL_CFG_USE_ENABLE_LIST_CMD 1
#endif /* LWSHELL_HDR_OPTS_H */

View File

@ -1,4 +1,4 @@
/**
/**
* \file lwshell_opt.h
* \brief LwSHELL options
*/
@ -97,6 +97,15 @@ extern "C" {
#define LWSHELL_CFG_USE_OUTPUT 1
#endif
/**
* \brief Enables `1` or disables `0` generic ˙listcmd` command to list of registered commands
*
* \ref LWSHELL_CFG_USE_OUTPUT must be enabled to use this feature
*/
#ifndef LWSHELL_CFG_USE_ENABLE_LIST_CMD
#define LWSHELL_CFG_USE_ENABLE_LIST_CMD 0
#endif
/**
* \}
*/

View File

@ -34,6 +34,11 @@
#include <string.h>
#include "lwshell/lwshell.h"
/* Check enabled features */
#if LWSHELL_CFG_USE_ENABLE_LIST_CMD && !LWSHELL_CFG_USE_OUTPUT
#error "To use list command feature, LWSHELL_CFG_USE_OUTPUT must be enabled"
#endif
/* Default characters */
#define LWSHELL_ASCII_NULL 0x00/*!< Null character */
#define LWSHELL_ASCII_BACKSPACE 0x08/*!< Backspace */
@ -176,6 +181,18 @@ prv_parse_input(lwshell_t* lw) {
} else {
c->fn(lw->argc, lw->argv);
}
#if LWSHELL_CFG_USE_ENABLE_LIST_CMD
} else if (strncmp(lw->argv[0], "listcmd", 7) == 0) {
LW_OUTPUT(lw, "List of registered commands\r\n");
for (size_t i = 0; i < cmds_cnt; ++i) {
LW_OUTPUT(lw, cmds[i].name);
LW_OUTPUT(lw, "\t\t\t");
LW_OUTPUT(lw, cmds[i].desc);
LW_OUTPUT(lw, "\r\n");
}
#endif /* LWSHELL_CFG_USE_ENABLE_LIST_CMD */
} else {
LW_OUTPUT(lw, "Unknown command\r\n");
}
}
}