diff --git a/tools/tests.py b/tools/tests.py index 687a2d649c..4164d45202 100644 --- a/tools/tests.py +++ b/tools/tests.py @@ -17,6 +17,7 @@ limitations under the License. from tools.paths import * from tools.data.support import * from argparse import ArgumentTypeError +from utils import columnate try: import tools.private_settings as ps @@ -1223,7 +1224,7 @@ TEST_MAP = dict([(test['id'], Test(i)) for i, test in enumerate(TESTS)]) def test_known(string): i = int(string) if i >= 0 and i < len(TESTS) : return i - else : raise ArgumentTypeError("{0} does not index a test. The accepted range is 0 to {1}".format(i, len(TEST_MAP) - 1)) + else : raise ArgumentTypeError("{0} does not index a test. The accepted range is 0 to {1}\nThe test mapping is:\n{2}".format(i, len(TEST_MAP) - 1, columnate([str(i) + ":" + t['id'] for i,t in zip(range(len(TESTS)), TESTS)]))) def test_name_known(string): nlist = string.split(',') @@ -1231,6 +1232,6 @@ def test_name_known(string): if test_id not in TEST_MAP.keys(): if getattr(ps, "test_alias", None) is None or \ ps.test_alias.get(test_id, "") not in TEST_MAP.keys(): - raise ArgumentTypeError("Program with name '{0}' not found. Supported tests are {1}".format(test_id, ", ".join(TEST_MAP.keys()))) + raise ArgumentTypeError("Program with name '{0}' not found. Supported tests are: \n{1}".format(test_id, columnate([t['id'] for t in TESTS]))) return [TEST_MAP[n].n for n in nlist] diff --git a/tools/utils.py b/tools/utils.py index c2c2f7141f..6ffca38c9f 100644 --- a/tools/utils.py +++ b/tools/utils.py @@ -18,6 +18,7 @@ import sys import inspect import os import argparse +import math from os import listdir, remove, makedirs from shutil import copyfile from os.path import isdir, join, exists, split, relpath, splitext @@ -209,7 +210,7 @@ def argparse_type(casedness, prefer_hyphen=False) : elif string not in list and newstring in list: raise argparse.ArgumentTypeError("{0} is not a supported {1}. Did you mean {2}?".format(string, type_name, newstring)) else: - raise argparse.ArgumentTypeError("{0} is not a supported {1}. Supported {1}s are {2}.".format(string, type_name, ", ".join(list))) + raise argparse.ArgumentTypeError("{0} is not a supported {1}. Supported {1}s are:\n{2}".format(string, type_name, columnate(list))) return parse_type return middle @@ -226,3 +227,19 @@ def argparse_many(fn): def argparse_filestring_type(string) : if exists(string) : return string else : raise argparse.ArgumentTypeError("{0} does not exist in the filesystem.".format(string)) + +def columnate(strings, seperator=", ", chars=80): + col_width = max(len(s) for s in strings) + total_width = col_width + len(seperator) + columns = math.floor(chars / total_width) + output = "" + for i, s in zip(range(len(strings)), strings): + append = s + if i != len(strings) - 1: + append += seperator + if i % columns == columns - 1: + append += "\n" + else: + append = append.ljust(total_width) + output += append + return output