有没有一种方法可以在python的argparse中创建参数,如果没有给出值则返回true [英] Is there a way to create argument in python's argparse that returns true in case no values given

查看:132
本文介绍了有没有一种方法可以在python的argparse中创建参数,如果没有给出值则返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我创建的--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()

有没有一种实现argparse参数的方法,该参数如果作为布尔标志(--resize)传递将返回true,但是如果与值(--resize 10)传递则将包含值.

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.

示例:

  1. python ./my_script.py --resize # Will contain True that means, resize all the objects
  2. python ./my_script.py --resize <index> # Will contain index, that means resize only specific object
  1. python ./my_script.py --resize # Will contain True that means, resize all the objects
  2. 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:

  1. 未指定:该参数将获得其默认值(默认为None):

>>> parser.parse_args(''.split())
Namespace(resize=None)

  • 指定不带值:参数将获得其const值:

  • 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')
    

  • 由于要查找索引,因此也可以指定type=int,以便将参数值自动解析为整数.这不会影响默认或const情况,因此在这些情况下您仍会得到NoneTrue:

    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屋!

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