Skip to content

Instantly share code, notes, and snippets.

@truemped
Forked from sw1nn/parse-arg.clj
Created October 24, 2015 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save truemped/c4bc3d5efacb81825967 to your computer and use it in GitHub Desktop.
Save truemped/c4bc3d5efacb81825967 to your computer and use it in GitHub Desktop.
;; Based on https://clojuredocs.org/clojure.core/condp#example-542692cbc026201cdc326bea
(defn parse-arg [s]
(condp (comp next re-matches) s
#"([+-])(\w+)" :>> (fn [[f k]] [(keyword k) (= f "+")])
#"(\w+)=(\w+)" :>> (fn [[k v]] [(keyword k) v])
(throw (IllegalArgumentException. s))))
(parse-arg "+foo") ;=> [:foo true]
(parse-arg "-foo") ;=> [:foo false]
(parse-arg "foo=bar") ;=> [:foo "bar"]
(->> ["+foo" "-bar" "baz=quuz"]
(map parse-arg)
(into {})) ;=> {:foo true, :bar false, :baz "quuz"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment