Optparse and varible nargs

J

John Goebel

I have a program that I want to pass a variable number of things too.

foo_prog -f one two three

optparse has the nargs feature, but it is determinant; you have to know
how many variables are going to be passed.

Is there a suggestion on how to use optparse with something like:

nargs = >1?

Thanks,
John
 
D

David Goodger

John said:
I have a program that I want to pass a variable number of things
too.

foo_prog -f one two three

Are "one", "two", and "three" positional arguments or option
arguments? Can you have a command line like this?

foo_prog -f one two -g three


If positional arguments, optparse doesn't process them. If option
arguments, how would optparse know how many to process? Have you
read "The Tao of Option Parsing"? If not, please do:
<http://optik.sourceforge.net/tao.html>.

-- David Goodger
 
P

Paddy McCarthy

John Goebel said:
I have a program that I want to pass a variable number of things too.

foo_prog -f one two three

optparse has the nargs feature, but it is determinant; you have to know
how many variables are going to be passed.

Is there a suggestion on how to use optparse with something like:

nargs = >1?

Thanks,
John
Normally see solutions to this kind of thing where you are allowed to
enter one option that can be a list of items with a defined separator,
e.g. if you opt for a space separator your line would be

foo_prog -f 'one two three'

Or a colon as a separator:

foo_prog -f 'one:two:three'

You would use optparse to return the one string for the -f option then
split it on the agreed separator to produce the list of values.

Cheers, Paddy.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top