简体   繁体   English

Argparse:如何接受具有不同“类型”的参数

[英]Argparse: How to accept arguments that have different “types”

I want to accept arguments in the following style: 我想接受以下样式的参数:

python3 test.py -r SERVICE=100,101,102 -r SERVICE2=1,2,3,4,5 
(-r REPO=REV#,REV#,etc... -r etc...)

I've done the following so far (added the argument -r and defined the type revs). 到目前为止,我已完成以下操作(添加了参数-r并定义了类型转速)。 It's supposed to pass back two lists, which may be problematic. 它应该传回两个列表,这可能会有问题。

import argparse

def revs(s):
    try:            
        REPO, REVISIONS = map(str, s.split('='))
        REVISIONS = map(int, REVISIONS.split(','))
        return REPO, REVISIONS
    except:
        raise argparse.ArgumentTypeError("Must be format -r REPO=REV,REV,etc.. e.g. SERVICES=181449,181447") 

parser.add_argument('-r', type='revs', nargs='*', action='append', help='Revisions to update. The list is prefixed with the name of the source repository and a "=" sign. This parameter can be used multiple times.', required=True)

I get the following error when running with the above: 运行上述内容时出现以下错误:

Traceback (most recent call last):
File "test.py", line 11, in <module>
parser.add_argument('-r', type='revs', nargs='*', action='append', help='Revisions to update. The list is prefixed with the name of the source repository and a "=" sign. This parameter can be used multiple times.', required=True)
File "/usr/local/lib/python3.3/argparse.py", line 1317, in add_argument
raise ValueError('%r is not callable' % (type_func,))

ValueError: 'revs' is not callable ValueError:'revs'不可调用

you want 你要

parser.add_argument('-r', type=revs, ...)

not

parser.add_argument('-r', type='revs', ...)

The type argument must be a callable object -- since strings aren't callable, they can't be used as the type . type参数必须是可调用对象 - 因为字符串不可调用,所以它们不能用作type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 配置argparse以接受带引号的参数 - Configure argparse to accept quoted arguments Argparse:如何接受任意数量的可选参数(以`-`或`--`开头) - Argparse: How to accept any number of optional arguments (starting with `-` or `--`) 如何在argparse的单独命名空间中使用子解析器参数? - How to have sub-parser arguments in separate namespace with argparse? 如何使用argparse列出不带连字符(-)的可能参数 - How to have a list of possible arguments without hyphens(--) using argparse 在 argparse 中传递不同的参数组合 - Pass different combinations of arguments in argparse 如何根据 Python 中的位置 arguments 与 argparse 的组合设置不同的选项? - How to set different options based on combination of positional arguments in Python with argparse? 如何使用 argparse 删除 CLI 参数,以便 unittest 接受 arg 列表 - How can I remove CLI arguments using argparse so unittest will accept arg list argparse - 使用其他参数定义自定义操作或类型 - argparse - Define custom actions or types with additional arguments python argparse具有不同数量的参数的不同参数 - python argparse different parameters with different number of arguments Python,argparse:具有不同数量参数的不同参数 - Python, argparse: different parameters with different number of arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM