Skip to content

Commit 1acfe11

Browse files
authored
Merge pull request #39 from Sardtok/print-usage-on-invalid-args
Prints usage when there are unparsed args
2 parents 7002e10 + f308fc0 commit 1acfe11

3 files changed

Lines changed: 47 additions & 22 deletions

File tree

src/drift/args.clj

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
(ns drift.args)
1+
(ns drift.args
2+
(:require [clojure.string :as string]
3+
[drift.config :as config]))
24

35
(defn split-args
46
"split an arglist using a matcher fn : returns
57
[args-before-match, args-including-and-after-match]"
68
[args matcher]
7-
(split-with #(not (matcher %)) args))
9+
(split-with #(not-any? (partial = %) matcher) args))
810

911
(defn remove-opt
1012
"given a matched option and whatever is after it in the arg list, remove the option
@@ -27,22 +29,35 @@
2729
[{} args]
2830
specs))
2931

32+
(def config-arg-spec
33+
{:key :config
34+
:matcher ["-c" "-config" "--config"]
35+
:parser symbol
36+
:default config/default-config-fn-symbol
37+
:desc "Fully qualified name of function returning a Drift configuration map."})
38+
3039
(def migrate-arg-specs
3140
[{:key :version
32-
:matcher #{"-v" "-version" "--version"}}
33-
{:key :config
34-
:matcher #{"-c" "-config" "--config"}
35-
:parser symbol}])
41+
:matcher ["-v" "-version" "--version"]
42+
:desc "The version number to migrate to. Can be lower than the current version to roll back migrations."}
43+
config-arg-spec])
3644

3745
(defn parse-migrate-args
3846
[args]
3947
(parse-args args migrate-arg-specs))
4048

4149
(def create-migration-arg-specs
42-
[{:key :config
43-
:matcher #{"-c" "-config" "--config"}
44-
:parser symbol}])
50+
[config-arg-spec])
4551

4652
(defn parse-create-migration-args
4753
[args]
4854
(parse-args args create-migration-arg-specs))
55+
56+
(defn print-usage
57+
#^{:doc "Prints out how to use a command with a given name and specification."}
58+
[cmd spec & [required-arg]]
59+
(println "Usage: lein" cmd "[options]" (or required-arg ""))
60+
(println "Options:")
61+
(doseq [arg spec]
62+
(println (str " " (string/join " " (:matcher arg)) "\t" (:desc arg)))
63+
(when (:default arg) (println "\t\t\tDefault value:" (:default arg)))))

src/drift/execute.clj

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns drift.execute
2-
(:require [clojure.tools.logging :as logging]
2+
(:require [clojure.string :as string]
3+
[clojure.tools.logging :as logging]
34
[drift.args :as args]
45
[drift.config :as config]
56
[drift.core :as core]
@@ -30,6 +31,10 @@
3031
(defn
3132
run [args]
3233
(let [[opts remaining] (args/parse-migrate-args args)]
33-
(config/with-config-fn-symbol (:config opts)
34-
(fn []
35-
(migrate (:version opts) remaining)))))
34+
(if (empty? remaining)
35+
(config/with-config-fn-symbol
36+
(:config opts)
37+
(fn []
38+
(migrate (:version opts) remaining)))
39+
(do (logging/error "Invalid arguments:" (string/join " " remaining))
40+
(args/print-usage "migrate" args/migrate-arg-specs)))))

src/drift/generator.clj

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
(ns drift.generator
2-
(:require [drift.args :as args]
2+
(:require [clojure.string :as string]
3+
[clojure.tools.logging :as logging]
4+
[drift.args :as args]
35
[drift.builder :as builder]
4-
[drift.core :as core]
5-
[drift.config :as config]))
6+
[drift.config :as config]
7+
[drift.core :as core]))
68

79
(defn
810
#^{ :doc "Prints out how to use the generate migration command." }
@@ -13,7 +15,7 @@
1315
(defn
1416
create-file-content [migration-namespace ns-content up-content down-content]
1517
(let [migration-number (core/migration-number-from-namespace migration-namespace)]
16-
(str "(ns " migration-namespace (or ns-content (config/default-ns-content)) ")
18+
(str "(ns " migration-namespace (or ns-content (config/default-ns-content)) ")
1719
1820
(defn up
1921
\"Migrates the database up to version " migration-number ".\"
@@ -49,8 +51,11 @@
4951
"parse command-line args from lein, set up any custom config,
5052
and invoke generate-migration-file"
5153
[args]
52-
(let [[opts remaining] (args/parse-create-migration-args args)
53-
migration-name (first remaining)]
54-
(config/with-config-fn-symbol (:config opts)
55-
(fn []
56-
(generate-migration-file migration-name)))))
54+
(let [[opts [migration-name & remaining]] (args/parse-create-migration-args args)]
55+
(if (empty? remaining)
56+
(config/with-config-fn-symbol
57+
(:config opts)
58+
(fn []
59+
(generate-migration-file migration-name)))
60+
(do (logging/error "Invalid arguments:" (string/join " " remaining))
61+
(args/print-usage "create-migration" args/create-migration-arg-specs "migration-name")))))

0 commit comments

Comments
 (0)