Created
May 2, 2010 12:36
-
-
Save raek/387097 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn- re-clause-to-if [s [re bindings body] else] | |
`(if-let [[~'_ ~@bindings] (re-find ~re ~s)] | |
~body | |
~else)) | |
(defn- re-clauses-to-ifs [s clauses] | |
(when-first [clause clauses] | |
(if (and (not (next clauses)) | |
(= (count clause) 1)) | |
(first clause) | |
(re-clause-to-if s clause (re-clauses-to-ifs s (rest clauses)))))) | |
(defmacro cond-re [s & clauses] | |
(re-clauses-to-ifs s (partition 3 3 nil clauses))) | |
(defn parse-flag [s] | |
(cond-re s | |
#"^--([^=]+)=(.*)" [flag value] [flag value] | |
#"^--(.*)" [flag] [flag nil])) | |
(comment | |
(parse-flag "--foo") | |
=> ["foo" nil] | |
(parse-flag "--foo=bar") | |
=> ["foo" "bar"] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment