argument problem in optparse

Q

Qian Xu

Hi All,

I have a problem with OptParse.
I want to define such an arugument. It can accept additional value or no
value.

"myscript.py --unittest File1,File2"
"myscript.py --unittest"

Is it possible in OptParse? I have tried several combination. But ...


Best regards
--Qian Xu
 
J

John Machin

Hi All,

I have a problem with OptParse.
I want to define such an arugument. It can accept additional value or no
value.

"myscript.py --unittest File1,File2"
"myscript.py --unittest"

Is it possible in OptParse? I have tried several combination. But ...

Do you meen the included optparse module, or something else? Case
matters :)

Do you really want no value (and how would you expect optparse to tell
you it got no value?), or would an empty string suffice?

The following should work on Windows; adjust depending on your shell's
quoting/escaping mechanism for getting an empty string or a string
containing whitespace as exactly 1 arg:

myscript.py --unittest "File1, File2"
myscript.py --unittest ""

POC:
command-prompt>python -c "import sys; print sys.argv[1:]" foo "
baa baa" "" zot
['foo', 'baa baa', '', 'zot']

HTH,
John
 
J

John O'Hagan

Hi All,

I have a problem with OptParse.
I want to define such an arugument. It can accept additional value or no
value.

"myscript.py --unittest File1,File2"
"myscript.py --unittest"

Is it possible in OptParse? I have tried several combination. But ...


For reasons explained in the optparse documentation, it doesn't directly
support options which take an arbitrary number of arguments, to do that you
use a callback function like this:


import sys
from optparse import OptionParser

def my_callback(option, opt_str, value, parser):
"Accept options which take any number of arguments"

value = []
for item in parser.rargs:
if item[0] == '-': #stop at next option
break
value.append(item)
if not value:
value = True
setattr(parser.values, option.dest, value)

parser = OptionParser()
parser.add_option("--unittest", action="callback",
callback=my_callback, dest="unittest")

options = parser.parse_args(sys.argv[1:])[0]

Now if you invoke myscript.py with --unittest, options.unittest will
be 'True', with --unittest File1 File2, it will be ['File1', 'File2'], etc..

This doesn't work with the comma-separated argument example you gave, but it
could be made to; I hope the above gets you started. There are callback
examples in the optparse docs too.

Regards,

John
 
N

Neal Becker

Qian said:
Hi All,

I have a problem with OptParse.
I want to define such an arugument. It can accept additional value or no
value.

"myscript.py --unittest File1,File2"
"myscript.py --unittest"

Is it possible in OptParse? I have tried several combination. But ...


Best regards
--Qian Xu

Also check out argparse.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top