相关疑难解决方法(0)

有没有办法在python的argparse中创建参数,如果没有给出值,则返回true

目前--resize我创建的标志是boolean,意味着我的所有对象都将被调整大小:

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()
Run Code Online (Sandbox Code Playgroud)

有没有办法实现argparse参数,如果传递为boolean flag(--resize)将返回true,但如果传递value(--resize 10),将包含值.

例:

  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

python python-2.7 argparse

3
推荐指数
1
解决办法
853
查看次数

标签 统计

argparse ×1

python ×1

python-2.7 ×1