(coerce s f)
Coerce string s
using f
. Does not coerce when s
is not a string.
f
may be a keyword (:boolean
, :int
, :double
, :symbol
,
:keyword
) or a function. When f
return nil
, this is
interpreted as a parse failure and throws.
Coerce string `s` using `f`. Does not coerce when `s` is not a string. `f` may be a keyword (`:boolean`, `:int`, `:double`, `:symbol`, `:keyword`) or a function. When `f` return `nil`, this is interpreted as a parse failure and throws.
(dispatch table args)
(dispatch table args opts)
Subcommand dispatcher.
Dispatches on first matching command entry in table
. A match is
determines by whether :cmds
, a vector of strings, is a subsequence
(matching from the start) of the invoked commands.
Table is in the form:
[{:cmds ["sub_1" .. "sub_n"] :fn f :cmds-opts [:lib]}
...
{:cmds [] :fn f}]
When a match is found, :fn
called with the return value of
parse-args
applied to args
enhanced with:
:dispatch
- the matching commands:rest-cmds
- any remaining cmdsAny trailing commands can be matched as options using :cmds-opts
.
This function does not throw. Use an empty :cmds
vector to always match.
Examples: see README.md.
Subcommand dispatcher. Dispatches on first matching command entry in `table`. A match is determines by whether `:cmds`, a vector of strings, is a subsequence (matching from the start) of the invoked commands. Table is in the form: ```clojure [{:cmds ["sub_1" .. "sub_n"] :fn f :cmds-opts [:lib]} ... {:cmds [] :fn f}] ``` When a match is found, `:fn` called with the return value of `parse-args` applied to `args` enhanced with: * `:dispatch` - the matching commands * `:rest-cmds` - any remaining cmds Any trailing commands can be matched as options using `:cmds-opts`. This function does not throw. Use an empty `:cmds` vector to always match. Examples: see [README.md](README.md#subcommands).
(parse-args args)
(parse-args args opts)
Same as parse-opts
but separates parsed opts into :opts
and adds
:cmds
and :rest-args
on the top level instead of metadata.
Same as `parse-opts` but separates parsed opts into `:opts` and adds `:cmds` and `:rest-args` on the top level instead of metadata.
(parse-opts args)
(parse-opts args opts)
Parse the command line arguments args
, a seq of strings.
Expected format: ["cmd_1" ... "cmd_n" ":k_1" "v_1" .. ":k_n" "v_n"]
.
Instead of a leading :
either --
or -
may be used as well.
Return value: a map with parsed opts. Additional data such as
initial subcommands and remaining args after --
are available
under the :org.babashka/cli
key in the metadata.
Supported options:
:coerce
: a map of keys to coercion functions that will be applied to parsed :opts
. See coerce-vals
.:aliases
: a map of short names to long names.Examples:
(parse-opts ["foo" ":bar" "1])
;; => {:bar "1", :org.babashka/cli {:cmds ["foo"]}}
(parse-args [":b" "1] {:aliases {:b :bar} :coerce {:bar parse-long}})
;; => {:bar 1}
Parse the command line arguments `args`, a seq of strings. Expected format: `["cmd_1" ... "cmd_n" ":k_1" "v_1" .. ":k_n" "v_n"]`. Instead of a leading `:` either `--` or `-` may be used as well. Return value: a map with parsed opts. Additional data such as initial subcommands and remaining args after `--` are available under the `:org.babashka/cli` key in the metadata. Supported options: - `:coerce`: a map of keys to coercion functions that will be applied to parsed `:opts`. See `coerce-vals`. - `:aliases`: a map of short names to long names. Examples: ```clojure (parse-opts ["foo" ":bar" "1]) ;; => {:bar "1", :org.babashka/cli {:cmds ["foo"]}} (parse-args [":b" "1] {:aliases {:b :bar} :coerce {:bar parse-long}}) ;; => {:bar 1} ```
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close