summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsteven.bethard <devnull@localhost>2009-07-13 00:34:43 +0000
committersteven.bethard <devnull@localhost>2009-07-13 00:34:43 +0000
commit849841ad0021e632141f9c7353bd98264b193fe2 (patch)
tree44c108ca1d3acf39811906c48819e9eb26c77364
parentbe051b2738ef9e459651e3c75a24f0ce97374d0c (diff)
downloadargparse-849841ad0021e632141f9c7353bd98264b193fe2.tar.gz
Allow tuples for metavars.
-rw-r--r--argparse.py33
-rw-r--r--doc/source/add_argument.rst14
-rw-r--r--test/test_argparse.py27
3 files changed, 61 insertions, 13 deletions
diff --git a/argparse.py b/argparse.py
index 8ee556b..fab54e3 100644
--- a/argparse.py
+++ b/argparse.py
@@ -495,7 +495,8 @@ class HelpFormatter(object):
def _format_action_invocation(self, action):
if not action.option_strings:
- return self._format_metavar(action, action.dest)
+ metavar, = self._metavar_formatter(action, action.dest)(1)
+ return metavar
else:
parts = []
@@ -515,30 +516,36 @@ class HelpFormatter(object):
return ', '.join(parts)
- def _format_metavar(self, action, default_metavar):
+ def _metavar_formatter(self, action, default_metavar):
if action.metavar is not None:
- name = action.metavar
+ result = action.metavar
elif action.choices is not None:
choice_strs = [str(choice) for choice in action.choices]
- name = '{%s}' % ','.join(choice_strs)
+ result = '{%s}' % ','.join(choice_strs)
else:
- name = default_metavar
- return name
+ result = default_metavar
+ def format(tuple_size):
+ if isinstance(result, tuple):
+ return result
+ else:
+ return (result, ) * tuple_size
+ return format
def _format_args(self, action, default_metavar):
- name = self._format_metavar(action, default_metavar)
+ get_metavar = self._metavar_formatter(action, default_metavar)
if action.nargs is None:
- result = name
+ result = '%s' % get_metavar(1)
elif action.nargs == OPTIONAL:
- result = '[%s]' % name
+ result = '[%s]' % get_metavar(1)
elif action.nargs == ZERO_OR_MORE:
- result = '[%s [%s ...]]' % (name, name)
+ result = '[%s [%s ...]]' % get_metavar(2)
elif action.nargs == ONE_OR_MORE:
- result = '%s [%s ...]' % (name, name)
+ result = '%s [%s ...]' % get_metavar(2)
elif action.nargs is PARSER:
- result = '%s ...' % name
+ result = '%s ...' % get_metavar(1)
else:
- result = ' '.join([name] * action.nargs)
+ formats = ['%s' for _ in range(action.nargs)]
+ result = ' '.join(formats) % get_metavar(action.nargs)
return result
def _expand_help(self, action):
diff --git a/doc/source/add_argument.rst b/doc/source/add_argument.rst
index 667889f..43e1b29 100644
--- a/doc/source/add_argument.rst
+++ b/doc/source/add_argument.rst
@@ -378,6 +378,20 @@ If you would like to provide a different name for your argument in help messages
Note that ``metavar`` only changes the *displayed* name - the name of the attribute on the :meth:`parse_args` object is still determined by the dest_ value.
+Different values of ``nargs`` may cause the metavar to be used multiple times.
+If you'd like to specify a different display name for each of the arguments, you can provide a tuple to ``metavar``::
+
+ >>> parser = argparse.ArgumentParser(prog='PROG')
+ >>> parser.add_argument('-x', nargs=2)
+ >>> parser.add_argument('--foo', nargs=2, metavar=('bar', 'baz'))
+ >>> parser.print_help()
+ usage: PROG [-h] [-x X X] [--foo bar baz]
+
+ optional arguments:
+ -h, --help show this help message and exit
+ -x X X
+ --foo bar baz
+
dest
----
diff --git a/test/test_argparse.py b/test/test_argparse.py
index 67026d9..bd5a854 100644
--- a/test/test_argparse.py
+++ b/test/test_argparse.py
@@ -3080,6 +3080,33 @@ class TestHelpNone(HelpTestCase):
version = ''
+class TestHelpTupleMetavar(HelpTestCase):
+ """Test specifying metavar as a tuple"""
+
+ parser_signature = Sig(prog='PROG')
+ argument_signatures = [
+ Sig('-w', help='w', nargs='+', metavar=('W1', 'W2')),
+ Sig('-x', help='x', nargs='*', metavar=('X1', 'X2')),
+ Sig('-y', help='y', nargs=3, metavar=('Y1', 'Y2', 'Y3')),
+ Sig('-z', help='z', nargs='?', metavar=('Z1', )),
+ ]
+ argument_group_signatures = []
+ usage = '''\
+ usage: PROG [-h] [-w W1 [W2 ...]] [-x [X1 [X2 ...]]] [-y Y1 Y2 Y3] \
+[-z [Z1]]
+ '''
+ help = usage + '''\
+
+ optional arguments:
+ -h, --help show this help message and exit
+ -w W1 [W2 ...] w
+ -x [X1 [X2 ...]] x
+ -y Y1 Y2 Y3 y
+ -z [Z1] z
+ '''
+ version = ''
+
+
class TestHelpRawText(HelpTestCase):
"""Test the RawTextHelpFormatter"""