Merge pull request #4528 from blueyed/parser-prog
argparsing: Parser: allow to forward prog to argparse
This commit is contained in:
commit
bb363c8ff2
|
@ -18,6 +18,8 @@ class Parser(object):
|
||||||
there's an error processing the command line arguments.
|
there's an error processing the command line arguments.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
prog = None
|
||||||
|
|
||||||
def __init__(self, usage=None, processopt=None):
|
def __init__(self, usage=None, processopt=None):
|
||||||
self._anonymous = OptionGroup("custom options", parser=self)
|
self._anonymous = OptionGroup("custom options", parser=self)
|
||||||
self._groups = []
|
self._groups = []
|
||||||
|
@ -82,7 +84,7 @@ class Parser(object):
|
||||||
def _getparser(self):
|
def _getparser(self):
|
||||||
from _pytest._argcomplete import filescompleter
|
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]
|
groups = self._groups + [self._anonymous]
|
||||||
for group in groups:
|
for group in groups:
|
||||||
if group.options:
|
if group.options:
|
||||||
|
@ -319,12 +321,13 @@ class OptionGroup(object):
|
||||||
|
|
||||||
|
|
||||||
class MyOptionParser(argparse.ArgumentParser):
|
class MyOptionParser(argparse.ArgumentParser):
|
||||||
def __init__(self, parser, extra_info=None):
|
def __init__(self, parser, extra_info=None, prog=None):
|
||||||
if not extra_info:
|
if not extra_info:
|
||||||
extra_info = {}
|
extra_info = {}
|
||||||
self._parser = parser
|
self._parser = parser
|
||||||
argparse.ArgumentParser.__init__(
|
argparse.ArgumentParser.__init__(
|
||||||
self,
|
self,
|
||||||
|
prog=prog,
|
||||||
usage=parser._usage,
|
usage=parser._usage,
|
||||||
add_help=False,
|
add_help=False,
|
||||||
formatter_class=DropShorterLongHelpFormatter,
|
formatter_class=DropShorterLongHelpFormatter,
|
||||||
|
|
|
@ -24,6 +24,12 @@ class TestParser(object):
|
||||||
out, err = capsys.readouterr()
|
out, err = capsys.readouterr()
|
||||||
assert err.find("error: unrecognized arguments") != -1
|
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):
|
def test_argument(self):
|
||||||
with pytest.raises(parseopt.ArgumentError):
|
with pytest.raises(parseopt.ArgumentError):
|
||||||
# need a short or long option
|
# need a short or long option
|
||||||
|
|
Loading…
Reference in New Issue