Skip to content

Commit 99d064e

Browse files
Jakub Rzeszutkonashif
authored andcommitted
shell: sample: add getopt
Modify an example by adding command that is using shell_getopt funtion. Extent sample.yaml with getopt configuration. Signed-off-by: Jakub Rzeszutko <[email protected]>
1 parent 7e46765 commit 99d064e

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CONFIG_PRINTK=y
2+
CONFIG_SHELL=y
3+
CONFIG_SHELL_GETOPT=y
4+
CONFIG_LOG=y
5+
CONFIG_INIT_STACKS=y
6+
CONFIG_THREAD_STACK_INFO=y
7+
CONFIG_KERNEL_SHELL=y
8+
CONFIG_THREAD_MONITOR=y
9+
CONFIG_BOOT_BANNER=n
10+
CONFIG_THREAD_NAME=y
11+
CONFIG_DEVICE_SHELL=y
12+
CONFIG_POSIX_CLOCK=y
13+
CONFIG_DATE_SHELL=y
14+
CONFIG_THREAD_RUNTIME_STATS=y

samples/subsys/shell/shell_module/sample.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ tests:
2020
tags: shell
2121
harness: keyboard
2222
extra_args: CONF_FILE="prj_minimal.conf"
23+
sample.shell.shell_module.getopt:
24+
filter: ( CONFIG_SERIAL and CONFIG_UART_SHELL_ON_DEV_NAME )
25+
tags: shell
26+
harness: keyboard
27+
min_ram: 40
28+
extra_args: CONF_FILE="prj_getopt.conf"
2329
sample.shell.shell_module.minimal_rtt:
2430
filter: CONFIG_HAS_SEGGER_RTT
2531
tags: shell

samples/subsys/shell/shell_module/src/main.c

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
*/
66

77
#include <zephyr.h>
8-
#include <sys/printk.h>
98
#include <shell/shell.h>
109
#include <version.h>
1110
#include <logging/log.h>
1211
#include <stdlib.h>
1312
#include <drivers/uart.h>
1413
#include <usb/usb_device.h>
14+
#include <ctype.h>
1515

1616
LOG_MODULE_REGISTER(app);
1717

@@ -89,6 +89,59 @@ static int cmd_demo_ping(const struct shell *shell, size_t argc, char **argv)
8989
return 0;
9090
}
9191

92+
#if defined CONFIG_SHELL_GETOPT
93+
static int cmd_demo_getopt(const struct shell *shell, size_t argc, char **argv)
94+
{
95+
struct getopt_state *state;
96+
char *cvalue = NULL;
97+
int aflag = 0;
98+
int bflag = 0;
99+
int c;
100+
101+
while ((c = shell_getopt(shell, argc, argv, "abhc:")) != -1) {
102+
state = shell_getopt_state_get(shell);
103+
switch (c) {
104+
case 'a':
105+
aflag = 1;
106+
break;
107+
case 'b':
108+
bflag = 1;
109+
break;
110+
case 'c':
111+
cvalue = state->optarg;
112+
break;
113+
case 'h':
114+
/* When getopt is active shell is not parsing
115+
* command handler to print help message. It must
116+
* be done explicitly.
117+
*/
118+
shell_help(shell);
119+
return SHELL_CMD_HELP_PRINTED;
120+
case '?':
121+
if (state->optopt == 'c') {
122+
shell_print(shell,
123+
"Option -%c requires an argument.",
124+
state->optopt);
125+
} else if (isprint(state->optopt)) {
126+
shell_print(shell,
127+
"Unknown option `-%c'.",
128+
state->optopt);
129+
} else {
130+
shell_print(shell,
131+
"Unknown option character `\\x%x'.",
132+
state->optopt);
133+
}
134+
return 1;
135+
default:
136+
break;
137+
}
138+
}
139+
140+
shell_print(shell, "aflag = %d, bflag = %d", aflag, bflag);
141+
return 0;
142+
}
143+
#endif
144+
92145
static int cmd_demo_params(const struct shell *shell, size_t argc, char **argv)
93146
{
94147
shell_print(shell, "argc = %d", argc);
@@ -139,6 +192,10 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_demo,
139192
SHELL_CMD(hexdump, NULL, "Hexdump params command.", cmd_demo_hexdump),
140193
SHELL_CMD(params, NULL, "Print params command.", cmd_demo_params),
141194
SHELL_CMD(ping, NULL, "Ping command.", cmd_demo_ping),
195+
#if defined CONFIG_SHELL_GETOPT
196+
SHELL_CMD(getopt, NULL, "Cammand using getopt, looking for: \"abhc:\".",
197+
cmd_demo_getopt),
198+
#endif
142199
SHELL_SUBCMD_SET_END /* Array terminated. */
143200
);
144201
SHELL_CMD_REGISTER(demo, &sub_demo, "Demo commands", NULL);

0 commit comments

Comments
 (0)