Minor argparse improvement.
This commit is contained in:
parent
1b67c8493e
commit
a8ed1ec414
6
lib/spack/external/argparse.py
vendored
6
lib/spack/external/argparse.py
vendored
@ -108,6 +108,8 @@
|
|||||||
import sys as _sys
|
import sys as _sys
|
||||||
import textwrap as _textwrap
|
import textwrap as _textwrap
|
||||||
|
|
||||||
|
from llnl.util.tty.colify import colified
|
||||||
|
|
||||||
from gettext import gettext as _
|
from gettext import gettext as _
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -2285,8 +2287,8 @@ def _get_value(self, action, arg_string):
|
|||||||
def _check_value(self, action, value):
|
def _check_value(self, action, value):
|
||||||
# converted value must be one of the choices (if specified)
|
# converted value must be one of the choices (if specified)
|
||||||
if action.choices is not None and value not in action.choices:
|
if action.choices is not None and value not in action.choices:
|
||||||
tup = value, ', '.join(map(repr, action.choices))
|
cols = colified(sorted(action.choices), indent=4, tty=True)
|
||||||
msg = _('invalid choice: %r (choose from %s)') % tup
|
msg = _('invalid choice: %r choose from:\n%s') % (value, cols)
|
||||||
raise ArgumentError(action, msg)
|
raise ArgumentError(action, msg)
|
||||||
|
|
||||||
# =======================
|
# =======================
|
||||||
|
@ -37,9 +37,11 @@
|
|||||||
import fcntl
|
import fcntl
|
||||||
import termios
|
import termios
|
||||||
import struct
|
import struct
|
||||||
|
from StringIO import StringIO
|
||||||
|
|
||||||
from llnl.util.tty import terminal_size
|
from llnl.util.tty import terminal_size
|
||||||
|
|
||||||
|
|
||||||
class ColumnConfig:
|
class ColumnConfig:
|
||||||
def __init__(self, cols):
|
def __init__(self, cols):
|
||||||
self.cols = cols
|
self.cols = cols
|
||||||
@ -102,16 +104,18 @@ def colify(elts, **options):
|
|||||||
output = options.get("output", sys.stdout)
|
output = options.get("output", sys.stdout)
|
||||||
indent = options.get("indent", 0)
|
indent = options.get("indent", 0)
|
||||||
padding = options.get("padding", 2)
|
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 needs to be an array of strings so we can count the elements
|
||||||
elts = [str(elt) for elt in elts]
|
elts = [str(elt) for elt in elts]
|
||||||
if not elts:
|
if not elts:
|
||||||
return
|
return
|
||||||
|
|
||||||
if not isatty(output):
|
if not tty:
|
||||||
for elt in elts:
|
if tty is False or not isatty(output):
|
||||||
output.write("%s\n" % elt)
|
for elt in elts:
|
||||||
return
|
output.write("%s\n" % elt)
|
||||||
|
return
|
||||||
|
|
||||||
console_cols = options.get("cols", None)
|
console_cols = options.get("cols", None)
|
||||||
if not console_cols:
|
if not console_cols:
|
||||||
@ -147,6 +151,15 @@ def colify(elts, **options):
|
|||||||
cols -= 1
|
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__":
|
if __name__ == "__main__":
|
||||||
import optparse
|
import optparse
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user