summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/optparse.py11
-rw-r--r--Lib/test/test_optparse.py16
2 files changed, 24 insertions, 3 deletions
diff --git a/Lib/optparse.py b/Lib/optparse.py
index bacef0f..d3593d3 100644
--- a/Lib/optparse.py
+++ b/Lib/optparse.py
@@ -1025,6 +1025,11 @@ class OptionParser (OptionContainer):
self.largs = None
self.values = None
+ def _get_prog_name(self):
+ if self.prog:
+ return self.prog
+ else:
+ return get_prog_name()
# -- Simple modifier methods ---------------------------------------
@@ -1288,12 +1293,12 @@ class OptionParser (OptionContainer):
should either exit or raise an exception.
"""
self.print_usage(sys.stderr)
- sys.exit("%s: error: %s" % (get_prog_name(), msg))
+ sys.exit("%s: error: %s" % (self._get_prog_name(), msg))
def get_usage (self):
if self.usage:
return self.formatter.format_usage(
- self.usage.replace("%prog", get_prog_name()))
+ self.usage.replace("%prog", self._get_prog_name()))
else:
return ""
@@ -1311,7 +1316,7 @@ class OptionParser (OptionContainer):
def get_version (self):
if self.version:
- return self.version.replace("%prog", get_prog_name())
+ return self.version.replace("%prog", self._get_prog_name())
else:
return ""
diff --git a/Lib/test/test_optparse.py b/Lib/test/test_optparse.py
index 4712446..37e229c 100644
--- a/Lib/test/test_optparse.py
+++ b/Lib/test/test_optparse.py
@@ -612,6 +612,22 @@ class TestVersion(BaseTest):
self.assertStdoutEquals(["--version"], "bar 0.1\n")
sys.argv[0] = oldargv
+ def test_version_with_prog_keyword(self):
+ oldargv = sys.argv[0]
+ sys.argv[0] = "./foo/bar"
+ self.parser = OptionParser(usage=SUPPRESS_USAGE, version="%prog 0.1",
+ prog="splat")
+ self.assertStdoutEquals(["--version"], "splat 0.1\n")
+ sys.argv[0] = oldargv
+
+ def test_version_with_prog_attribute(self):
+ oldargv = sys.argv[0]
+ sys.argv[0] = "./foo/bar"
+ self.parser = OptionParser(usage=SUPPRESS_USAGE, version="%prog 0.1")
+ self.parser.prog = "splat"
+ self.assertStdoutEquals(["--version"], "splat 0.1\n")
+ sys.argv[0] = oldargv
+
def test_no_version(self):
self.parser = OptionParser(usage=SUPPRESS_USAGE)
self.assertParseFail(["--version"],