Skip to content
This repository has been archived by the owner on May 3, 2023. It is now read-only.

Commit

Permalink
add rudimentary cli arg parsing (env style only)
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepodmatt committed Nov 6, 2014
1 parent 3793620 commit 36b3eba
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/clj_configurator/core.clj
Expand Up @@ -11,6 +11,17 @@

(def props (Props.))

(deftype Args [arg-map] clojure.lang.ILookup
(valAt [_ k] (lookup-variants #(get arg-map %) (name k))))

(defn from-args [args]
(let [arg-map (into {} (map (fn [arg]
(let [parts (.split arg "=" 2)
arg-key (first parts)
arg-val (or (second parts) "")]
[arg-key arg-val])) args))]
(Args. arg-map)))

(defn- process-tree [m sources a]
(into {}
(map (fn [[k v]]
Expand Down
20 changes: 20 additions & 0 deletions test/clj_configurator/core_test.clj
Expand Up @@ -28,3 +28,23 @@
=> {:a 2
:b "b"
:c "123"})

(fact "rudimentary command line argument (env style) parsing works"
(let [raw-args ["PORT=3000"
"DEBUG=true"
"PATH=ws/project"
"EMPTY_PERSISTS="
"DOUBLE_EQUALS=1+2=3"]]
(config
:sources [(from-args raw-args)] ; pass -main args in normally
:defaults {:port 9999
:debug false
:path ""
:empty-persists "default"
:double-equals ""
}) => {:port 3000
:debug true
:path "ws/project"
:empty-persists ""
:double-equals "1+2=3"
}))

0 comments on commit 36b3eba

Please sign in to comment.