Browse Source
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>pull/31544/head
6 changed files with 124 additions and 1 deletions
@ -0,0 +1,27 @@
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Nordic Semiconductor ASA |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
|
||||
#ifndef SHELL_GETOPT_H__ |
||||
#define SHELL_GETOPT_H__ |
||||
|
||||
#include <getopt.h> |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
|
||||
/* Initializing shell getopt module.
|
||||
* |
||||
* @param[in] shell Pointer to the shell instance. |
||||
*/ |
||||
void z_shell_getopt_init(struct getopt_state *state); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* SHELL_GETOPT_H__ */ |
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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; |
||||
} |
Loading…
Reference in new issue