Search code examples
pythonoptparse

Python Optparse list


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?


Solution

  • Look at option callbacks. Your callback function can parse the value into a list using a basic optarg.split(',')