Python argparse:metavar 和 action=store_true 一起使用 [英] Python argparse: metavar and action=store_true together

查看:27
本文介绍了Python argparse:metavar 和 action=store_true 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 中使用 argparse 模块来解析在命令行界面中输入的参数.我对子解析器对象进行了以下 add_argument 调用:

I'm using argparse module in Python to parse parameters typed in a command line interface. I have the following add_argument call to a subparser object:

submit_parser.add_argument('-pv','--provision',metavar='PROVISION', dest='PROVISION',
                                 help='provision system',
                                 action='store_true', default=False, required=False)

我收到此错误:

Traceback (most recent call last):
  File "./scripts/tp4", line 94, in <module> 
    main()
  File "./scripts/tp4", line 74, in main 
    modloader.loadModules(sub_parsers)
  File "/usr/lib/python2.6/site-packages/tp4/cli/Moduleloader.py", line 66, in loadModules 
    registered_modules[module_name].setSubparserArgs(module_sub_parser)
  File "/usr/lib/python2.6/site-packages/tp4/cli/modules/AutotestModule.py", line 135, in setSubparserArgs
    action='store_true', default=False, required=False)
  File "/usr/share/tp4/cli/zip/argparse.zip/argparse.py", line 1302, in add_argument
    TypeError: __init__() got an unexpected keyword argument 'metavar'

如果我删除操作或元变量参数,它会起作用.为什么两个不能在一起?在 http://docs.python.org/dev/library/argparse 的 argparse 文档中没有任何关于此限制的内容.html.

If I remove action or metavar parameters, it works. Why both can't be together? There is nothing about this restriction in argparse documentation at http://docs.python.org/dev/library/argparse.html.

在此先感谢您的帮助

推荐答案

元变量仅对位置参数(想想命令行末尾的文件名)或当参数采用自己的参数(如 --input-files foo.txt bar.txt).

A metavar only makes sense for positional arguments (think filenames at the end of the command line) or for when an argument takes arguments of its own (like --input-files foo.txt bar.txt).

您的 --provision 参数是一个标志,因为您将 action 设置为 store_true.它不接受任何参数(即 nargs 未设置).因此,拥有元变量是没有意义的.

Your --provision argument is a flag because you set the action to store_true. It doesn't take any arguments (i.e., nargs isn't set). As such, it doesn't make sense to have a metavar.

来自argparse 文档:

ArgumentParser 生成帮助消息时,它需要某种方式来引用每个预期的参数.默认情况下,ArgumentParser 对象使用 dest 值作为每个对象的名称".默认情况下,对于位置参数动作,直接使用 dest 值,对于可选参数动作,dest 值是大写的.因此,带有 dest='bar' 的单个位置参数将被称为 bar.单个可选参数 --foo 后面应该跟一个命令行参数将被称为 FOO.

When ArgumentParser generates help messages, it need some way to refer to each expected argument. By default, ArgumentParser objects use the dest value as the "name" of each object. By default, for positional argument actions, the dest value is used directly, and for optional argument actions, the dest value is uppercased. So, a single positional argument with dest='bar' will be referred to as bar. A single optional argument --foo that should be followed by a single command-line argument will be referred to as FOO.

这篇关于Python argparse:metavar 和 action=store_true 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆