delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2002/05/30/09:59:29

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
From: "Bernard A Badger" <bab AT vx DOT com>
To: "Michael A Chase" <mchase AT ix DOT netcom DOT com>, <cygwin AT cygwin DOT com>
Subject: RE: trivial mkpasswd defect
Date: Thu, 30 May 2002 10:00:24 -0400
Message-ID: <INEKLKBFCDBPKMKAJLMDOEJOCDAA.bab@vx.com>
MIME-Version: 1.0
X-Priority: 3 (Normal)
X-MSMail-Priority: Normal
In-Reply-To: <E17DC0P-0006XD-00@blount.mail.mindspring.net>
Importance: Normal
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4807.1700

> On Wed, 29 May 2002 17:45:35 -0400 Bernard A Badger <bab AT vx DOT com> wrote:
[SNIP] 
> I don't see anything there that says that POSIX endorses '-?' for
> displaying the usage message.  It just shows how to handle invalid option
> characters.  If getopt sees an invalid character after '-' it returns 
> '?' and puts the
> invalid character ("?" in your example) in optopt.  In that example, 
> I'd expect to see
> 
> Unrecognized option -?
> <<usage message>>
> 
> >From 'man bash':
> 
> getopts optstring name [args]
>        getopts is used by shell procedures to parse positional 
> parameters.   optstring  contains  the
>        option  characters  to  be  recognized;  if  a character is 
> followed by a colon, the option is
>        expected to have an argument, which should be separated from 
> it by white space.  The colon and
>        question  mark  characters  may  not  be  used as option 
> characters. . . .
> 
> I don't have the man page for getopt() handy, but I think it contains the
> same restriction.

Oh, I didn't understand what you meant.  You think that '-?' cannot be used
as an option.

Yes, it's true that you cannot have '?' as an legal option character.  
This is because getopt returns the option character that was used, 
and '?' is used as an error indication.  Whenever the user calls the 
command with an error, '?' is returned instead of a legal option character.  
Thus, all illegal options -- and options with missing arguments ---
conveniently come back as a single character '?'.  

But so does '-?' !  That means that '-?' is _always_ illegal.  
The programmer cannot make -? a legal option, 
but the user can deliberately invoke it as an illegal option syntax.
The user can _rely_ on it being erroneous!

If you provide an illegal option, the command is supposed to 
print a usage message to stderr and fail (return a non-zero error code).
That's exactly what the example did.

Since this is hard-coded into the getopt() function, many commands are
written with '-?' in mind.  Since the user can get a usage this way, 
it's not necessary, in POSIX, to have -h as an option.  
(But see my other mail about GNU and --help requirement.)

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019