Skip to content

Commit

Permalink
argparsing: Parser: allow to forward prog to argparse
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Dec 11, 2018
1 parent 26a9d23 commit 40c2d82
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/_pytest/config/argparsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -82,7 +84,7 @@ def parse(self, args, namespace=None):
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:
Expand Down Expand Up @@ -319,12 +321,13 @@ def _addoption_instance(self, option, shortupper=False):


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,
Expand Down
6 changes: 6 additions & 0 deletions testing/test_parseopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def test_no_help_by_default(self, capsys):
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
Expand Down

0 comments on commit 40c2d82

Please sign in to comment.