Python Optparse list
Question:
I’m using the python optparse module in my program, and I’m having trouble finding an easy way to parse an option that contains a list of values.
For example:
--groups one,two,three.
I’d like to be able to access these values in a list format as options.groups[]
. Is there an optparse option to convert comma separated values into a list? Or do I have to do this manually?
Answers:
Look at option callbacks. Your callback function can parse the value into a list using a basic optarg.split(',')
S.Lott’s answer has already been accepted, but here’s a code sample for the archives:
def foo_callback(option, opt, value, parser):
setattr(parser.values, option.dest, value.split(','))
parser = OptionParser()
parser.add_option('-f', '--foo',
type='string',
action='callback',
callback=foo_callback)
Again, just for the sake of archive completeness, expanding the example above:
- You can still use “dest” to specify the option name for later access
- Default values cannot be used in such cases (see explanation in Triggering callback on default value in optparse)
- If you’d like to validate the input, OptionValueError should be thrown from foo_callback
The code (with tiny changes) would then be:
def get_comma_separated_args(option, opt, value, parser):
setattr(parser.values, option.dest, value.split(','))
parser = OptionParser()
parser.add_option('-f', '--foo',
type='string',
action='callback',
callback=get_comma_separated_args,
dest = foo_args_list)
With optparse, to get a list value you can use action ‘append‘:
from optparse import OptionParser
parser = OptionParser()
parser.add_option("--group",
action="append",
dest="my_groups")
(options, args) = parser.parse_args()
print options.my_groups
Then call your program like this:
$ python demo.py –group one –group two –group three
[‘one’, ‘two’, ‘three’]
I’m using the python optparse module in my program, and I’m having trouble finding an easy way to parse an option that contains a list of values.
For example:
--groups one,two,three.
I’d like to be able to access these values in a list format as options.groups[]
. Is there an optparse option to convert comma separated values into a list? Or do I have to do this manually?
Look at option callbacks. Your callback function can parse the value into a list using a basic optarg.split(',')
S.Lott’s answer has already been accepted, but here’s a code sample for the archives:
def foo_callback(option, opt, value, parser):
setattr(parser.values, option.dest, value.split(','))
parser = OptionParser()
parser.add_option('-f', '--foo',
type='string',
action='callback',
callback=foo_callback)
Again, just for the sake of archive completeness, expanding the example above:
- You can still use “dest” to specify the option name for later access
- Default values cannot be used in such cases (see explanation in Triggering callback on default value in optparse)
- If you’d like to validate the input, OptionValueError should be thrown from foo_callback
The code (with tiny changes) would then be:
def get_comma_separated_args(option, opt, value, parser):
setattr(parser.values, option.dest, value.split(','))
parser = OptionParser()
parser.add_option('-f', '--foo',
type='string',
action='callback',
callback=get_comma_separated_args,
dest = foo_args_list)
With optparse, to get a list value you can use action ‘append‘:
from optparse import OptionParser parser = OptionParser() parser.add_option("--group", action="append", dest="my_groups") (options, args) = parser.parse_args() print options.my_groups
Then call your program like this:
$ python demo.py –group one –group two –group three
[‘one’, ‘two’, ‘three’]