Att Righ
Att Righ

Reputation: 1799

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']

Upvotes: 0

Views: 379

Answers (1)

KamilCuk
KamilCuk

Reputation: 141493

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.

Upvotes: 1

Related Questions