0

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']
Att Righ
  • 1,439
  • 1
  • 16
  • 29
  • What is the expected output? For input, I see a command name `python3` with a 4-character argument consisting of a linefeed, an `a`, and `r`, and a `g`, so `shlex.split` is parsing it correctly. – chepner May 16 '22 at 15:57
  • You can specify multiline arguments enclosed in quotes. – Pranav Hosangadi May 16 '22 at 15:57
  • @chepner I've added more context above. I want `subproces.check_call(shlex.split(x))` to do the same thing as bash would. – Att Righ May 16 '22 at 16:01
  • 1
    `shlex` is for *lexical* analysis, not full-blown parsing. If you can't get an appropriate command list in the first place, just use `shell=True` instead. – chepner May 16 '22 at 16:05
  • A question isn't wrong just because you can't instantly answer it chepner! I am happy to use another tool, I would prefer to reuse code if it exists. I want to parse the command and understand it's meaning, not execute it. – Att Righ May 16 '22 at 16:09

1 Answers1

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.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Yeah, that's what I tried. But I was hoping there was a library that could magically do it wo something. Unfortunately I then had problems with c-style strings (https://unix.stackexchange.com/questions/155367/when-to-use-bash-ansi-c-style-escape-e-g-n), so had to use a completely different approach to the problems. – Att Righ May 17 '22 at 09:34