diff options
| author | J08nY | 2017-10-04 19:30:42 +0200 |
|---|---|---|
| committer | J08nY | 2017-10-04 19:30:42 +0200 |
| commit | 12167c2d6b76dbcb224f13e82ef56c00cf3fab01 (patch) | |
| tree | 5b7ba87092e65f4138eecbf4491f7aa61d622516 | |
| parent | 531c26c732ca8d01cd95c88c70bed6038bc8cba8 (diff) | |
| download | ecgen-12167c2d6b76dbcb224f13e82ef56c00cf3fab01.tar.gz ecgen-12167c2d6b76dbcb224f13e82ef56c00cf3fab01.tar.zst ecgen-12167c2d6b76dbcb224f13e82ef56c00cf3fab01.zip | |
| -rw-r--r-- | src/io/cli.c | 17 | ||||
| -rw-r--r-- | test/src/io/test_cli.c | 10 |
2 files changed, 22 insertions, 5 deletions
diff --git a/src/io/cli.c b/src/io/cli.c index 74e6cc7..0a707de 100644 --- a/src/io/cli.c +++ b/src/io/cli.c @@ -73,9 +73,13 @@ struct argp_option cli_options[] = { }; // clang-format on -static unsigned long cli_parse_memory(const char *str) { +static unsigned long cli_parse_memory(const char *str, + struct argp_state *state) { char *suffix = NULL; unsigned long read = strtoul(str, &suffix, 10); + if (suffix == str) { + argp_failure(state, 1, 0, "Wrong memory value."); + } if (suffix) { if (*suffix == 'k' || *suffix == 'K') { read *= 1000; @@ -88,9 +92,12 @@ static unsigned long cli_parse_memory(const char *str) { return read; } -static unsigned long cli_parse_time(const char *str) { +static unsigned long cli_parse_time(const char *str, struct argp_state *state) { char *suffix = NULL; unsigned long read = strtoul(str, &suffix, 10); + if (suffix == str) { + argp_failure(state, 1, 0, "Wrong time value."); + } if (suffix) { if (*suffix == 'm' || *suffix == 'M') { read *= 60; @@ -111,13 +118,13 @@ error_t cli_parse(int key, char *arg, struct argp_state *state) { cfg->datadir = arg; break; case OPT_MEMORY: - cfg->memory = cli_parse_memory(arg); + cfg->memory = cli_parse_memory(arg, state); break; case OPT_TSTACK: - cfg->thread_memory = cli_parse_memory(arg); + cfg->thread_memory = cli_parse_memory(arg, state); break; case OPT_TIMEOUT: - cfg->timeout = cli_parse_time(arg); + cfg->timeout = cli_parse_time(arg, state); break; case OPT_THREADS: if (!strcmp(arg, "auto") || !strcmp(arg, "AUTO")) { diff --git a/test/src/io/test_cli.c b/test/src/io/test_cli.c index e5077ef..448fd0c 100644 --- a/test/src/io/test_cli.c +++ b/test/src/io/test_cli.c @@ -52,4 +52,14 @@ Test(cli, test_auto_threads) { int ret = argp_parse(&test_argp, argc, argv, 0, 0, &cfg); cr_assert_eq(ret, 0, ); cr_assert_eq(cfg.threads, sysconf(_SC_NPROCESSORS_ONLN), ); +} + +Test(cli, test_timeout) { + int argc = 4; + char *argv[] = {"ecgen", "--timeout=10m", "--fp", "1"}; + config_t cfg; + memset(&cfg, 0, sizeof(cfg)); + int ret = argp_parse(&test_argp, argc, argv, 0, 0, &cfg); + cr_assert_eq(ret, 0, ); + cr_assert_eq(cfg.timeout, 600, ); }
\ No newline at end of file |
