栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用argparse将列表作为命令行参数传递?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用argparse将列表作为命令行参数传递?

TL; DR

使用

nargs
选项或选项的
'append'
设置
action
(取决于您希望用户界面的行为方式)。

纳尔

parser.add_argument('-l','--list', nargs='+', help='<Required> Set flag', required=True)# Use like:# python arg.py -l 1234 2345 3456 4567

nargs='+'
接受1个或多个参数,
nargs='*'
接受零个或多个。

附加

parser.add_argument('-l','--list', action='append', help='<Required> Set flag', required=True)# Use like:# python arg.py -l 1234 -l 2345 -l 3456 -l 4567

append
您提供多次创建该列表的选项。

不要使用

type=list
-可能没有可能要与一起使用的
type=list
情况
argparse
。曾经


让我们更详细地了解人们可能尝试执行此操作的一些不同方式以及最终结果。

import argparseparser = 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 integersparser.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')# To show the results of the given option to screen.for _, value in parser.parse_args()._get_kwargs():    if value is not None:        print(value)

这是您可以期望的输出:

$ python arg.py --default 1234 2345 3456 4567...arg.py: error: unrecognized arguments: 2345 3456 4567$ python arg.py --list-type 1234 2345 3456 4567...arg.py: error: unrecognized arguments: 2345 3456 4567$ # Quotes won't help here... $ 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']

小贴士

  • 使用
    nargs
    action='append'
    • nargs
      从用户的角度来看,它可能更直接,但是如果存在位置参数,则可能是不直观的,因为
      argparse
      无法分辨什么应该是位置参数以及什么属于
      nargs
      ;如果您有位置参数,那么
      action='append'
      最终可能是更好的选择。
    • 如果以上是唯一真正的
      nargs
      给予
      '*'
      '+'
      '?'
      。如果您提供一个整数(例如
      4
      ),那么将选项与
      nargs
      和位置参数混合将不会有问题,因为
      argparse
      它将确切知道该选项需要多少个值。
  • 不要在命令行1上使用引号
  • 不要使用
    type=list
    ,因为它会返回列表列表
    • 发生这种情况是因为在后台
      argparse
      使用的值
      type
      来强迫您选择的 每个给定参数
      type
      ,而不是所有参数的总和。
    • 您可以使用
      type=int
      (或其他任何方式)获取一个整数列表(或其他任何方式)

1:我的意思不是一般。.我的意思不是用引号 _将列表传递给

argparse
_您。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/645618.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号