python - How to capture and use data after commands in argparse? -


hello understand nothing in argparse documentation. want able capture , use data given user follow given command.

for instance :

python func.py -type mult -data 2 3 1 6 (2*3*1)  python func.py -type add -data 2 5 1 8 (2+5+1) 

how function ?

import argparse  parser = argparse.argumentparser(     description="math equations",     formatter_class=argparse.rawtexthelpformatter )  parser.add_argument("-type", choices=("mult", "add", "div", "sub"), required=true) parser.add_argument("-data", nargs='+', type=int, required=true) # '+' nargs means 1 or more.  if want limited three, change 3.  args = vars(parser.parse_args()) print(args) 

for python func.py -type mult -data 2 3 1:

{'data': [2, 3, 1], 'type': 'mult'} 

for python func.py -type mul -data 2 3 1:

usage: func.py [-h] -type {mult,add,div,sub} -data data [data ...] test.py: error: argument -type: invalid choice: 'mul' (choose 'mult', 'add', 'div', 'sub') 

python func.py -h:

usage: test.py [-h] -type {mult,add,div,sub} -data data [data ...]  math equations  optional arguments:   -h, --help            show message , exit   -type {mult,add,div,sub}   -data data [data ...] 

python func.py -data 2 3 1:

usage: func.py [-h] -type {mult,add,div,sub} -data data [data ...] test.py: error: argument -type required 

Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -