Skip to content

Commit

Permalink
print usage and exit with error on invalid arguments
Browse files Browse the repository at this point in the history
Add also some testing to make sure help, long help and handling
of invalid arguments works as expected.

Based on pull request #19 originally by Laurent Arnoud (@spk).
  • Loading branch information
fabled committed Jun 5, 2019
1 parent c3a93a4 commit 366d0ee
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/apk.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,11 +588,11 @@ int main(int argc, char **argv)
help_requested = 1;
break;
}
if (r != -ENOTSUP) goto err_and_usage;
if (r != -ENOTSUP) goto err;
}
}

if (help_requested) {
if (help_requested || r == -ENOTSUP) {
r = usage(applet);
goto err;
}
Expand Down Expand Up @@ -686,12 +686,9 @@ int main(int argc, char **argv)
r = 0;
#endif

err_and_usage:
if (r == -EINVAL)
r = usage(applet);
err:
if (r == -ESHUTDOWN)
r = 0;
err:
if (ctx)
free(ctx);

Expand Down
26 changes: 26 additions & 0 deletions test/command-parsing.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

fail=0

help_output=$(../src/apk version --help 2>/dev/null)
invalid_option_output="$(../src/apk --invalid-option version 2>/dev/null)"
if [ "$help_output" != "$invalid_option_output" ]; then
echo "FAIL: invalid option does not trigger help"
fail=$((fail+1))
fi

if ! ../src/apk --help 2>/dev/null | grep -q "^Use apk --help --verbose for a full command listing."; then
echo "FAIL: brief help gives long help"
fail=$((fail+1))
fi

if ../src/apk --help --verbose 2>/dev/null | grep -q "^Use apk --help --verbose for a full command listing."; then
echo "FAIL: long help does not work"
fail=$((fail+1))
fi

if [ $fail -eq 0 ]; then
echo "OK: command parsing works"
fi

exit $fail

0 comments on commit 366d0ee

Please sign in to comment.