On Mar 9, 10:48 am, timc <timgcl...@gmail.com> wrote:
> Can someone please see what's wrong with this.
>
> (defn getArg [arg]
> "Return a [keyword value] depending on the form of the string arg:
> 1. arg is of the form +x ==> [:x true].
> 2. arg is of the form -x ==> [:x false].
> 3. arg is of the form x=v ==> [:x v].
> 4. else ==> [:files arg]."
> (let [x (or
> (re-seq #"([+])([a-zA-Z_0-9]+)" arg)
> (re-seq #"([-])([a-zA-Z_0-9]+)" arg)
> (re-seq #"([a-zA-Z_0-9]+)=([^ ]+)" arg))]
> (if (nil? x) [:files arg]
> (let [x (first x)
> y (nth x 1)
> z (nth x 2)]
> (cond
> (= y "+") [(keyword z) true]
> (= y "-") [(keyword z) false]
> :else [(keyword y) z])))))
>
(defn get-arg [arg]
(let [[_ prefix word] (re-find #"^([+-])(.*)$" arg)]
(if word
[(keyword word) (= prefix "+")]
(let [[_ left right] (re-find #"^(\w*)=(\S+)$" arg)]
(if right
[(keyword left) right]
[:files arg])))))
(prn (get-arg "+foo"))
(prn (get-arg "-foo"))
(prn (get-arg "foo=bar"))
(prn (get-arg "foo-bar"))
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---