'Python parse multiline shell commands?
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']
Solution 1:[1]
Is there an idiomatic way
No.
Python parse multiline shell commands?
Regex replace a slash followed a newline for a whitespace. Something along re.sub(r"\\\n", r" ", ... or similar.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | KamilCuk |
