Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 02b1cc3

Browse files
add a parser to the arg specs
1 parent 80bda63 commit 02b1cc3

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

src/drift/args.clj

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
"given a matched option and whatever is after it in the arg list, remove the option
1111
and return [{opt-key opt-value} remaining-args]"
1212
[[switch val & rest :as args] spec]
13-
(if (and switch val)
14-
[{(:key spec) val} rest]
15-
[{} args]))
16-
13+
(let [parser (or (:parser spec) identity)]
14+
(if (and switch val)
15+
[{(:key spec) (parser val)} rest]
16+
[{} args])))
1717

1818
(defn parse-args
1919
"do a partial parse of args... removing only options we know about and leaving everything else to
@@ -31,15 +31,17 @@
3131
[{:key :version
3232
:matcher #{"-v" "-version" "--version"}}
3333
{:key :config
34-
:matcher #{"-c" "-config" "--config"}}])
34+
:matcher #{"-c" "-config" "--config"}
35+
:parser symbol}])
3536

3637
(defn parse-migrate-args
3738
[args]
3839
(parse-args args migrate-arg-specs))
3940

4041
(def create-migration-arg-specs
4142
[{:key :config
42-
:matcher #{"-c" "-config" "--config"}}])
43+
:matcher #{"-c" "-config" "--config"}
44+
:parser symbol}])
4345

4446
(defn parse-create-migration-args
4547
[args]

test/drift/test_args.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
(deftest test-parse-migrate-args
2828
(is (= (parse-migrate-args ["foo" "--other" "blah" "-v" "10" "bar" "--config" "conf" "baz"])
29-
[{:version "10" :config "conf"} ["foo" "--other" "blah" "bar" "baz"]])))
29+
[{:version "10" :config 'conf} ["foo" "--other" "blah" "bar" "baz"]])))
3030

3131
(deftest test-parse-create-migration-args
3232
(is (= (parse-create-migration-args ["foo" "--other" "blah" "-v" "10" "bar" "--config" "conf" "baz"])
33-
[{:config "conf"} ["foo" "--other" "blah" "-v" "10" "bar" "baz"]])))
33+
[{:config 'conf} ["foo" "--other" "blah" "-v" "10" "bar" "baz"]])))

0 commit comments

Comments
 (0)