optional args for getopt()

Richard Jones richard at bizarsoftware.com.au
Thu Aug 23 05:37:46 CEST 2001

On Thursday 23 August 2001 12:59, bvdpoel at uniserve.com wrote:
> Is there a way to signal that the arg for getopt in optional. In C I can
> set an option string to something like:
> 	opts = "hg:b::"
> which means that 'g' needs an arg and that 'b' may have an arg. Seems
> the python routine is lacking??

>From the manual page on my Linux system,
       optstring  is  a  string  containing the legitimate option
       characters.  If such a character is followed by  a  colon,
       the  option  requires  an  argument,  so  getopt  places a
       pointer to the following text in the same argv-element, or
       the  text  of  the following argv-element, in optarg.  Two
       colons mean an option takes an optional arg; if  there  is
       text  in  the  current  argv-element,  it  is  returned in
       optarg, otherwise optarg is set to zero.  This  is  a  GNU

Note "This is a GNU extension". I presume the POSIX standard doesn't require 
it (don't have it handy). The python implementation will probably have been 
written by someone working from the POSIX spec (or at least someone who was 
working on a non-GNU-enhanced system :)

By all means, put in a feature/bug request on python.sf.net to have this 
ammended for the next release.


More information about the Python-list mailing list