Skip to content

Commit 6ef5e3a

Browse files
committed
Add a more terse error message for arg validation
"--std" and "-p" will now print a short message instead of a complete Python stacktrace when the argument doesn't pass validation. Fixes: #130
1 parent 16236c8 commit 6ef5e3a

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

cmake-init/cmake_init.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,26 @@ def not_empty(value):
5757
return len(value) != 0
5858

5959

60+
class ArgumentError(Exception):
61+
pass
62+
63+
6064
def prompt(msg, default, mapper=None, predicate=not_empty, header=None, no_prompt=False):
6165
if header is not None:
6266
print(header)
6367
while True:
6468
# noinspection PyBroadException
6569
try:
6670
print(msg.format(default), end=": ")
67-
value = ("" if no_prompt else input()) or default
68-
if mapper is not None:
69-
value = mapper(value)
71+
in_value = ("" if no_prompt else input()) or default
72+
value = mapper(value) if mapper is not None else in_value
7073
if predicate(value):
7174
print()
7275
return value
7376
except Exception:
7477
pass
7578
if no_prompt:
76-
raise ValueError()
79+
raise ArgumentError(f"'{in_value}' is not an acceptable value")
7780
print("Invalid value, try again")
7881

7982

@@ -349,11 +352,15 @@ def create(args, zip):
349352
file=sys.stderr,
350353
)
351354
exit(1)
352-
if args.flags_used:
353-
with contextlib.redirect_stdout(io.StringIO()):
355+
try:
356+
if args.flags_used:
357+
with contextlib.redirect_stdout(io.StringIO()):
358+
d = get_substitutes(args, os.path.basename(path))
359+
else:
354360
d = get_substitutes(args, os.path.basename(path))
355-
else:
356-
d = get_substitutes(args, os.path.basename(path))
361+
except ArgumentError as e:
362+
print(str(e), file=sys.stderr)
363+
exit(1)
357364
mkdir(path)
358365
mapping = {"e": "executable/", "h": "header/", "s": "shared/"}
359366
zip_paths = [("c/" if d["c"] else "") + mapping[d["type_id"]], "common/"]

0 commit comments

Comments
 (0)