]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
flock, getopt: write --help to stdout and return 0
authorRuediger Meier <ruediger.meier@ga-group.nl>
Sun, 18 Jun 2017 22:02:44 +0000 (00:02 +0200)
committerRuediger Meier <ruediger.meier@ga-group.nl>
Mon, 26 Jun 2017 12:38:24 +0000 (14:38 +0200)
... and use errtryhelp() instead of usage().

Note in past "getopt --help" returned 2. But it is otherwise
documented and was just a mistake IMO. See the unreachable exit(0)
which was removed here: d1d03b54

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
misc-utils/getopt.c
sys-utils/flock.c
tests/expected/getopt/options-compatible
tests/expected/getopt/options-invalid_getopt_option
tests/expected/getopt/options-invocation_without_parameters
tests/expected/getopt/options-no-arguments

index af4cf386a8bd4a3dfac18ca4d1609d6a56390b5f..87d088ed53ff74eb66b263a7c0370a38061c5e4d 100644 (file)
@@ -243,9 +243,7 @@ static void __attribute__ ((__noreturn__)) parse_error(const char *message)
 {
        if (message)
                warnx("%s", message);
-       fprintf(stderr, _("Try `%s --help' for more information.\n"),
-               program_invocation_short_name);
-       exit(PARAMETER_EXIT_CODE);
+       errtryhelp(PARAMETER_EXIT_CODE);
 }
 
 
@@ -325,33 +323,33 @@ static shell_t shell_type(const char *new_shell)
        parse_error(_("unknown shell after -s or --shell argument"));
 }
 
-static void __attribute__ ((__noreturn__)) print_help(void)
+static void __attribute__((__noreturn__)) usage(void)
 {
-       fputs(USAGE_HEADER, stderr);
-       fprintf(stderr, _(
+       fputs(USAGE_HEADER, stdout);
+       printf(_(
                " %1$s <optstring> <parameters>\n"
                " %1$s [options] [--] <optstring> <parameters>\n"
                " %1$s [options] -o|--options <optstring> [options] [--] <parameters>\n"),
                program_invocation_short_name);
 
-       fputs(USAGE_SEPARATOR, stderr);
-       fputs(_("Parse command options.\n"), stderr);
-
-       fputs(USAGE_OPTIONS, stderr);
-       fputs(_(" -a, --alternative             allow long options starting with single -\n"), stderr);
-       fputs(_(" -l, --longoptions <longopts>  the long options to be recognized\n"), stderr);
-       fputs(_(" -n, --name <progname>         the name under which errors are reported\n"), stderr);
-       fputs(_(" -o, --options <optstring>     the short options to be recognized\n"), stderr);
-       fputs(_(" -q, --quiet                   disable error reporting by getopt(3)\n"), stderr);
-       fputs(_(" -Q, --quiet-output            no normal output\n"), stderr);
-       fputs(_(" -s, --shell <shell>           set quoting conventions to those of <shell>\n"), stderr);
-       fputs(_(" -T, --test                    test for getopt(1) version\n"), stderr);
-       fputs(_(" -u, --unquoted                do not quote the output\n"), stderr);
-       fputs(USAGE_SEPARATOR, stderr);
-       fputs(USAGE_HELP, stderr);
-       fputs(USAGE_VERSION, stderr);
-       fprintf(stderr, USAGE_MAN_TAIL("getopt(1)"));
-       exit(PARAMETER_EXIT_CODE);
+       fputs(USAGE_SEPARATOR, stdout);
+       fputs(_("Parse command options.\n"), stdout);
+
+       fputs(USAGE_OPTIONS, stdout);
+       fputs(_(" -a, --alternative             allow long options starting with single -\n"), stdout);
+       fputs(_(" -l, --longoptions <longopts>  the long options to be recognized\n"), stdout);
+       fputs(_(" -n, --name <progname>         the name under which errors are reported\n"), stdout);
+       fputs(_(" -o, --options <optstring>     the short options to be recognized\n"), stdout);
+       fputs(_(" -q, --quiet                   disable error reporting by getopt(3)\n"), stdout);
+       fputs(_(" -Q, --quiet-output            no normal output\n"), stdout);
+       fputs(_(" -s, --shell <shell>           set quoting conventions to those of <shell>\n"), stdout);
+       fputs(_(" -T, --test                    test for getopt(1) version\n"), stdout);
+       fputs(_(" -u, --unquoted                do not quote the output\n"), stdout);
+       fputs(USAGE_SEPARATOR, stdout);
+       fputs(USAGE_HELP, stdout);
+       fputs(USAGE_VERSION, stdout);
+       printf(USAGE_MAN_TAIL("getopt(1)"));
+       exit(EXIT_SUCCESS);
 }
 
 int main(int argc, char *argv[])
@@ -417,7 +415,7 @@ int main(int argc, char *argv[])
                        getopt_long_fp = getopt_long_only;
                        break;
                case 'h':
-                       print_help();
+                       usage();
                case 'o':
                        free(ctl.optstr);
                        ctl.optstr = xstrdup(optarg);
index 003a126187f9b1e58baa784c5d0474191afa6216..5abe2def5f29942d3e558ba73ab2edda38649f28 100644 (file)
 #include "monotonic.h"
 #include "timer.h"
 
-static void __attribute__((__noreturn__)) usage(int ex)
+static void __attribute__((__noreturn__)) usage(void)
 {
-       fprintf(stderr, USAGE_HEADER);
-       fprintf(stderr,
+       fputs(USAGE_HEADER, stdout);
+       printf(
                _(" %1$s [options] <file>|<directory> <command> [<argument>...]\n"
                  " %1$s [options] <file>|<directory> -c <command>\n"
                  " %1$s [options] <file descriptor number>\n"),
                program_invocation_short_name);
 
-       fputs(USAGE_SEPARATOR, stderr);
-       fputs(_("Manage file locks from shell scripts.\n"), stderr);
-
-       fputs(USAGE_OPTIONS, stderr);
-       fputs(_(  " -s, --shared             get a shared lock\n"), stderr);
-       fputs(_(  " -x, --exclusive          get an exclusive lock (default)\n"), stderr);
-       fputs(_(  " -u, --unlock             remove a lock\n"), stderr);
-       fputs(_(  " -n, --nonblock           fail rather than wait\n"), stderr);
-       fputs(_(  " -w, --timeout <secs>     wait for a limited amount of time\n"), stderr);
-       fputs(_(  " -E, --conflict-exit-code <number>  exit code after conflict or timeout\n"), stderr);
-       fputs(_(  " -o, --close              close file descriptor before running command\n"), stderr);
-       fputs(_(  " -c, --command <command>  run a single command string through the shell\n"), stderr);
-       fputs(_(  " -F, --no-fork            execute command without forking\n"), stderr);
-       fputs(_(  "     --verbose            increase verbosity\n"), stderr);
-       fprintf(stderr, USAGE_SEPARATOR);
-       fprintf(stderr, USAGE_HELP);
-       fprintf(stderr, USAGE_VERSION);
-       fprintf(stderr, USAGE_MAN_TAIL("flock(1)"));
-       exit(ex);
+       fputs(USAGE_SEPARATOR, stdout);
+       fputs(_("Manage file locks from shell scripts.\n"), stdout);
+
+       fputs(USAGE_OPTIONS, stdout);
+       fputs(_(  " -s, --shared             get a shared lock\n"), stdout);
+       fputs(_(  " -x, --exclusive          get an exclusive lock (default)\n"), stdout);
+       fputs(_(  " -u, --unlock             remove a lock\n"), stdout);
+       fputs(_(  " -n, --nonblock           fail rather than wait\n"), stdout);
+       fputs(_(  " -w, --timeout <secs>     wait for a limited amount of time\n"), stdout);
+       fputs(_(  " -E, --conflict-exit-code <number>  exit code after conflict or timeout\n"), stdout);
+       fputs(_(  " -o, --close              close file descriptor before running command\n"), stdout);
+       fputs(_(  " -c, --command <command>  run a single command string through the shell\n"), stdout);
+       fputs(_(  " -F, --no-fork            execute command without forking\n"), stdout);
+       fputs(_(  "     --verbose            increase verbosity\n"), stdout);
+       fputs(USAGE_SEPARATOR, stdout);
+       fputs(USAGE_HELP, stdout);
+       fputs(USAGE_VERSION, stdout);
+       printf(USAGE_MAN_TAIL("flock(1)"));
+       exit(EXIT_SUCCESS);
 }
 
 static sig_atomic_t timeout_expired = 0;
@@ -172,8 +172,10 @@ int main(int argc, char *argv[])
 
        strutils_set_exitcode(EX_USAGE);
 
-       if (argc < 2)
-               usage(EX_USAGE);
+       if (argc < 2) {
+               warnx(_("not enough arguments"));
+               errtryhelp(EX_USAGE);
+       }
 
        memset(&timeout, 0, sizeof timeout);
 
@@ -217,7 +219,7 @@ int main(int argc, char *argv[])
                        printf(UTIL_LINUX_VERSION);
                        exit(EX_OK);
                case 'h':
-                       usage(0);
+                       usage();
                default:
                        errtryhelp(EX_USAGE);
                }
index 585de608ac690b847c46af0ae4c0f8e8453ab421..66e42a7b510d663a8b02af7a0ab2e18d749565a2 100644 (file)
@@ -1,5 +1,5 @@
 getopt: missing optstring argument
-Try `getopt --help' for more information.
+Try 'getopt --help' for more information.
 exit value: 2
  --
 exit value: 0
index 531cc9828e5cc839f7c99ea46976336c40447544..b3577e027ab7f8a5ae633ceb4874b91c3ec7b80e 100644 (file)
@@ -1,3 +1,3 @@
 getopt: invalid option -- 'b'
-Try `getopt --help' for more information.
+Try 'getopt --help' for more information.
 exit value: 2
index 08b3ed5cff2d747499d090ebc1b96bad26ac091c..f1ecc161077ccc96061e2db976b6f9594f764225 100644 (file)
@@ -1,3 +1,3 @@
 getopt: missing optstring argument
-Try `getopt --help' for more information.
+Try 'getopt --help' for more information.
 exit value: 2
index 08b3ed5cff2d747499d090ebc1b96bad26ac091c..f1ecc161077ccc96061e2db976b6f9594f764225 100644 (file)
@@ -1,3 +1,3 @@
 getopt: missing optstring argument
-Try `getopt --help' for more information.
+Try 'getopt --help' for more information.
 exit value: 2