Skip to content

Commit de3ca65

Browse files
authored
Suggestion from Phil Tooley: Fail on non-ASCII inputs (#198)
1 parent e34b924 commit de3ca65

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

mlc/main.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,30 @@ def build_run_args(args):
200200

201201
return run_args
202202

203+
def is_quoted(arg):
204+
return (arg.startswith("'") and arg.endswith("'")) or \
205+
(arg.startswith('"') and arg.endswith('"'))
206+
207+
def check_raw_arguments_for_non_ascii():
208+
bad_args = []
209+
210+
# Skip sys.argv[0] (script name)
211+
for arg in sys.argv[1:]:
212+
if is_quoted(arg):
213+
continue # allow non-ASCII inside quotes
214+
for ch in arg:
215+
if ord(ch) > 127: # non-ASCII
216+
bad_args.append((arg, ch, unicodedata.name(ch, "UNKNOWN")))
217+
break # report each arg once
218+
219+
if bad_args:
220+
print("\n⚠️ ERROR: Non-ASCII characters detected in command-line arguments!\n")
221+
for arg, ch, name in bad_args:
222+
print(f" → Argument: {arg}")
223+
print(f" Contains non-ASCII character: '{ch}' ({name})")
224+
print("\nThis often happens due to copy-paste from PDFs or documents.\n"
225+
"Please retype the arguments using plain ASCII.\n")
226+
sys.exit(1)
203227

204228
def main():
205229
"""
@@ -239,6 +263,9 @@ def main():
239263
mlc run script --help
240264
mlc pull repo -h
241265
"""
266+
267+
check_raw_arguments_for_non_ascii()
268+
242269
pre_parser = build_pre_parser()
243270
pre_args, remaining_args = pre_parser.parse_known_args()
244271

0 commit comments

Comments
 (0)