Python 如何在argparse中的命令后捕获和使用数据?
您好,我对argparse文档一无所知。我希望能够捕获并使用用户根据给定命令提供的数据 例如:Python 如何在argparse中的命令后捕获和使用数据?,python,argparse,Python,Argparse,您好,我对argparse文档一无所知。我希望能够捕获并使用用户根据给定命令提供的数据 例如: 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) 您将如何执行此功能 import argparse parser = argparse.ArgumentParser( description="Math equations", format
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)
您将如何执行此功能
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) # '+' for nargs means one or more. If you want it to be limited to three, just change it to 3.
args = vars(parser.parse_args())
print(args)
对于python func.py-type mult-data 2 3 1
:
{'data': [2, 3, 1], 'type': 'mult'}
usage: func.py [-h] -type {mult,add,div,sub} -data DATA [DATA ...]
test.py: error: argument -type: invalid choice: 'mul' (choose from 'mult', 'add', 'div', 'sub')
usage: func.py [-h] -type {mult,add,div,sub} -data DATA [DATA ...]
test.py: error: argument -type is required
对于python func.py-type mul-data 2 3 1
:
{'data': [2, 3, 1], 'type': 'mult'}
usage: func.py [-h] -type {mult,add,div,sub} -data DATA [DATA ...]
test.py: error: argument -type: invalid choice: 'mul' (choose from 'mult', 'add', 'div', 'sub')
usage: func.py [-h] -type {mult,add,div,sub} -data DATA [DATA ...]
test.py: error: argument -type is required
python func.py-h
:
usage: test.py [-h] -type {mult,add,div,sub} -data DATA [DATA ...]
Math equations
optional arguments:
-h, --help show this help message and exit
-type {mult,add,div,sub}
-data DATA [DATA ...]
python func.py-data 2 3 1
:
{'data': [2, 3, 1], 'type': 'mult'}
usage: func.py [-h] -type {mult,add,div,sub} -data DATA [DATA ...]
test.py: error: argument -type: invalid choice: 'mul' (choose from 'mult', 'add', 'div', 'sub')
usage: func.py [-h] -type {mult,add,div,sub} -data DATA [DATA ...]
test.py: error: argument -type is required
每一个开始都是艰难的。例如(尽管从长远来看,提出解决方案可能不会真正帮助您),代码可能如下所示:
import argparse
import numpy as np
# map the -type inputs to a function and a symbol for printing
TYPE_MAP = {
'mult': {'func': np.prod, 'symbol': '*'},
'add': {'func': np.sum, 'symbol': '+'}
}
# parse the args
def _parse_args():
parser = argparse.ArgumentParser(description='Foo')
parser.add_argument('-type', choices=TYPE_MAP.keys(), type=str,
help='specify type of operation that we want to do') # we accept 1 known string as input
parser.add_argument('-data', nargs='+', type=float,
help='specify numbers to process') # we accept a space separated list of inputs
return parser.parse_args()
if __name__ == '__main__':
args = _parse_args()
# make stuff in args easily accessable
symbol = TYPE_MAP[args.type]['symbol']
func = TYPE_MAP[args.type]['func']
data = args.data
# compute stuff
result = func(data)
details = '(' + symbol.join([str(number) for number in data]) + ')'
# print stuff
print result, details
为了从中学习,您可以尝试向该方法添加更多功能或添加另一个参数。也许您可以添加一个参数来禁止打印详细信息。?