python *args参数如何用input_python – Argparse:两个位置参数,带有nargs =”

@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加在一起,并将自己分开寻找“”,“/”,但在做一些如此丑陋的事情之前我想过点击集体思想.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值