[Tutor] optparse example

Carlos Hanson carlos.hanson at gmail.com
Mon Sep 22 16:53:07 CEST 2008


On Mon, Sep 22, 2008 at 7:16 AM, Tasos Latsas <tlatsas2000 at gmail.com> wrote:
> Hello list,
> I tried the optparse example from the python library reference and it
> doesn't seem to work..what am I doing wrong?
> I keep getting the "incorrect number of arguments" message although i
> use the correct number..
>
> The example is here :
> http://docs.python.org/lib/optparse-putting-it-all-together.html
>
> if i edit the code and write:
> print parser.parse_args()
>
> I get:
> (<Values at 0xb7d3b9ec: {'verbose': False, 'filename': None}>, [])
>
> as you can see the argument list is empty
> I run the program as ./parser.py -q
> (i also tried 'python parser.py -q' but didnt help)
>
> Thank you in advance
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>

(I failed to send my original reply to the list)

Notice the Usage message when you run the example:

 $ python parser.py -q
 Usage: parser.py [options] arg

 parser.py: error: incorrect number of arguments

"-q" is an option, not an argument. If you look at the example, you add options:

 parser.add_option("-q", ...

The arguments are everything left over. In this case, they are not
used, but they are required. The following runs without issue:

 $ python parser.py junk
 $ python parser.py -v junk
 reading None...

The bottom line is the example is just an example, not something you
can use as is.

-- 
Carlos Hanson


More information about the Tutor mailing list