]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
script: support for long options
authorSami Kerola <kerolasa@iki.fi>
Sun, 20 Feb 2011 12:21:15 +0000 (13:21 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 21 Feb 2011 15:47:00 +0000 (16:47 +0100)
[kzak@redhat.com: - use -V instead of -v
  - minor changes in usage()]

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/script.c

index 5e4cc978b2a1ea42d49e816262fec14fde6a7fa9..788c4c4f4a0f99d5d3aefe2babbd942e9c9fa54b 100644 (file)
@@ -119,6 +119,27 @@ die_if_link(char *fn) {
                        fn, program_invocation_short_name, fn);
 }
 
+static void __attribute__((__noreturn__))
+usage(FILE *out)
+{
+       fprintf(out, _(
+               "\nUsage:\n"
+               " %s [options] [file]\n"), program_invocation_short_name);
+
+       fprintf(out, _(
+               "\nOptions:\n"
+               " -a, --append            append output\n"
+               " -c, --command COMMAND   run command rather than interactive shell\n"
+               " -r, --return            return exit code of the child process\n"
+               " -f, --flush             run flush after each write\n"
+               " -q, --quiet             be quiet\n"
+               " -t, --timing            output timing data to stderr\n"
+               " -V, --version           output version information and exit\n"
+               " -h, --help              display this help and exit\n\n"));
+
+       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+}
+
 /*
  * script -t prints time delays as floating point numbers
  * The example program (scriptreplay) that we provide to handle this
@@ -135,20 +156,24 @@ main(int argc, char **argv) {
        extern int optind;
        int ch;
 
+       struct option longopts[] = {
+               { "append",     no_argument,       0, 'a' },
+               { "command",    required_argument, 0, 'c' },
+               { "return",     no_argument,       0, 'e' },
+               { "flush",      no_argument,       0, 'f' },
+               { "quiet",      no_argument,       0, 'q' },
+               { "timing",     no_argument,       0, 't' },
+               { "version",    no_argument,       0, 'V' },
+               { "help",       no_argument,       0, 'h' },
+               { NULL,         0, 0, 0 }
+       };
+
        setlocale(LC_ALL, "");
        setlocale(LC_NUMERIC, "C");     /* see comment above */
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
 
-       if (argc == 2) {
-               if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) {
-                       printf(_("%s (%s)\n"),
-                              program_invocation_short_name, PACKAGE_STRING);
-                       return 0;
-               }
-       }
-
-       while ((ch = getopt(argc, argv, "ac:efqt")) != -1)
+       while ((ch = getopt_long(argc, argv, "ac:efqtVh", longopts, NULL)) != -1)
                switch((char)ch) {
                case 'a':
                        aflg++;
@@ -168,11 +193,17 @@ main(int argc, char **argv) {
                case 't':
                        tflg++;
                        break;
+               case 'V':
+                       printf(_("%s from %s\n"), program_invocation_short_name,
+                                                 PACKAGE_STRING);
+                       exit(EXIT_SUCCESS);
+                       break;
+               case 'h':
+                       usage(stdout);
+                       break;
                case '?':
                default:
-                       fprintf(stderr,
-                               _("usage: script [-a] [-e] [-f] [-q] [-t] [file]\n"));
-                       exit(1);
+                       usage(stderr);
                }
        argc -= optind;
        argv += optind;