Split shell-like syntax in Haskell?

564 Views Asked by At

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

There are 2 best solutions below

4
On

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.

0
On

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

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