Optparse and varible nargs

Paddy McCarthy paddy3118 at netscape.net
Tue Nov 4 22:23:21 CET 2003


John Goebel <jgoebel987 at yahoo.com> wrote in message news:<pan.2003.11.03.19.10.34.993533 at yahoo.com>...
> 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.




More information about the Python-list mailing list