Merge pull request #4528 from blueyed/parser-prog

argparsing: Parser: allow to forward prog to argparse
This commit is contained in:
Daniel Hahler 2018-12-11 13:33:40 +01:00 committed by GitHub
commit bb363c8ff2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View File

@ -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,

View File

@ -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