shell: Refactor shell argument count validation
Shell arguments structure was stored as a pointer in shell structure and NULL pointer indicated that argument count checking is skipped. It has been reworked to hold the structure (2 bytes) in the shell structure with mandatory=0 skipping the check. This approach is cpp friendly, contrary to the legacy one. Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
parent
138f485c1e
commit
349dd964e0
2 changed files with 8 additions and 19 deletions
|
@ -110,7 +110,7 @@ struct shell_static_entry {
|
||||||
const char *help; /*!< Command help string. */
|
const char *help; /*!< Command help string. */
|
||||||
const struct shell_cmd_entry *subcmd; /*!< Pointer to subcommand. */
|
const struct shell_cmd_entry *subcmd; /*!< Pointer to subcommand. */
|
||||||
shell_cmd_handler handler; /*!< Command handler. */
|
shell_cmd_handler handler; /*!< Command handler. */
|
||||||
const struct shell_static_args *args; /*!< Command arguments. */
|
struct shell_static_args args; /*!< Command arguments. */
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -213,8 +213,7 @@ struct shell_static_entry {
|
||||||
.subcmd = _subcmd, \
|
.subcmd = _subcmd, \
|
||||||
.help = (const char *)_help, \
|
.help = (const char *)_help, \
|
||||||
.handler = _handler, \
|
.handler = _handler, \
|
||||||
.args = _mandatory ? \
|
.args = SHELL_ARG(_mandatory, _optional) \
|
||||||
(&(struct shell_static_args) SHELL_ARG(_mandatory, _optional)) : NULL \
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -535,23 +535,13 @@ static int exec_cmd(const struct shell *shell, size_t argc, char **argv,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shell->ctx->active_cmd.args) {
|
if (shell->ctx->active_cmd.args.mandatory) {
|
||||||
const struct shell_static_args *args;
|
u8_t mand = shell->ctx->active_cmd.args.mandatory;
|
||||||
|
u8_t opt = shell->ctx->active_cmd.args.optional;
|
||||||
|
bool in_range = (argc >= mand) && (argc <= (mand + opt));
|
||||||
|
|
||||||
args = shell->ctx->active_cmd.args;
|
/* Check if argc is within allowed range */
|
||||||
|
ret_val = cmd_precheck(shell, in_range);
|
||||||
if (args->optional > 0) {
|
|
||||||
/* Check if argc is within allowed range */
|
|
||||||
ret_val = cmd_precheck(shell,
|
|
||||||
((argc >= args->mandatory)
|
|
||||||
&&
|
|
||||||
(argc <= args->mandatory +
|
|
||||||
args->optional)));
|
|
||||||
} else {
|
|
||||||
/* Perform exact match if there are no optional args */
|
|
||||||
ret_val = cmd_precheck(shell,
|
|
||||||
(args->mandatory == argc));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ret_val) {
|
if (!ret_val) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue