Skip to content
Advertisement

Parsing command-line arguments from a STRING in Clojure

I’m in a situation where I need to parse arguments from a string in the same way that they would be parsed if provided on the command-line to a Java/Clojure application.

For example, I need to turn "foo "bar baz" 'fooy barish' foo" into ("foo" "bar baz" "fooy barish" "foo").

I’m curious if there is a way to use the parser that Java or Clojure uses to do this. I’m not opposed to using a regex, but I suck at regexes, and I’d fail hard if I tried to write one for this.

Any ideas?

Advertisement

Answer

Updated with a new, even more convoluted version. This is officially ridiculous; the next iteration will use a proper parser (or c.c.monads and a little bit of Parsec-like logic on top of that). See the revision history on this answer for the original.

This convoluted bunch of functions seems to do the trick (not at my DRYest with this one, sorry!):

JavaScript

Somewhat encouragingly, the following yields true:

JavaScript

So does this:

JavaScript

Hopefully this should trim regular arguments, but not ones surrounded with quotes, handle double and single quotes, including quoted double quotes inside unquoted double quotes (note that it currently treats quoted single quotes inside unquoted single quotes in the same way, which is apparently at variance with the *nix shell way… argh) etc. Note that it’s basically a computation in an ad-hoc state monad, just written in a particularly ugly way and in a dire need of DRYing up. 😛

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement