【Python】命令行如何传入列表数组作为参数

【Python】命令行如何传入列表作为参数

1. 代码

import argparse
 
parser = argparse.ArgumentParser()
 
'''By default it will fail with multiple arguments.'''
parser.add_argument('--default')
 
'''
Telling the type to be a list will also fail for multiple arguments,
but give incorrect results for a single argument.
'''
parser.add_argument('--list-type', type=list)

'''
This will allow you to provide multiple arguments, but you will get 
a list of lists which is not desired.
'''
parser.add_argument('--list-type-nargs', type=list, nargs='+')
 
'''
This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
An int is an explicit number of arguments to accept.
'''
parser.add_argument('--nargs', nargs='+')
 
'''To make the input integers'''
parser.add_argument('--nargs-int-type', nargs='+', type=int)
 
'''
An alternate way to accept multiple inputs, but you must
provide the flag once per input. Of course, you can use
type=int here if you want.
'''
parser.add_argument('--append-action', action='append')
 
'''Show the results of the given option to screen.'''
for _, value in parser.parse_args()._get_kwargs():
    if value is not None:
        print(value)

2. 实验结果

$ python arg.py --default 1234 2345 3456 4567
error: unrecognized arguments: 2345 3456 4567
 
$ python arg.py --list-type 1234 2345 3456 4567
error: unrecognized arguments: 2345 3456 4567

$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']

$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]

$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]

$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']

3. 总结

这几种是可以的:

parser.add_argument('--nargs', nargs='+')
$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']

parser.add_argument('--nargs-int-type', nargs='+', type=int)
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]

parser.add_argument('--append-action', action='append')
$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']

4. 参考

【1】https://blog.csdn.net/kinggang2017/article/details/94036386

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋冬无暖阳°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值