-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Jim Meyering on 12/29/2008 10:02 AM:
>> $ /bin/[ --help me | head -n1
>> /bin/[: missing `]'
>> $ /bin/[ --help | head -n1
>> Usage: test EXPRESSION
>> $ /bin/[ --hel | head -n1
>> Usage: test EXPRESSION
>>
>> Should the last example also complain about missing `]', rather than printing
>> help text?
> 
> test and [ have always had a conflicted relationship
> with --help/--version.
> 
> Making [ accept no abbreviations does seem like it'd be an improvement.
> 

What do you think of this patch?  It changes programs that take exactly
one of the two mandated options to only recognize exactly "--help" or
"--version" but not abbreviations.  After the patch, I get:

$ src/basename --oops
src/basename: invalid option -- -
Try `src/basename --help' for more information.
$ src/basename --help me
src/basename: invalid option -- -
Try `src/basename --help' for more information.
$ src/basename --hel
src/basename: invalid option -- -
Try `src/basename --help' for more information.
$ src/basename --help | head -n2
Usage: src/basename NAME [SUFFIX]
  or:  src/basename OPTION
$ src/echo --he
- --he

In other words, most programs will treat --h identically to --oops,
diagnosing it as an invalid option (since the application only takes one
of two specific options), and exceptional programs, like echo or [, which
react differently to invalid options, will react the same way to an
abbreviation as to any other invalid option.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             e...@byu.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAklo2EsACgkQ84KuGfSFAYCI7wCgxOjDYYv1iklsomt/HVvMr2dE
nZgAoMw8w66MjdbPSyw9h90CtS8k4goE
=HKir
-----END PGP SIGNATURE-----
>From b8f77e710856f8996cf0f149826691a0f75e71ec Mon Sep 17 00:00:00 2001
From: Eric Blake <e...@byu.net>
Date: Sat, 10 Jan 2009 10:07:46 -0700
Subject: [PATCH] parse_long_options: don't accept abbreviations

* long-options.c (long_options): Ignore abbreviations, so that
/bin/echo --h prints --h rather than usage.
---
 ChangeLog          |    6 ++++++
 lib/long-options.c |   13 +++++++++++--
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0c9bcfb..fad1240 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2009-01-10  Eric Blake  <e...@byu.net>
+
+       parse_long_options: don't accept abbreviations
+       * long-options.c (long_options): Ignore abbreviations, so that
+       /bin/echo --h prints --h rather than usage.
+
 2009-01-09  Paolo Bonzini  <bonz...@gnu.org>
 
        regex: fix glibc bug 9697
diff --git a/lib/long-options.c b/lib/long-options.c
index 0286bdf..b605b68 100644
--- a/lib/long-options.c
+++ b/lib/long-options.c
@@ -1,7 +1,7 @@
 /* Utility to accept --help and --version options as unobtrusively as possible.
 
    Copyright (C) 1993, 1994, 1998, 1999, 2000, 2002, 2003, 2004, 2005,
-   2006 Free Software Foundation, Inc.
+   2006, 2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -33,12 +33,21 @@
 static struct option const long_options[] =
 {
   {"help", no_argument, NULL, 'h'},
+  {"hel", no_argument, NULL, ' '},
+  {"he", no_argument, NULL, ' '},
+  {"h", no_argument, NULL, ' '},
   {"version", no_argument, NULL, 'v'},
+  {"versio", no_argument, NULL, ' '},
+  {"versi", no_argument, NULL, ' '},
+  {"vers", no_argument, NULL, ' '},
+  {"ver", no_argument, NULL, ' '},
+  {"ve", no_argument, NULL, ' '},
+  {"v", no_argument, NULL, ' '},
   {NULL, 0, NULL, 0}
 };
 
 /* Process long options --help and --version, but only if argc == 2.
-   Be careful not to gobble up `--'.  */
+   Be careful not to gobble up `--'.  Ignore abbreviations.  */
 
 void
 parse_long_options (int argc,
-- 
1.6.0.4

_______________________________________________
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils

Reply via email to