@H_404_6@
我正在尝试在图像之间进行数学运算.我已定义(我的真实代码的简化版本):
parser = argparse.ArgumentParser(description='Arithmetic operations on images')
parser.add_argument("input1",Metavar='input1',action='store',help='list of input images to operate with',nargs="+",type=str)
parser.add_argument("operation",Metavar='operation',type=str,help='type of operation (+,-,*,/) to be done',nargs=1)
parser.add_argument("input2",Metavar='input2',help='image (or value) with which to perform the operation on input1')
这段代码产生:
arith.py -h
usage: arith.py [-h] input1 [input1 ...] operation input2 [input2 ...]
所以它确实理解input1可以包含一个或多个元素,操作将是单个元素,input2可以是任意数量的元素.
当然,问题在于,有两个位置参数和不确定数量的元素,argparse会混淆什么.我已经尝试将choices = [“”,“ – ”,“*”,“/”]添加到’operation’,以便它知道在哪里进行分离,但似乎argparse无法做到.实际上,在argparse文档中,谈论nargs =’*’你可以读到:
Note that it generally doesn’t make much sense to have more than one positional argument with nargs=’*’
我原以为我可以将args.input1,args.operation和args.input2加在一起,并将自己分开寻找“”,“/”,但在做一些如此丑陋的事情之前我想过点击集体思想.