optparse multiple arguments

R

Ritesh Raj Sarraf

Hi,

I'm having some minor problems with optparse. I'm just worried that
someone shouldn't say that multiple argument feature isn't implemented
in optpartse.

How to tackle multiple arguments to an option ?
As far as I dug, I've found that >1 arguments are being ignored.

parser.add_option("", "--my-option", dest="my_option", action="store",
type="string")

Now if someone uses it as:
../foo --my-option a b c

I want somehow to store all the three arguments but my_option stores
only "a" while ignoring b and c.

Any help?

Ritesh
 
R

Ritesh Raj Sarraf

Ritesh said:
Hi,

I'm having some minor problems with optparse. I'm just worried that
someone shouldn't say that multiple argument feature isn't implemented
in optpartse.

How to tackle multiple arguments to an option ?
As far as I dug, I've found that >1 arguments are being ignored.

parser.add_option("", "--my-option", dest="my_option", action="store",
type="string")

Now if someone uses it as:
./foo --my-option a b c

I want somehow to store all the three arguments but my_option stores
only "a" while ignoring b and c.

I just noticed that the args variable is holding values b and c.
the args variables comes from:
(options, args) = parser.parse_args()

I guess I only need to figure out now is why args isn't storing
argument "a" also...

Ritesh
 
R

Ritesh Raj Sarraf

Ritesh said:
I just noticed that the args variable is holding values b and c.
the args variables comes from:
(options, args) = parser.parse_args()

I guess I only need to figure out now is why args isn't storing
argument "a" also...

Ritesh

I fixed it, I guess.

parser.add_option("", "--my-option", dest="my_option",
action="store_true")

sets my_option to True and the arguments are all stored in the list
"args". :)

Ritesh
 
F

Fredrik Lundh

Ritesh said:
I fixed it, I guess.

parser.add_option("", "--my-option", dest="my_option",
action="store_true")

sets my_option to True and the arguments are all stored in the list
"args". :)

note that whatever action you're using, args holds the *remaining* arguments, after
option processing.

</F>
 
F

Fredrik Lundh

Ritesh said:
That won't help because by design of my program, I can't limit the
number of arguments a user can pass to it.

do you want options, arguments, or are you just somewhat confused ?

</F>
 
R

Ritesh Raj Sarraf

Fredrik said:
do you want options, arguments, or are you just somewhat confused ?

</F>

I'm not sure about that.

My program will accept user input. The user can input multiple
arguments. maybe 1 or maybe 100, that's not definite. I do need option
because this particular (multiple) argument criteria is for one single
option.

Thanks,
Ritesh
 
S

Steven Bethard

Ritesh said:
Now if someone uses it as:
./foo --my-option a b c

I want somehow to store all the three arguments

Currently optparse doesn't support options with an arbitrary number of
arguments. I'm currently working on a optparse-inspired replacement
which does support this along the lines of:

parser.add_option('--my-option', nargs='*')

but it's not quite ready for prime-time yet. I'm hoping to put an
announcement out in the next couple of weeks.

STeVe
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top