diff --git a/src/_pytest/config/argparsing.py b/src/_pytest/config/argparsing.py index 5b8306dda..51f708335 100644 --- a/src/_pytest/config/argparsing.py +++ b/src/_pytest/config/argparsing.py @@ -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, diff --git a/testing/test_parseopt.py b/testing/test_parseopt.py index 3048c96bd..c3b4ee698 100644 --- a/testing/test_parseopt.py +++ b/testing/test_parseopt.py @@ -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