diff options
author | Todd Gamblin <tgamblin@llnl.gov> | 2014-09-29 20:00:00 -0700 |
---|---|---|
committer | Todd Gamblin <tgamblin@llnl.gov> | 2014-09-29 20:00:00 -0700 |
commit | a8ed1ec414c678683628a603a1bb3c76d1319ea6 (patch) | |
tree | 9167bcf6818fb9e64ce307d83a85330f2aee0a3a /lib | |
parent | 1b67c8493e2037bd3226bafbf77f0ee622183a0a (diff) | |
download | spack-a8ed1ec414c678683628a603a1bb3c76d1319ea6.tar.gz spack-a8ed1ec414c678683628a603a1bb3c76d1319ea6.tar.bz2 spack-a8ed1ec414c678683628a603a1bb3c76d1319ea6.tar.xz spack-a8ed1ec414c678683628a603a1bb3c76d1319ea6.zip |
Minor argparse improvement.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/spack/external/argparse.py | 6 | ||||
-rw-r--r-- | lib/spack/llnl/util/tty/colify.py | 21 |
2 files changed, 21 insertions, 6 deletions
diff --git a/lib/spack/external/argparse.py b/lib/spack/external/argparse.py index c8dfdd3bed..42b64ee7be 100644 --- a/lib/spack/external/argparse.py +++ b/lib/spack/external/argparse.py @@ -108,6 +108,8 @@ import re as _re import sys as _sys import textwrap as _textwrap +from llnl.util.tty.colify import colified + from gettext import gettext as _ try: @@ -2285,8 +2287,8 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): def _check_value(self, action, value): # converted value must be one of the choices (if specified) if action.choices is not None and value not in action.choices: - tup = value, ', '.join(map(repr, action.choices)) - msg = _('invalid choice: %r (choose from %s)') % tup + cols = colified(sorted(action.choices), indent=4, tty=True) + msg = _('invalid choice: %r choose from:\n%s') % (value, cols) raise ArgumentError(action, msg) # ======================= diff --git a/lib/spack/llnl/util/tty/colify.py b/lib/spack/llnl/util/tty/colify.py index 1b04f1012f..5586b2681a 100644 --- a/lib/spack/llnl/util/tty/colify.py +++ b/lib/spack/llnl/util/tty/colify.py @@ -37,9 +37,11 @@ import sys import fcntl import termios import struct +from StringIO import StringIO from llnl.util.tty import terminal_size + class ColumnConfig: def __init__(self, cols): self.cols = cols @@ -102,16 +104,18 @@ def colify(elts, **options): output = options.get("output", sys.stdout) indent = options.get("indent", 0) padding = options.get("padding", 2) + tty = options.get('tty', None) # elts needs to be an array of strings so we can count the elements elts = [str(elt) for elt in elts] if not elts: return - if not isatty(output): - for elt in elts: - output.write("%s\n" % elt) - return + if not tty: + if tty is False or not isatty(output): + for elt in elts: + output.write("%s\n" % elt) + return console_cols = options.get("cols", None) if not console_cols: @@ -147,6 +151,15 @@ def colify(elts, **options): cols -= 1 +def colified(elts, **options): + """Invokes the colify() function but returns the result as a string + instead of writing it to an output string.""" + sio = StringIO() + options['output'] = sio + colify(elts, **options) + return sio.getvalue() + + if __name__ == "__main__": import optparse |