Matt Joiner
Matt Joiner

Reputation: 118600

Split shell-like syntax in Haskell?

How can I split a string in shell-style syntax in Haskell? The equivalent in Python is shlex.split.

>>> shlex.split('''/nosuchconf "/this doesn't exist either" "yep"''')
['/nosuchconf', "/this doesn't exist either", 'yep']

Upvotes: 3

Views: 559

Answers (2)

L29Ah
L29Ah

Reputation: 298

https://hackage.haskell.org/package/shellwords

λ> import ShellWords
λ> parse "/nosuchconf \"/this doesn't exist either\" \"yep\""
Right ["/nosuchconf","/this doesn't exist either","yep"]

Upvotes: 0

ivanm
ivanm

Reputation: 3927

I'm not sure what exactly you mean: are you wanting to get get all quoted sub-strings from a String? Note that unlike Python, etc. Haskell only has one set of quotes that indicate something is a String, namely "...".

Possibilities to consider:

  • The words and lines functions

  • The split package

  • Write a custom parser using polyparse, uu-parsinglib, parsec, etc.

It may be useful if you specified why you wanted such functionality: are you trying to parse existing shell scripts? Then language-sh might be of use. But you shouldn't be using such Strings internally in Haskell, and instead using [String] or something.

Upvotes: 1

Related Questions