@@ -341,6 +341,8 @@ present"
341
341
inits)))))
342
342
343
343
(defn default-main
344
+ " Default handler for the --main flag. Will start REPL, invoke -main with the
345
+ supplied arguments."
344
346
[repl-env {:keys [main script args repl-env-options options inits] :as cfg}]
345
347
(let [opts (cond-> options
346
348
(not (:output-dir options))
@@ -542,34 +544,50 @@ present"
542
544
((::compile (repl/-repl-options (repl-env )) default-compile )
543
545
repl-env (merge cfg {:args args :ns ns })))
544
546
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)
547
553
(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" )))
549
555
keys set)))
550
556
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]
552
564
(reduce
553
565
(fn [ret [flags config]]
554
566
(cond-> ret
555
567
(= " bool" (:arg config))
556
568
(into flags)))
557
569
#{} (:init commands)))
558
570
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))
561
576
562
577
(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."
563
581
([commands]
564
582
(add-commands {:main-dispatch nil :init-dispatch nil } commands))
565
583
([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 ]
568
586
(fn [m]
569
587
(reduce
570
- (fn [ret [cs csm ]]
588
+ (fn [ret [flag-names flag-config ]]
571
589
(merge ret
572
- (zipmap cs (repeat (:fn csm )))))
590
+ (zipmap flag-names (repeat (:fn flag-config )))))
573
591
m options))))]
574
592
(-> commands
575
593
(update-in [:groups ] merge groups)
@@ -578,7 +596,12 @@ present"
578
596
(merge-dispatch :init-dispatch init)
579
597
(merge-dispatch :main-dispatch main)))))
580
598
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
582
605
(add-commands
583
606
{:groups {::main&compile {:desc " init options"
584
607
:pseudos
@@ -662,9 +685,14 @@ present"
662
685
[" -h" " --help" " -?" ] {:fn help-opt
663
686
:doc " Print this help message and exit" }}}))
664
687
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]
666
694
(letfn [(normalize* [args*]
667
- (if (not (contains? (get-options commands :main ) (first args*)))
695
+ (if (not (contains? (get-flags-set commands :main ) (first args*)))
668
696
(let [pred (complement (bool-init-options commands))
669
697
[pre post] ((juxt #(take-while pred %)
670
698
#(drop-while pred %))
@@ -685,7 +713,11 @@ present"
685
713
args'
686
714
(recur args' (normalize* args'))))))
687
715
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]
689
721
(add-commands default-commands
690
722
(::commands (repl/repl-options (repl-env )))))
691
723
0 commit comments