From 8052cd0131a4f483ef14be3e564530c07ea382f5 Mon Sep 17 00:00:00 2001 From: Ruslan Sayfutdinov Date: Sat, 16 Dec 2023 15:28:19 +0000 Subject: [PATCH] cli: better errors on arguent parsing --- programs/zstdcli.c | 48 +++++++++++++---------- tests/cli-tests/basic/args.sh | 10 +++++ tests/cli-tests/basic/args.sh.exit | 1 + tests/cli-tests/basic/args.sh.stderr.glob | 28 +++++++++++++ 4 files changed, 66 insertions(+), 21 deletions(-) create mode 100755 tests/cli-tests/basic/args.sh create mode 100644 tests/cli-tests/basic/args.sh.exit create mode 100644 tests/cli-tests/basic/args.sh.stderr.glob diff --git a/programs/zstdcli.c b/programs/zstdcli.c index 66952aa8293..3f0ae8bdd21 100644 --- a/programs/zstdcli.c +++ b/programs/zstdcli.c @@ -138,8 +138,8 @@ static int exeNameMatch(const char* exeName, const char* test) * Command Line **************************************/ /* print help either in `stderr` or `stdout` depending on originating request - * error (badusage) => stderr - * help (usage_advanced) => stdout + * error (badUsage) => stderr + * help (usageAdvanced) => stdout */ static void usage(FILE* f, const char* programName) { @@ -175,7 +175,7 @@ static void usage(FILE* f, const char* programName) DISPLAY_F(f, "\n"); } -static void usage_advanced(const char* programName) +static void usageAdvanced(const char* programName) { DISPLAYOUT(WELCOME_MESSAGE); DISPLAYOUT("\n"); @@ -316,9 +316,9 @@ static void usage_advanced(const char* programName) } -static void badusage(const char* programName) +static void badUsage(const char* programName, const char* parameter) { - DISPLAYLEVEL(1, "Incorrect parameters \n"); + DISPLAYLEVEL(1, "Incorrect parameter: %s\n", parameter); if (g_displayLevel >= 2) usage(stderr, programName); } @@ -589,7 +589,7 @@ static ZDICT_fastCover_params_t defaultFastCoverParams(void) /** parseAdaptParameters() : - * reads adapt parameters from *stringPtr (e.g. "--zstd=min=1,max=19) and store them into adaptMinPtr and adaptMaxPtr. + * reads adapt parameters from *stringPtr (e.g. "--adapt=min=1,max=19) and store them into adaptMinPtr and adaptMaxPtr. * Both adaptMinPtr and adaptMaxPtr must be already allocated and correctly initialized. * There is no guarantee that any of these values will be updated. * @return 1 means that parsing was successful, @@ -933,6 +933,7 @@ int main(int argCount, const char* argv[]) /* command switches */ for (argNb=1; argNb&2 +zstd --blah +println "+ zstd -xz" >&2 +zstd -xz +println "+ zstd --adapt=min=1,maxx=2 file.txt" >&2 +zstd --adapt=min=1,maxx=2 file.txt +println "+ zstd --train-cover=k=48,d=8,steps32 file.txt" >&2 +zstd --train-cover=k=48,d=8,steps32 file.txt diff --git a/tests/cli-tests/basic/args.sh.exit b/tests/cli-tests/basic/args.sh.exit new file mode 100644 index 00000000000..d00491fd7e5 --- /dev/null +++ b/tests/cli-tests/basic/args.sh.exit @@ -0,0 +1 @@ +1 diff --git a/tests/cli-tests/basic/args.sh.stderr.glob b/tests/cli-tests/basic/args.sh.stderr.glob new file mode 100644 index 00000000000..df275471ca8 --- /dev/null +++ b/tests/cli-tests/basic/args.sh.stderr.glob @@ -0,0 +1,28 @@ ++ zstd --blah +Incorrect parameter: --blah +... +Usage: zstd * + +Options: +... ++ zstd -xz +Incorrect parameter: -x +... +Usage: zstd * + +Options: +... ++ zstd --adapt=min=1,maxx=2 file.txt +Incorrect parameter: --adapt=min=1,maxx=2 +... +Usage: zstd * + +Options: +... ++ zstd --train-cover=k=48,d=8,steps32 file.txt +Incorrect parameter: --train-cover=k=48,d=8,steps32 +... +Usage: zstd * + +Options: +...