Skip to content

Commit c969a4c

Browse files
committed
Better test completion and everything that can be is columnated
1 parent 91c45a7 commit c969a4c

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

tools/tests.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from tools.paths import *
1818
from tools.data.support import *
1919
from argparse import ArgumentTypeError
20+
from utils import columnate
2021

2122
try:
2223
import tools.private_settings as ps
@@ -1223,14 +1224,14 @@ def __getitem__(self, key):
12231224
def test_known(string):
12241225
i = int(string)
12251226
if i >= 0 and i < len(TESTS) : return i
1226-
else : raise ArgumentTypeError("{0} does not index a test. The accepted range is 0 to {1}".format(i, len(TEST_MAP) - 1))
1227+
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)])))
12271228

12281229
def test_name_known(string):
12291230
nlist = string.split(',')
12301231
for test_id in nlist:
12311232
if test_id not in TEST_MAP.keys():
12321233
if getattr(ps, "test_alias", None) is None or \
12331234
ps.test_alias.get(test_id, "") not in TEST_MAP.keys():
1234-
raise ArgumentTypeError("Program with name '{0}' not found. Supported tests are {1}".format(test_id, ", ".join(TEST_MAP.keys())))
1235+
raise ArgumentTypeError("Program with name '{0}' not found. Supported tests are: \n{1}".format(test_id, columnate([t['id'] for t in TESTS])))
12351236

12361237
return [TEST_MAP[n].n for n in nlist]

tools/utils.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import inspect
1919
import os
2020
import argparse
21+
import math
2122
from os import listdir, remove, makedirs
2223
from shutil import copyfile
2324
from os.path import isdir, join, exists, split, relpath, splitext
@@ -209,7 +210,7 @@ def parse_type(string):
209210
elif string not in list and newstring in list:
210211
raise argparse.ArgumentTypeError("{0} is not a supported {1}. Did you mean {2}?".format(string, type_name, newstring))
211212
else:
212-
raise argparse.ArgumentTypeError("{0} is not a supported {1}. Supported {1}s are {2}.".format(string, type_name, ", ".join(list)))
213+
raise argparse.ArgumentTypeError("{0} is not a supported {1}. Supported {1}s are:\n{2}".format(string, type_name, columnate(list)))
213214
return parse_type
214215
return middle
215216

@@ -226,3 +227,19 @@ def wrap(string):
226227
def argparse_filestring_type(string) :
227228
if exists(string) : return string
228229
else : raise argparse.ArgumentTypeError("{0} does not exist in the filesystem.".format(string))
230+
231+
def columnate(strings, seperator=", ", chars=80):
232+
col_width = max(len(s) for s in strings)
233+
total_width = col_width + len(seperator)
234+
columns = math.floor(chars / total_width)
235+
output = ""
236+
for i, s in zip(range(len(strings)), strings):
237+
append = s
238+
if i != len(strings) - 1:
239+
append += seperator
240+
if i % columns == columns - 1:
241+
append += "\n"
242+
else:
243+
append = append.ljust(total_width)
244+
output += append
245+
return output

0 commit comments

Comments
 (0)