diff options
author | Sami Kerola | 2011-02-20 13:21:15 +0100 |
---|---|---|
committer | Karel Zak | 2011-02-21 16:47:00 +0100 |
commit | 3ff526391fb5c6b33418dc9cfec31c2ff9b4792e (patch) | |
tree | fd4483cb5218e6ba352100594b5f98cf53543c93 /misc-utils/script.c | |
parent | script: use libc error printing facilities (diff) | |
download | kernel-qcow2-util-linux-3ff526391fb5c6b33418dc9cfec31c2ff9b4792e.tar.gz kernel-qcow2-util-linux-3ff526391fb5c6b33418dc9cfec31c2ff9b4792e.tar.xz kernel-qcow2-util-linux-3ff526391fb5c6b33418dc9cfec31c2ff9b4792e.zip |
script: support for long options
[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>
Diffstat (limited to 'misc-utils/script.c')
-rw-r--r-- | misc-utils/script.c | 55 |
1 files changed, 43 insertions, 12 deletions
diff --git a/misc-utils/script.c b/misc-utils/script.c index 5e4cc978b..788c4c4f4 100644 --- a/misc-utils/script.c +++ b/misc-utils/script.c @@ -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; |