Python provides the library shlex
to parse shell commands. This appears not to parse multiline strings.
The shell command
python3 \
arg
is equivalent to subproces.check_call(["python3", "arg"])
However, shlex.split
appends to append a newline to the argument.
>>> shlex.split("python3 \\\narg")
['python3', '\narg']
Is there an idiomatic way to parse multiline shell commands.
Approaches tried
bashlex is a more general version of shlex
. It does a slightly better job.
>>> list(bashlex.split("python3 \\\narg"))
['python3', '\n', 'arg']
No.
Regex replace a slash followed a newline for a whitespace. Something along
re.sub(r"\\\n", r" ", ...
or similar.