简体   繁体   English

将元组作为命令行参数传递

[英]Passing a tuple as command line argument

My requirement is to pass a tuple as command line argument like我的要求是将元组作为命令行参数传递,例如

--data (1,2,3,4)

I tried to use the argparse module, but if I pass like this it is receiving as the string '(1,2,3,4)' .我尝试使用argparse模块,但如果我像这样传递,它会作为字符串'(1,2,3,4)' I tried by giving type=tuple for argparse.add_argument , but is of no use here.我尝试为argparse.add_argument提供type=tuple ,但在这里没有用。

Do I have to add a new type class and pass that to type argument of add_argument ?我是否必须添加一个新类型类并将其传递给add_argument类型参数?

Update更新

I tried the ast.literal_eval based on answers.我根据答案尝试了ast.literal_eval Thanks for that.感谢那。 But it is giving spaces in the result as shown below.但它在结果中给出了空格,如下所示。

(1,2,3,4)
<type 'str'>
(1, 2, 3, 4)
<type 'tuple'>

Set nargs of the data argument to nargs="+" (meaning one or more) and type to int , you can then set the arguments like this on the command line:nargs所述的data参数nargs="+" (意思是一个或多个)和类型int ,可以再这样设置的参数在命令行上:

--data 1 2 3 4

args.data will now be a list of [1, 2, 3, 4] . args.data现在将是[1, 2, 3, 4]

If you must have a tuple, you can do:如果你必须有一个元组,你可以这样做:

my_tuple = tuple(args.data)

Putting it all together:把它们放在一起:

parser = argparse.ArgumentParser()
parser.add_argument('--data', nargs='+', type=int)
args = parser.parse_args()
my_tuple = tuple(args.data)

I had same requirement.我有同样的要求。 Could not find in standard packages.在标准包中找不到。 So, rolled this.所以,推出了这个。 Python 3x.蟒蛇 3 倍。 Put inside simple argv parser, called one arg at a time:放入简单的 argv 解析器,一次调用一个 arg:

  • var is passed in default, of correct type. var是默认传递的,类型正确。
  • argval is found command line string. argval是找到命令行字符串。 Comma separated,no spaces.逗号分隔,没有空格。 ie 1,2,3 or one,two,three.即 1、2、3 或一、二、三。

Works with bash shell, may need quotes, other adjustments for other shells, OS's.与 bash shell 一起工作,可能需要引号,其他 shell 的其他调整,操作系统的。 Same technique works non list types.相同的技术适用于非列表类型。 This version creates a homogeneous list or tuple, based on var[0].此版本基于 var[0] 创建同构列表或元组。

example:例子:

./foo.py   --mytuple 1,2,3,4,5

code:代码:

def chk_arg( var, argstr, dbg=False): 
    ### loop on sys.argv here .....
    # if list/tuple type, use list comprehension
    # converts csv list to list or tuple 'type(var)' of 'type(var[0])' items. 
    if type(var) is list or type(var) is tuple:  
        val = type(var)([type(var[0])(i) for i in argval.split(',')]) 

If you want to scale this, not deal with syntax trees, and provide the tuple conversion within the argument, you'll need a custom argparse type.如果你想扩展它,而不是处理语法树,并在参数中提供元组转换,你需要一个自定义的 argparse 类型。 (I needed this since my parser is in a package, and I didn't want to require my users to all write my_tuple = tuple(args.data) . With an argument type, you can also receive a string with parenthesis like the original question. Also, I wouldn't include nargs , because that will wrap your custom type output into a list, which basically defeats the purpose of your custom type. I mapped a float because my input was something like "0.9, 0.99", but your int works the same: (我需要这个,因为我的解析器在一个包中,我不想要求我的用户都写my_tuple = tuple(args.data) 。使用参数类型,您还可以像原始一样接收带括号的字符串问题。另外,我不会包括nargs ,因为这会将您的自定义类型输出包装到一个列表中,这基本上违背了您的自定义类型的目的。我映射了一个浮点数,因为我的输入类似于“0.9, 0.99”,但是你的 int 工作原理相同:

def tuple_type(strings):
    strings = strings.replace("(", "").replace(")", "")
    mapped_int = map(int, strings.split(","))
    return tuple(mapped_int)

parser = argparse.ArgumentParser()
parser.add_argument('--data', type=tuple_type)
args = parser.parse_args()

This way you can send in --data "(1,2,3,4)" or --data "1,2,3,4"这样你就可以发送--data "(1,2,3,4)"--data "1,2,3,4"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM