有没有办法在 python 的 argparse 中创建参数,在没有给出值的情况下返回 true [英] Is there a way to create argument in python's argparse that returns true in case no values given
问题描述
目前我创建的 --resize
标志是布尔值,这意味着我的所有对象都将被调整大小:
parser.add_argument("--resize", action="store_true", help="Do dictionary resize")# ...# 如果调整大小标志为真,我将重新调整所有对象的大小如果 args.resize:对于 my_obects 中的对象:object.do_resize()
有没有办法实现 argparse 参数,如果作为布尔标志 (--resize
) 传递将返回 true,但如果传递值 (--resize
), 将包含值.
示例:
python ./my_script.py --resize # 将包含 True 表示,调整所有对象的大小
python ./my_script.py --resize
# 将包含索引,这意味着只调整特定对象的大小
为了有选择地接受一个值,您需要设置 nargs
到 '?'
.如果指定,这将使参数消耗一个值.如果参数已指定但没有值,则参数将被分配参数的 const
值,所以这也是你需要指定的:
parser = argparse.ArgumentParser()parser.add_argument('--resize', nargs='?', const=True)
这个论点现在有三种情况:
未指定:参数将获得其默认值 (
<预><代码>>>>parser.parse_args(''.split())命名空间(调整大小=无)None
默认):未指定值:参数将获得其常量值:
<预><代码>>>>parser.parse_args('--resize'.split())命名空间(调整大小=真)Specified with a value:参数会得到指定的值:
<预><代码>>>>parser.parse_args('--resize 123'.split())命名空间(resize='123')
由于您正在寻找索引,您还可以指定 type=int
以便参数值将自动解析为整数.这不会影响 default 或 const 情况,因此在这些情况下您仍然会得到 None
或 True
:
<小时>
您的使用情况将如下所示:
如果 args.resize 为 True:对于 my_objects 中的对象:object.do_resize()elif args.resize:my_objects[args.resize].do_resize()
Currently --resize
flag that I created is boolean, and means that all my objects will be resized:
parser.add_argument("--resize", action="store_true", help="Do dictionary resize")
# ...
# if resize flag is true I'm re-sizing all objects
if args.resize:
for object in my_obects:
object.do_resize()
Is there a way implement argparse argument that if passed as boolean flag (--resize
) will return true, but if passed with value (--resize 10
), will contain value.
Example:
python ./my_script.py --resize # Will contain True that means, resize all the objects
python ./my_script.py --resize <index> # Will contain index, that means resize only specific object
In order to optionally accept a value, you need to set nargs
to '?'
. This will make the argument consume one value if it is specified. If the argument is specified but without value, then the argument will be assigned the argument’s const
value, so that’s what you need to specify too:
parser = argparse.ArgumentParser()
parser.add_argument('--resize', nargs='?', const=True)
There are now three cases for this argument:
Not specified: The argument will get its default value (
None
by default):>>> parser.parse_args(''.split()) Namespace(resize=None)
Specified without a value: The argument will get its const value:
>>> parser.parse_args('--resize'.split()) Namespace(resize=True)
Specified with a value: The argument will get the specified value:
>>> parser.parse_args('--resize 123'.split()) Namespace(resize='123')
Since you are looking for an index, you can also specify type=int
so that the argument value will be automatically parsed as an integer. This will not affect the default or const case, so you still get None
or True
in those cases:
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--resize', nargs='?', type=int, const=True)
>>> parser.parse_args('--resize 123'.split())
Namespace(resize=123)
Your usage would then look something like this:
if args.resize is True:
for object in my_objects:
object.do_resize()
elif args.resize:
my_objects[args.resize].do_resize()
这篇关于有没有办法在 python 的 argparse 中创建参数,在没有给出值的情况下返回 true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!