ArgumentParser() 存储bool类型有坑,不能直接传True/False,需要设置action。
‘store_true’ and ‘store_false’
- These are special cases of ‘store_const’ used for storing the values True and False respectively. In addition, they create default values of False and True respectively. For example:
parser = argparse.ArgumentParser()
parser.add_argument('--foo', action='store_true')
parser.add_argument('--bar', action='store_false')
parser.add_argument('--baz', action='store_false')
parser.parse_args('--foo --bar'.split())
Namespace(foo=True, bar=False, baz=True)
[1] Parser for command-line options, arguments and sub-commands
本文揭示了如何在Python argparse模块中正确处理--foo和--bar类型的布尔选项,强调了'store_true'和'store_false'的特殊用法,以及它们默认值的区别。通过实例展示了如何设置并解析包含这些选项的命令行参数。

1474

被折叠的 条评论
为什么被折叠?



