getopt question
Sean DiZazzo
half.italian at gmail.com
Fri Apr 9 20:13:51 EDT 2010
On Apr 9, 4:37 pm, Raphael Mayoraz <may... at netplus.ch> wrote:
> Ooops, forgot to attach the file in my first e-mail. Now, here it is.
>
> --------------------------------------------------------
> Hello,
>
> I have some trouble to make getopt.getopt work and the way I want (and
> is described in the documentation).
>
> Attached is very small script to reproduce my problem.
>
> If running:
> > python testGetOpt.py -a junk1 -b junk2
> everything is ok
>
> > python testGetOpt.py -c junk1
> ok too: I get the 'Invalid option' error message
>
> now:
> > python testGetOpt.py -a junk1 -c junk2
> I'm expecting this to also print the error message, as 'c' is not in the
> argument given in getopt.getopt, but it doesn't.
>
> What am I doing wrong ?
>
> Using python 2.6.4 on WindowXP.
>
> Thanks.
>
> Raphael
> ---------------------------------------------------------
>
> [testGetOpt.py< 1K ]#!/usr/bin/env python
> import sys
> import getopt
>
> try:
> opts, args = getopt.getopt(sys.argv[1:], 'ab')
> except getopt.error:
> print 'Invalid option'
> sys.exit(0)
If your argument is expecting a value, you need to add a colon after
it in the argument string. ie. 'a:b:'
Guessing if you print 'args' from your example, both the -c and junk1
will be in there.
~Sean
More information about the Python-list
mailing list