views:

278

answers:

3

I am using the OptionParser from optparse module to parse my command that I get using the raw_input().

I have these questions.

1.) I use OptionParser to parse this input, say for eg. (getting multiple args)

my prompt> -a foo -b bar -c spam eggs

I did this with setting the action='store_true' in add_option() for '-c',now if there is another option with multiple argument say -d x y z then how to know which arguments come from which option? also if one of the arguments has to be parsed again like

my prompt> -a foo -b bar -c spam '-f anotheroption'

2.) if i wanted to do something like this..

my prompt> -a foo -b bar 
my prompt> -c spam eggs 
my prompt> -d x y z

now each entry must not affect the other options set by the previous command. how to accomplish these?

+3  A: 

For part 2: you want a new OptionParser instance for each line you process. And look at the cmd module for writing a command loop like this.

Ned Batchelder
wow! thanx.. i din't know python had a builtin library for these kind of stuff.. but still there are no examples provided in the doc. :(
Sriram
cmd is awesome... now i am trying to insert OptionParser into the processing of a command in the custom command line UI..
Sriram
+1  A: 

optparse solves #1 by requiring that an argument always have the same number of parameters (even if that number is 0), variable-parameter arguments are not allowed:

Typically, a given option either takes an argument or it doesn’t. Lots of people want an “optional option arguments” feature, meaning that some options will take an argument if they see it, and won’t if they don’t. This is somewhat controversial, because it makes parsing ambiguous: if "-a" takes an optional argument and "-b" is another option entirely, how do we interpret "-ab"? Because of this ambiguity, optparse does not support this feature.

You would solve #2 by not reusing the previous values to parse_args, so it would create a new values object rather than update.

Roger Pate
Of course, I also agree you shouldn't use optparse where cmd is more appropriate, but here is how optparse works!
Roger Pate
thanx for the part two of the 1s question..i know there is a way to handle multiple argument option but, i want to know how to manage multiple options with multiple arguments. it can be done by using 'store_true'. but still all the aruguments go into the same args list, and no reference for which function it belongs :(
Sriram
If an option takes arguments, you need to specify nargs for it, then those won't be saved in 'args'.
Roger Pate
yes now i get a tuple for the arguments.. this is what i wanted.. and 'store_true' must not be used with nargs option...
Sriram
+2  A: 

You can also solve #1 using the nargs option attribute as follows:

parser = OptionParser()
parser.add_option("-c", "", nargs=2)
parser.add_option("-d", "", nargs=3)
ema
thanx i think i am a bit clear now! :)
Sriram
it helped a bit but how can i say what arguments are for what options as all the arguments end up on the same list
Sriram
got it! sexy!yes now i get a tuple for the arguments.. this is what i wanted.. and 'store_true' must not be used with nargs option... :)
Sriram
@Sriram: right! Glad I could help.
ema