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

查看:52
本文介绍了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...

有人明白吗?

推荐答案

我将在 argparse 中说明 default 的正常行为(使用 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');

帮助.注意所有的Action都有[],表示它们是可选的:

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 向选项.您要么提供 3 个值,要么不使用 -r.如果您需要区分 1) no-r 标志,2) 不带参数的 r 标志,和 3) 带 3 个参数的 r 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天全站免登陆