Skip to content

Commit f810157

Browse files
committed
Scrub bogus spaces and unicode from command line arguments.
1 parent 355bc87 commit f810157

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

e4s_cl/cli/commands/__main__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from e4s_cl.cli import UnknownCommandError, arguments
2020
from e4s_cl.cli.command import AbstractCommand
2121
from e4s_cl.cli.commands.launch import COMMAND as LAUNCH_COMMAND
22+
from e4s_cl.util import sanitize_args
2223

2324
LOGGER = logger.get_logger(__name__)
2425

@@ -215,6 +216,7 @@ def main(self, argv):
215216
else:
216217
parse_method = self._py39_parse
217218

219+
argv = sanitize_args(argv)
218220
args = parse_method(argv)
219221

220222
self.set_log_level(args)

e4s_cl/util.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from pathlib import Path
1313
from hashlib import sha256
1414
import json
15+
import unicodedata
1516
from typing import (
1617
Any,
1718
Callable,
@@ -583,3 +584,23 @@ def prepend_library_path(path: Union[str, Path]):
583584
"""
584585
env = os.environ.get("LD_LIBRARY_PATH", [])
585586
os.environ["LD_LIBRARY_PATH"] = os.pathsep.join([str(path), *env])
587+
588+
589+
def sanitize_args(argv: List[str]) -> List[str]:
590+
"""
591+
Cleans a list of command-line arguments by normalizing Unicode characters
592+
and collapsing all whitespace to a single space.
593+
"""
594+
sanitized_argv = []
595+
for arg in argv:
596+
if isinstance(arg, str):
597+
# NFKC normalization
598+
normalized_str = unicodedata.normalize('NFKC', arg)
599+
# Collapse all whitespace and strip ends
600+
sanitized_arg = ' '.join(normalized_str.split())
601+
sanitized_argv.append(sanitized_arg)
602+
else:
603+
# Preserve non-string arguments
604+
sanitized_argv.append(arg)
605+
606+
return sanitized_argv

0 commit comments

Comments
 (0)