Skip to content

Commit

Permalink
Fixing #35 - :global-options not required if empty.
Browse files Browse the repository at this point in the history
We already injected default options on reading, so
I basically extended the SETUP-DEFAULTS def that
was already there..... :)
  • Loading branch information
lenz committed Nov 3, 2019
1 parent 9bc879d commit 7a2bd99
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).


## 0.3.9 - 2019-11-03
### Changed
* Fixes #35 - `:global-opts` can be left out entirely if empty
* Fixes #37 - clj-kondo warnings

## 0.3.8 - 2019-07-13
### Changed
* Fixes #75 - Misc linter errors
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ So we define a configuration:
It contains:

* Information on the app itself (name, version)
* The list of global parameters, i.e. the ones that apply to al subcommands (may be empty)
* The list of global parameters, i.e. the ones that apply to al subcommands (may be empty, or you may skip it at all)
* A list of sub-commands, each with its own parameters in `:opts`, and a function to be called in `:runs`. You can optionally validate the full parameter-map that is received by subcommand at once by passing a Spec into `:spec`.
* If within the subcommand you add a 0-arity function to `:on-shutdown`, it will be called when the JVM terminates. This is
mostly useful for long running servers, or to do some clean-up. Note that the hook is always called - whether the shutdown
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject cli-matic "0.3.8"
(defproject cli-matic "0.3.9"
:description "Compact [sub]command line parsing library, for Clojure"
:url "https://github.com/l3nz/cli-matic"
:license {:name "Eclipse Public License, v2"
Expand Down
17 changes: 12 additions & 5 deletions src/cli_matic/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,6 @@
(str "JVM Exception: "
(with-out-str (println t)))))))

(def setup-defaults
{:app {:global-help H/generate-global-help
:subcmd-help H/generate-subcmd-help}})

(defn run-cmd*
"
Executes our code.
Expand Down Expand Up @@ -561,6 +557,17 @@
(str "Option error: " error-text))
:NONE (invoke-subcmd subcommand-def commandline))))

(def SETUP-DEFAULTS
{:app {:global-help H/generate-global-help
:subcmd-help H/generate-subcmd-help}
:global-opts []})

(defn add-setup-defaults
"Adds all elements that need to be in the setup spec
but we allow the caller not specify explicitly."
[supplied-setup]
(U/deep-merge SETUP-DEFAULTS supplied-setup))

(defn run-cmd
"This is the actual function that is executed.
It wraps [[run-cmd*]] and then does the printing
Expand All @@ -569,7 +576,7 @@
As it invokes `System.exit` you cannot use it from a REPL.
"
[args supplied-setup]
(let [setup (U/deep-merge setup-defaults supplied-setup)
(let [setup (add-setup-defaults supplied-setup)
{:keys [help stderr subcmd retval]}
(run-cmd* setup (if (nil? args) [] args))]
(when (seq stderr)
Expand Down
35 changes: 23 additions & 12 deletions test/cli_matic/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
[clojure.spec.alpha :as s]
[clojure.string :as str]
[cli-matic.core :refer [parse-cmds
run-cmd* ->RV
run-cmd*
->RV
assert-unique-values
assert-cfg-sanity
parse-cmds-with-defaults]]))
parse-cmds-with-defaults
add-setup-defaults]]))

(defn cmd_foo [& opts])
(defn cmd_bar [& opts])
Expand Down Expand Up @@ -228,7 +230,7 @@
(are [i o]
(= (try-catch-all
(apply assert-unique-values i)
(fn [e] :ERR))
(fn [_] :ERR))
o)

; empty
Expand All @@ -252,12 +254,14 @@
(deftest check-cfg-format
(testing "Cfg format"
(are [i o]
(= (try-catch-all
(assert-cfg-sanity i)
(fn [e]
;(prn e)
:ERR))
o)
(= o
(try-catch-all
(-> i
add-setup-defaults
assert-cfg-sanity)
(fn [_]
;(prn e)
:ERR)))

;; OK
{:app {:command "toycalc" :description "A" :version "0.0.1"}
Expand All @@ -269,7 +273,14 @@
{:option "b" :as "Addendum 2" :type :int :default 0}]}]}
nil

;; double in global
;; No global options - still OK (bug #35)
{:app {:command "toycalc" :description "A" :version "0.0.1"}
:commands [{:command "add" :description "Adds" :runs identity
:opts [{:option "a" :as "Addendum 1" :type :int}
{:option "b" :as "Addendum 2" :type :int :default 0}]}]}
nil

;; double in global
{:app {:command "toycalc" :description "A" :version "0.0.1"}

:global-opts [{:option "base" :as "T" :type :int :default 10}
Expand All @@ -280,7 +291,7 @@
{:option "b" :as "Addendum 2" :type :int :default 0}]}]}
:ERR

;; double in specific
;; double in specific
{:app {:command "toycalc" :description "A" :version "0.0.1"}

:global-opts [{:option "base" :as "T" :type :int :default 10}]
Expand All @@ -290,7 +301,7 @@
{:option "b" :short "q" :as "Addendum 2" :type :int :default 0}]}]}
:ERR

;; positional subcmds in global opts
;; positional subcmds in global opts
{:app {:command "toycalc" :description "A" :version "0.0.1"}

:global-opts [{:option "base" :short 0 :as "T" :type :int :default 10}]
Expand Down

0 comments on commit 7a2bd99

Please sign in to comment.