action=store_true和store_false理解及实战测试

· store_true 是指带触发 action 时为真,不触发则为假, 即默认 False ,传参 则 设置为 True
· store_false 则与之相反

以代码为例:

import sys
import argparse
def parse_args():
    parser = argparse.ArgumentParser(description='run a test ')
    parser.add_argument('-t', '--threshold', type=float, default=100.0, help='blurry threshold')
    
    parser.add_argument('-f', '--first', action='store_true', help='default False')

    parser.add_argument('-s', '--second', action='store_false', help='default True')

    return parser.parse_args()
if __name__ == '__main__':
    assert sys.version_info >= (3, 6), sys.version_info
    args = parse_args()

    print(args.first)
    print(args.second)

运行1:

python test.py

结果

False
True

运行2:

python test.py --f --s

结果2:

True
False

猜你喜欢

转载自blog.csdn.net/Bluebro/article/details/131951245