Skip to content

Commit 48b4d7e

Browse files
committed
* add docstrings to cljs.cli, use clearer names
1 parent 47a7bfd commit 48b4d7e

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

src/main/clojure/cljs/cli.clj

+46-14
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ present"
341341
inits)))))
342342

343343
(defn default-main
344+
"Default handler for the --main flag. Will start REPL, invoke -main with the
345+
supplied arguments."
344346
[repl-env {:keys [main script args repl-env-options options inits] :as cfg}]
345347
(let [opts (cond-> options
346348
(not (:output-dir options))
@@ -542,34 +544,50 @@ present"
542544
((::compile (repl/-repl-options (repl-env)) default-compile)
543545
repl-env (merge cfg {:args args :ns ns})))
544546

545-
(defn get-options [commands k]
546-
(if (= :all k)
547+
(defn get-options
548+
"Given a commands map and a phase (:init or :main), return all flags
549+
which can be handled as a set. If phase is :all will return the entire
550+
flag set (:init + :main)."
551+
[commands phase]
552+
(if (= :all phase)
547553
(into (get-options commands :main) (get-options commands :init))
548-
(-> (get commands (keyword (str (name k) "-dispatch")))
554+
(-> (get commands (keyword (str (name phase) "-dispatch")))
549555
keys set)))
550556

551-
(defn bool-init-options [commands]
557+
(defn get-flags-set
558+
"See a get-options, this just provides a better name."
559+
[commands phase]
560+
(get-options commands phase))
561+
562+
(defn bool-init-options
563+
[commands]
552564
(reduce
553565
(fn [ret [flags config]]
554566
(cond-> ret
555567
(= "bool" (:arg config))
556568
(into flags)))
557569
#{} (:init commands)))
558570

559-
(defn dispatch? [commands k opt]
560-
(contains? (get-options commands k) opt))
571+
(defn dispatch?
572+
"Given a commands map, a phase (:init or :main) and a command line flag,
573+
return true if the flag has a handler."
574+
[commands phase opt]
575+
(contains? (get-flags-set commands phase) opt))
561576

562577
(defn add-commands
578+
"Given commands map (see below), create a commands map with :init-dispatch
579+
and :main-dispatch keys where short and long arguments are mapped individually
580+
to their processing fn."
563581
([commands]
564582
(add-commands {:main-dispatch nil :init-dispatch nil} commands))
565583
([commands {:keys [groups main init]}]
566-
(letfn [(merge-dispatch [st k options]
567-
(update-in st [k]
584+
(letfn [(merge-dispatch [commands dispatch-key options]
585+
(update-in commands [dispatch-key]
568586
(fn [m]
569587
(reduce
570-
(fn [ret [cs csm]]
588+
(fn [ret [flag-names flag-config]]
571589
(merge ret
572-
(zipmap cs (repeat (:fn csm)))))
590+
(zipmap flag-names (repeat (:fn flag-config)))))
573591
m options))))]
574592
(-> commands
575593
(update-in [:groups] merge groups)
@@ -578,7 +596,12 @@ present"
578596
(merge-dispatch :init-dispatch init)
579597
(merge-dispatch :main-dispatch main)))))
580598

581-
(def default-commands
599+
(def ^{:doc "Default commands for ClojureScript REPLs. :groups are to support
600+
printing organized output for --help. a :main option must come at the end, they
601+
specify things like running a -main fn, compile, repl, or web serving. Sometimes
602+
:main options can use be used together (i.e. --compile --repl), but this is not
603+
generic - the combinations must be explicitly supported"}
604+
default-commands
582605
(add-commands
583606
{:groups {::main&compile {:desc "init options"
584607
:pseudos
@@ -662,9 +685,14 @@ present"
662685
["-h" "--help" "-?"] {:fn help-opt
663686
:doc "Print this help message and exit"}}}))
664687

665-
(defn normalize [commands args]
688+
(defn normalize
689+
"Given a commands map (flag + value -> option processor fn) and the sequence of
690+
command line arguments passed to the process, normalize it. Boolean flags don't
691+
need to specify anything, insert the implied trues and return the normalized
692+
command line arguments."
693+
[commands args]
666694
(letfn [(normalize* [args*]
667-
(if (not (contains? (get-options commands :main) (first args*)))
695+
(if (not (contains? (get-flags-set commands :main) (first args*)))
668696
(let [pred (complement (bool-init-options commands))
669697
[pre post] ((juxt #(take-while pred %)
670698
#(drop-while pred %))
@@ -685,7 +713,11 @@ present"
685713
args'
686714
(recur args' (normalize* args'))))))
687715

688-
(defn merged-commands [repl-env]
716+
(defn merged-commands
717+
"Given a repl environment combine the default commands with the custom
718+
REPL commands. Commands are a mapping from a command line argument
719+
(flag + value) to a function to handle that particular flag + value."
720+
[repl-env]
689721
(add-commands default-commands
690722
(::commands (repl/repl-options (repl-env)))))
691723

0 commit comments

Comments
 (0)