zephyr/subsys/shell/shell_getopt.c
Jakub Rzeszutko 7e46765153 shell: add getopt library support
This functionality is is enabled by setting CONFIG_SHELL_GETOPT.
It is not active by default.

User can call following functions inside command handlers:
 - shell_getopt - getopt function based on freebsd implementation
 - shell_getopt_status_get - returns getopt status

Beware when getopt functionality is enabled shell will not parse
command handler to look for "-h" or "--help" options and print
help message automatically.

Signed-off-by: Jakub Rzeszutko <jakub.rzeszutko@nordicsemi.no>
2021-03-01 09:50:32 -05:00

37 lines
694 B
C

/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <shell/shell.h>
#include <shell/shell_getopt.h>
void z_shell_getopt_init(struct getopt_state *state)
{
getopt_init(state);
}
int shell_getopt(const struct shell *shell, int argc, char *const argv[],
const char *ostr)
{
if (!IS_ENABLED(CONFIG_SHELL_GETOPT)) {
return 0;
}
__ASSERT_NO_MSG(shell);
return getopt(&shell->ctx->getopt_state, argc, argv, ostr);
}
struct getopt_state *shell_getopt_state_get(const struct shell *shell)
{
if (!IS_ENABLED(CONFIG_SHELL_GETOPT)) {
return NULL;
}
__ASSERT_NO_MSG(shell);
return &shell->ctx->getopt_state;
}