summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsteven.bethard <devnull@localhost>2009-09-13 00:01:52 +0000
committersteven.bethard <devnull@localhost>2009-09-13 00:01:52 +0000
commit6964050e4ae3122748b8fc207f1a44aa6a917d1a (patch)
tree0e9a26e017f433c4b0391e71daf8ad64c165dc41
parentd1ce37550e67bc777504f3cde9a2c2c5bd7951ec (diff)
downloadargparse-6964050e4ae3122748b8fc207f1a44aa6a917d1a.tar.gz
Fix a bug that was preventing % from being used in help messages.
-rw-r--r--argparse.py10
-rw-r--r--test/test_argparse.py34
2 files changed, 39 insertions, 5 deletions
diff --git a/argparse.py b/argparse.py
index 75a9043..ce1adb9 100644
--- a/argparse.py
+++ b/argparse.py
@@ -306,7 +306,7 @@ class HelpFormatter(object):
# Help-formatting methods
# =======================
def format_help(self):
- help = self._root_section.format_help() % dict(prog=self._prog)
+ help = self._root_section.format_help()
if help:
help = self._long_break_matcher.sub('\n\n', help)
help = help.strip('\n') + '\n'
@@ -321,9 +321,13 @@ class HelpFormatter(object):
if prefix is None:
prefix = _('usage: ')
+ # if usage is specified, use that
+ if usage is not None:
+ usage = usage % dict(prog=self._prog)
+
# if no optionals or positionals are available, usage is just prog
- if usage is None and not actions:
- usage = '%(prog)s'
+ elif usage is None and not actions:
+ usage = '%(prog)s' % dict(prog=self._prog)
# if optionals and positionals are available, calculate usage
elif usage is None:
diff --git a/test/test_argparse.py b/test/test_argparse.py
index 7536d5e..ba19835 100644
--- a/test/test_argparse.py
+++ b/test/test_argparse.py
@@ -3126,7 +3126,7 @@ class TestHelpVariableExpansion(HelpTestCase):
parser_signature = Sig(prog='PROG')
argument_signatures = [
Sig('-x', type='int',
- help='x %(prog)s %(default)s %(type)s'),
+ help='x %(prog)s %(default)s %(type)s %%'),
Sig('-y', action='store_const', default=42, const='XXX',
help='y %(prog)s %(default)s %(const)s'),
Sig('--foo', choices='abc',
@@ -3154,7 +3154,7 @@ class TestHelpVariableExpansion(HelpTestCase):
optional arguments:
-h, --help show this help message and exit
- -x X x PROG None int
+ -x X x PROG None int %
-y y PROG 42 XXX
--foo {a,b,c} foo PROG None a, b, c
--bar BBB bar PROG baz bar
@@ -3166,6 +3166,36 @@ class TestHelpVariableExpansion(HelpTestCase):
version = ''
+class TestHelpVariableExpansionUsageSupplied(HelpTestCase):
+ """Test that variables are expanded properly when usage= is present"""
+
+ parser_signature = Sig(prog='PROG', usage='%(prog)s FOO')
+ argument_signatures = []
+ argument_group_signatures = []
+ usage = ('''\
+ usage: PROG FOO
+ ''')
+ help = usage + '''\
+
+ optional arguments:
+ -h, --help show this help message and exit
+ '''
+ version = ''
+
+
+class TestHelpVariableExpansionNoArguments(HelpTestCase):
+ """Test that variables are expanded properly with no arguments"""
+
+ parser_signature = Sig(prog='PROG', add_help=False)
+ argument_signatures = []
+ argument_group_signatures = []
+ usage = ('''\
+ usage: PROG
+ ''')
+ help = usage
+ version = ''
+
+
class TestHelpSuppressUsage(HelpTestCase):
"""Test that items can be suppressed in usage messages"""