One optional argument which does not require positional arguments

后端 未结 3 2013
抹茶落季
抹茶落季 2021-02-10 23:02

I have a question regarding python\'s argparse: Is it possible to have a optional argument, which does not require positional arguments?

Example:

parser.         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-10 23:51

    You get error: to few arguments because lat and lon arguments are required.

    In [10]: parser.parse_args('--list-methods'.split())
    ipython: error: too few arguments
    

    but

    In [11]: parser.parse_args('--list-methods 10 20'.split())
    Out[11]: Namespace(lat='10', list_methods=True, lon='20', method='add')
    

    You should make lat and lon arguments optional.

提交回复
热议问题