Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit b933764

Browse files
authored
Merge pull request #407 from HowJMay/cli_arg
fix: Fix error as CLI argument without giving key
2 parents ccd7074 + de3e972 commit b933764

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

accelerator/config.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#define CONFIG_LOGGER "config"
1414

1515
static logger_id_t logger_id;
16+
// FIXME: We need further improvements without depending on temporary variables
17+
static int tmp_argc;
1618

1719
int get_conf_key(char const* const key) {
1820
for (int i = 0; i < cli_cmd_num; ++i) {
@@ -24,6 +26,19 @@ int get_conf_key(char const* const key) {
2426
return 0;
2527
}
2628

29+
static void remove_nonvalue_dash(int argc, char** argv) {
30+
for (int i = 1; i < argc; i++) {
31+
if ((strlen(argv[i]) == 2) && (!strncmp(argv[i], "--", 2))) {
32+
argc--;
33+
if (argc == (i + 1)) {
34+
break;
35+
}
36+
memmove(argv + i, argv + (i + 1), (argc - i) * sizeof(char*));
37+
}
38+
}
39+
tmp_argc = argc;
40+
}
41+
2742
struct option* cli_build_options() {
2843
struct option* long_options = (struct option*)malloc(cli_cmd_num * sizeof(struct option));
2944
for (int i = 0; i < cli_cmd_num; ++i) {
@@ -190,6 +205,9 @@ status_t ta_core_file_init(ta_core_t* const core, int argc, char** argv) {
190205
goto done;
191206
}
192207

208+
// remove `--` from argv
209+
remove_nonvalue_dash(argc, argv);
210+
193211
// Loop through the CLI arguments for first time to find the configuration file path
194212
while ((key = getopt_long(argc, argv, "hv", long_options, NULL)) != -1) {
195213
switch (key) {
@@ -277,7 +295,10 @@ status_t ta_core_cli_init(ta_core_t* const core, int argc, char** argv) {
277295
status_t ret = SC_OK;
278296
struct option* long_options = cli_build_options();
279297

280-
while ((key = getopt_long(argc, argv, "hv", long_options, NULL)) != -1) {
298+
// remove `--` from argv
299+
remove_nonvalue_dash(tmp_argc, argv);
300+
301+
while ((key = getopt_long(tmp_argc, argv, "hv", long_options, NULL)) != -1) {
281302
switch (key) {
282303
case ':':
283304
ret = SC_CONF_MISSING_ARGUMENT;

0 commit comments

Comments
 (0)