optparse options

Robert Kern robert.kern at gmail.com
Tue May 19 18:16:46 EDT 2009


On 2009-05-19 16:07, icarus wrote:
> Why do I need to put two options for this script to print the path?
>
> if I just specify the option and argument...
> $ python<script>.py -p xxxx
> Usage:<script>.py [-p dir] [--part=dir]
>
> <script>.py: error: No options specified
>
> So I need to give it two arguments...
> $ python<script>.py --part xxxx y
> xxxx

You have this piece of code:

	if len(args) != 1:
		parser.error("No options specified")

The args list contains all of the arguments after the options. Unlike sys.argv, 
it does not contain the executable's name in the first slot.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list