Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Terminates the process after `dispatch`'s `:help` option prints an *error*
Must exit or throw.

Default: `System/exit` (JVM), `js/process.exit` (Node), `throw` (browser).
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L1694-L1722">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L1696-L1724">Source</a></sub></p>

## <a name="babashka.cli/apply-defaults">`apply-defaults`</a>
``` clojure
Expand All @@ -76,7 +76,7 @@ Fills missing keys in `m` from defaults. Existing keys in `m` win.
Supported options:
* `:exec-args` - map of defaults. Not subject to `:restrict`.
* `:spec` - spec; `:default` entries become defaults via `spec->opts`.
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L491-L507">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L493-L509">Source</a></sub></p>

## <a name="babashka.cli/auto-coerce">`auto-coerce`</a>
``` clojure
Expand Down Expand Up @@ -123,7 +123,7 @@ Coerces values in the map `m` using the provided configuration.
`"--foo"`, `"-f"`, or `":foo"`), as opposed to `:option`, the normalized
keyword (`:foo`). It lets a handler echo what the user actually typed rather
than reconstruct it. It is omitted when no originating token is known.
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L317-L396">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L317-L398">Source</a></sub></p>

## <a name="babashka.cli/default-width-fn">`default-width-fn`</a>
``` clojure
Expand All @@ -136,7 +136,7 @@ The default `:max-width-fn` for [`format-table`](#babashka.cli/format-table)/[`f
width or nil: node `process.stdout.columns`, else `$COLUMNS`, else a JLine
provider probe (clj, when JLine is on the classpath, e.g. babashka), else nil
(the caller then falls back to 80).
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L816-L834">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L818-L836">Source</a></sub></p>

## <a name="babashka.cli/dispatch">`dispatch`</a>
``` clojure
Expand Down Expand Up @@ -209,7 +209,7 @@ Command dispatcher.
Each entry in the table may have additional [`parse-args`](#babashka.cli/parse-args) options.

For more information and examples, see [README.md](README.md#commands).
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L2023-L2111">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L2025-L2113">Source</a></sub></p>

## <a name="babashka.cli/format-command-error">`format-command-error`</a>
``` clojure
Expand All @@ -233,7 +233,7 @@ Render a terse, helpful message (a string) for a dispatch error.
this, then calls [`*exit-fn*`](#babashka.cli/*exit-fn*)). Call it from a custom `:error-fn` to keep the
standard message and add your own output. `--help`/`-h` is not an error - it
goes to the `:help-fn`, rendered by [`format-command-help`](#babashka.cli/format-command-help).
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L1733-L1780">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L1735-L1782">Source</a></sub></p>

## <a name="babashka.cli/format-command-help">`format-command-help`</a>
``` clojure
Expand Down Expand Up @@ -278,7 +278,7 @@ Render conventional `--help` text (a string) for the command at path `cmds`
This is the renderer the `:help` option uses; call it from a custom `:help-fn`
to render the standard help and then add your own output. An entry may carry
`:no-doc true` to be omitted from `Commands:`.
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L1651-L1692">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L1653-L1694">Source</a></sub></p>

## <a name="babashka.cli/format-opts">`format-opts`</a>
``` clojure
Expand All @@ -289,7 +289,7 @@ Function.
Formats options into an options usage help string.

See [Printing options](/README.md#printing-options).
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L998-L1009">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L1000-L1011">Source</a></sub></p>

## <a name="babashka.cli/format-table">`format-table`</a>
``` clojure
Expand All @@ -302,7 +302,7 @@ Function.

Formats `rows` into a table (string).
See [Printing options](/README.md#printing-options).
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L887-L909">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L889-L911">Source</a></sub></p>

## <a name="babashka.cli/merge-opts">`merge-opts`</a>
``` clojure
Expand All @@ -321,7 +321,7 @@ Function.

Converts options to a table of rows.
See [Printing options](/README.md#printing-options).
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L939-L962">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L941-L964">Source</a></sub></p>

## <a name="babashka.cli/parse-args">`parse-args`</a>
``` clojure
Expand All @@ -335,7 +335,7 @@ Same as [`parse-opts`](#babashka.cli/parse-opts) with return data reshaped.
Returns a map with:
* `:opts` parsed opts
* `:args` remaining unparsed `args`
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L757-L767">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L759-L769">Source</a></sub></p>

## <a name="babashka.cli/parse-cmds">`parse-cmds`</a>
``` clojure
Expand Down Expand Up @@ -395,7 +395,7 @@ Returns a map of options parsed from command line arguments `args`, a seq of str
;; => throws 'Unknown option --qux' exception b/c there is no :qux key in the spec
```
See also: [`parse-args`](#babashka.cli/parse-args)
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L709-L755">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L711-L757">Source</a></sub></p>

## <a name="babashka.cli/parse-opts*">`parse-opts*`</a>
``` clojure
Expand All @@ -415,7 +415,7 @@ Parses CLI `args` into a raw opts map. Returns string values unchanged

Supported options (subset of `parse-opts`): `:alias`/`:aliases`, `:coerce`,
`:collect`, `:no-keyword-opts`, `:repeated-opts`, `:args->opts`, `:spec`.
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L512-L707">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L514-L709">Source</a></sub></p>

## <a name="babashka.cli/spec->opts">`spec->opts`</a>
``` clojure
Expand Down Expand Up @@ -445,7 +445,7 @@ Converts a `dispatch` table into a tree. Each `:cmds` becomes a path of
```

A tree passed in is normalized and returned, so the function is idempotent.
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L1170-L1192">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L1172-L1194">Source</a></sub></p>

## <a name="babashka.cli/validate-opts">`validate-opts`</a>
``` clojure
Expand All @@ -469,7 +469,7 @@ Validates the map `m` using the provided configuration. Returns `m`.
keyword (`:foo`). It lets a handler echo what the user actually typed rather
than reconstruct it. It is present for `:restrict` and `:validate`, and absent
for `:require` (a missing required option was never typed, so it has no token).
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L398-L489">Source</a></sub></p>
<p><sub><a href="https://github.com/babashka/cli/blob/main/src/babashka/cli.cljc#L400-L491">Source</a></sub></p>

-----
# <a name="babashka.cli.exec">babashka.cli.exec</a>
Expand Down
12 changes: 7 additions & 5 deletions src/babashka/cli.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -370,17 +370,19 @@
(let [data (ex-data e)
km (::opt->flag m-meta)
flag (get km k)
iv (:implicit-value data)]
iv (:implicit-value data)
label (option-label km k)]
(error-fn (cond-> {:cause :coerce
;; same shape as validate: name the option, then the reason.
;; when implicit-value, give a more nuanced error message
;; instead of "cannot transform (implicit) true to ..."
:msg (case iv
true (str "Missing value for option " (option-label km k))
true (str "Missing value for option " label)
;; NOTE: squint lacks clojure.string/replace-first until > 0.14.196; use JS interop for now
false (str "Cannot negate option " #?(:squint (.replace (option-label km k) "no-" "")
:default (str/replace-first (option-label km k) "no-" "")))
(str "Invalid value for option " (option-label km k) ": "
false (str "Negation " label " invalid for option "
#?(:squint (.replace label "no-" "")
:default (str/replace-first label "no-" "")))
(str "Invalid value for option " label ": "
(coerce-failure-reason (:input data) iv (:coerce-fn data))))
:option k
:value v
Expand Down
8 changes: 4 additions & 4 deletions test/babashka/cli_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,19 @@
(testing "--no-foo throws on non-boolean"
(doseq [coerce-fn [:long :number :symbol :keyword :string :edn]]
(is (thrown-with-msg?
#?(:cljd Object :default Exception) #"Cannot negate option --foo"
#?(:cljd Object :default Exception) #"Negation --no-foo invalid for option --foo"
(cli/parse-opts ["--no-foo"] {:coerce {:foo coerce-fn}}))
(str "for coerce to: " coerce-fn))
(is (thrown-with-msg?
#?(:cljd Object :default Exception) #"Cannot negate option --foo"
#?(:cljd Object :default Exception) #"Negation --no-foo invalid for option --foo"
(cli/parse-opts ["--no-foo"] {:coerce {:foo [coerce-fn]}}))
(str "for coerce to: [" coerce-fn "]"))
(is (thrown-with-msg?
#?(:cljd Object :default Exception) #"Cannot negate option :foo"
#?(:cljd Object :default Exception) #"Negation :no-foo invalid for option :foo"
(cli/parse-opts [":no-foo"] {:coerce {:foo coerce-fn}}))
(str "for coerce to: " coerce-fn))
(is (thrown-with-msg?
#?(:cljd Object :default Exception) #"Cannot negate option :foo"
#?(:cljd Object :default Exception) #"Negation :no-foo invalid for option :foo"
(cli/parse-opts [":no-foo"] {:coerce {:foo [coerce-fn]}}))
(str "for coerce to: [" coerce-fn "]")))))

Expand Down
Loading