|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | #include <zephyr.h> |
8 | | -#include <sys/printk.h> |
9 | 8 | #include <shell/shell.h> |
10 | 9 | #include <version.h> |
11 | 10 | #include <logging/log.h> |
12 | 11 | #include <stdlib.h> |
13 | 12 | #include <drivers/uart.h> |
14 | 13 | #include <usb/usb_device.h> |
| 14 | +#include <ctype.h> |
15 | 15 |
|
16 | 16 | LOG_MODULE_REGISTER(app); |
17 | 17 |
|
@@ -89,6 +89,59 @@ static int cmd_demo_ping(const struct shell *shell, size_t argc, char **argv) |
89 | 89 | return 0; |
90 | 90 | } |
91 | 91 |
|
| 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 | + |
92 | 145 | static int cmd_demo_params(const struct shell *shell, size_t argc, char **argv) |
93 | 146 | { |
94 | 147 | shell_print(shell, "argc = %d", argc); |
@@ -139,6 +192,10 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_demo, |
139 | 192 | SHELL_CMD(hexdump, NULL, "Hexdump params command.", cmd_demo_hexdump), |
140 | 193 | SHELL_CMD(params, NULL, "Print params command.", cmd_demo_params), |
141 | 194 | 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 |
142 | 199 | SHELL_SUBCMD_SET_END /* Array terminated. */ |
143 | 200 | ); |
144 | 201 | SHELL_CMD_REGISTER(demo, &sub_demo, "Demo commands", NULL); |
|
0 commit comments