Pythons argparse默认值不起作用 [英] Pythons argparse default value doesn't work

查看:248
本文介绍了Pythons argparse默认值不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python 2.7.13.

I'm using python 2.7.13.

我的目标是拥有三个可能的参数,如果用户未提供任何参数,则设置默认值:

My goal is to have three possible arguments, with default values being set if no arguments are given by the user:

parser.add_argument("-r", nargs=3, default=(0, 1000, 50), type=int, help="Useful help text")

这对我不起作用,如果可以以上述方式使用默认值,我在任何地方都找不到.

This doesn't work for me, and I can't find anywhere if it is possible to use default in such a way as above.

program.py -r身份运行时,出现错误:预期3个参数

When running it as program.py -r I get a an error: expected 3 argument(s)

但是我也尝试过完全删除nargs,并且只有一个默认值:

But I also tried removing nargs completely and only having one default value:

parser.add_argument("-r", default=100)

奇怪的是,这也不起作用.它至少需要一个参数...

Strangely enough, this doesn't work either. It requires at least one argument...

有人知道吗?

推荐答案

我将说明defaultargparse中的正常行为(带有Ipython交互式会话)

I'll illustrate the normal behavior of default in argparse (with a Ipython interactive session)

In [32]: parser = argparse.ArgumentParser()

定义3个动作:

In [33]: parser.add_argument('-r', nargs=3, type=int, default=(1,2,3));
In [35]: parser.add_argument('-f', default='DEFAULT');
In [37]: parser.add_argument('-g', nargs='?', default='DEFAULT', const='const');

help.请注意,所有操作都有[],表示它们是可选的:

The help. Note that all Actions have [], indicating that they are optional:

In [39]: parser.print_help()
usage: ipython3 [-h] [-r R R R] [-f F] [-g [G]]

optional arguments:
  -h, --help  show this help message and exit
  -r R R R
  -f F
  -g [G]

如果不带任何参数的情况下调用,则所有默认值都出现在args名称空间中.

If called without any argments, all of the defaults appear in the args namespace.

In [40]: parser.parse_args([])  # e.g python myprog.py
Out[40]: Namespace(f='DEFAULT', g='DEFAULT', r=(1, 2, 3))

-r提供3个数字(由nargs指定)

Giving -r with 3 numbers (as specified by the nargs)

In [41]: parser.parse_args('-r 4 5 6'.split())  
Out[41]: Namespace(f='DEFAULT', g='DEFAULT', r=[4, 5, 6])

指定其他标志之一.注意其余的默认值

Specify one of the other flags. Note the remaining defaults

In [42]: parser.parse_args('-f other'.split())  
Out[42]: Namespace(f='other', g='DEFAULT', r=(1, 2, 3))

-gnargs='?'还有另一种选择.它可以不带参数地给出.在这种情况下,它将获得const值.

-g with nargs='?' has another option. It can be given without arguments. In that case it gets the const value.

In [43]: parser.parse_args('-f other -g'.split())  
Out[43]: Namespace(f='other', g='const', r=(1, 2, 3))
In [44]: parser.parse_args('-f other -g more'.split())  
Out[44]: Namespace(f='other', g='more', r=(1, 2, 3))

nargs=3没有这样的三向选项.您要么提供3个值,要么不使用-r.如果您需要区分1)no-r标志,2)不带参数的r标志和3)带3个参数的flat,我建议将功能分为2个动作,一个为'store_true',另一个则为3个值.

There isn't such a 3 way option for nargs=3. You either provide the 3 values, or you don't use -r. If you need to distinguish between 1) no-r flag, 2) r flag without arguments, and 3) r flat with 3 arguments, I'd suggest splitting functionality into 2 actions, one a 'store_true', and other that takes the 3 values.

argparse中的默认值可能很复杂,设置方法有多种,字符串值和非字符串值之间的差异,甚至还有抑制它们的方法.但是我已经展示了基本行为.

Defaults in argparse can be a complicated matter, with various ways of setting them, differences between string and non-string values, even a way of suppressing them. But I've shown the basic behavior.

这篇关于Pythons argparse默认值不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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