argparsing: Parser: allow to forward prog to argparse
Ref: https://github.com/pytest-dev/pytest-xdist/pull/388.
This commit is contained in:
parent
8152b6837e
commit
3445eae737
|
@ -18,6 +18,8 @@ class Parser(object):
|
|||
there's an error processing the command line arguments.
|
||||
"""
|
||||
|
||||
prog = None
|
||||
|
||||
def __init__(self, usage=None, processopt=None):
|
||||
self._anonymous = OptionGroup("custom options", parser=self)
|
||||
self._groups = []
|
||||
|
@ -82,7 +84,7 @@ class Parser(object):
|
|||
def _getparser(self):
|
||||
from _pytest._argcomplete import filescompleter
|
||||
|
||||
optparser = MyOptionParser(self, self.extra_info)
|
||||
optparser = MyOptionParser(self, self.extra_info, prog=self.prog)
|
||||
groups = self._groups + [self._anonymous]
|
||||
for group in groups:
|
||||
if group.options:
|
||||
|
@ -319,12 +321,13 @@ class OptionGroup(object):
|
|||
|
||||
|
||||
class MyOptionParser(argparse.ArgumentParser):
|
||||
def __init__(self, parser, extra_info=None):
|
||||
def __init__(self, parser, extra_info=None, prog=None):
|
||||
if not extra_info:
|
||||
extra_info = {}
|
||||
self._parser = parser
|
||||
argparse.ArgumentParser.__init__(
|
||||
self,
|
||||
prog=prog,
|
||||
usage=parser._usage,
|
||||
add_help=False,
|
||||
formatter_class=DropShorterLongHelpFormatter,
|
||||
|
|
|
@ -24,6 +24,12 @@ class TestParser(object):
|
|||
out, err = capsys.readouterr()
|
||||
assert err.find("error: unrecognized arguments") != -1
|
||||
|
||||
def test_custom_prog(self, parser):
|
||||
"""Custom prog can be set for `argparse.ArgumentParser`."""
|
||||
assert parser._getparser().prog == os.path.basename(sys.argv[0])
|
||||
parser.prog = "custom-prog"
|
||||
assert parser._getparser().prog == "custom-prog"
|
||||
|
||||
def test_argument(self):
|
||||
with pytest.raises(parseopt.ArgumentError):
|
||||
# need a short or long option
|
||||
|
|
Loading…
Reference in New Issue