Argparse中action的可选参数store_true和store_false
store_true 是指带触发action时为真,不触发则为假
parser.add_argument('-c', action='store_true')
# python test.py -c => c是true(触发)
# python test.py => c是false(无触发)
举个例子:
a.py文件的代码如下:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--t', help=' ', action='store_true', default=False)
config = parser.parse_args()
print(config.t)
直接运行python a.py,输出结果False
运行python a.py --t,输出结果True
也就是说,action=‘store_true’,只要运行时该变量有传参就将该变量设为True。