# test.py
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--test_action", action='store_true')
args = parser.parse_args()
action_val = args.test_action
print(action_val)
以上面的代码为例,若触发 test_action,则为 True, 否则为 False:
$ python test.py,输出为 False
$ python test.py --test_action,输出为 True
若在上面的代码中加入default,设为 False 时:
parser.add_argument("--test_action", default='False', action='store_true')
1
$ python test.py,输出为 False
$ python test.py --test_action,输出为 True<