Getting the remaining arguments in argparse

后端 未结 4 866
一个人的身影
一个人的身影 2021-01-30 16:08

I want to get all the remaining unused arguments at once. How do I do it?

parser.add_argument(\'-i\', action=\'store\', dest=\'i\', default=\'i.log\')
parser.add         


        
4条回答
  •  忘掉有多难
    2021-01-30 16:43

    Another option is to add a positional argument to your parser. Specify the option without leading dashes, and argparse will look for them when no other option is recognized. This has the added benefit of improving the help text for the command:

    >>> parser.add_argument('otherthings', nargs='*')
    >>> parser.parse_args(['foo', 'bar', 'baz'])
    Namespace(i='i.log', o='o.log', otherthings=['foo', 'bar', 'baz'])
    

    and

    >>> print parser.format_help()
    usage: ipython-script.py [-h] [-i I] [-o O] [otherthings [otherthings ...]]
    
    positional arguments:
      otherthings
    
    optional arguments:
      -h, --help   show this help message and exit
      -i I
      -o O
    

提交回复
热议问题