argparse list

N

Neal Becker

I'm interested in using argparse to parse a string formatted as:

my_prog --option1=1,10,37

That is, a list of comma delimited values. I guess nargs almost does it,
but expects options to be space-delimited.

What would be the easiest approach?
 
P

Peter Otten

Neal said:
I'm interested in using argparse to parse a string formatted as:

my_prog --option1=1,10,37

That is, a list of comma delimited values. I guess nargs almost does it,
but expects options to be space-delimited.

What would be the easiest approach?
.... return map(int, value.split(","))
....
p = argparse.ArgumentParser()
p.add_argument("--option1", type=csv) and None
p.parse_args(["--option1=1,10,37"]) Namespace(option1=[1, 10, 37])
_.option1[0]
1

Peter
 
M

Michele Simionato

I'm interested in using argparse to parse a string formatted as:

my_prog --option1=1,10,37

That is, a list of comma delimited values.  I guess nargs almost does it,
but expects options to be space-delimited.

What would be the easiest approach?

In plac (http://pypi.python.org/pypi/plac) you would write the
following:

import plac

@plac.annotations(
option1=('option with comma separated values',
'option',
'o',
lambda x: x.split(',')))
def main(option1):
print option1

if __name__ == '__main__':
plac.call(main)
 

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,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top