argparse module How to add option without any argument?

后端 未结 2 1229
天涯浪人
天涯浪人 2021-01-29 22:57

I have created a script using argparse.

The script needs to take a configuration file name as an option, and user can specify whether they need to proceed t

2条回答
  •  野的像风
    2021-01-29 23:49

    As @Felix Kling suggested use action='store_true':

    >>> from argparse import ArgumentParser
    >>> p = ArgumentParser()
    >>> _ = p.add_argument('-f', '--foo', action='store_true')
    >>> args = p.parse_args()
    >>> args.foo
    False
    >>> args = p.parse_args(['-f'])
    >>> args.foo
    True
    

提交回复
热议问题