babashka.cliapply-defaults- Fills missing keys inmfrom defaults.auto-coerce- Auto-coercessto data.coerce- Coerce stringsusingf.coerce-opts- Coerces values in the mapmusing the provided configuration.dispatch- Subcommand dispatcher.format-optsformat-tablemerge-opts- Merges babashka CLI options.number-char?opts->tablepadpad-cellsparse-args- Same asparse-optswith return data reshaped.parse-cmds- Parses sub-commands (arguments not starting with an option prefix).parse-keyword- Parse keyword froms.parse-opts- Returns a map of options parsed from command line argumentsargs, a seq of strings.parse-opts*- Parses CLIargsinto a raw opts map.spec->opts- Converts spec into opts format.validate-opts- Validates the mapmusing the provided configuration.
babashka.cli.exec
(apply-defaults m)
(apply-defaults m opts)Function.
Fills missing keys in m from defaults. Existing keys in m win.
Preserves metadata of m.
Supported options:
:exec-args- map of defaults.:spec- spec;:defaultentries become defaults viaspec->opts.
(auto-coerce s)Function.
Auto-coerces s to data. Does not coerce when s is not a string.
If s:
- is
trueorfalse, it is coerced as boolean - starts with number, it is coerced as a number (through Clojure's
edn/read-string) - starts with
:, it is coerced as a keyword (throughparse-keyword)
(coerce s f)Function.
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-opts m)
(coerce-opts m opts)Function.
Coerces values in the map m using the provided configuration.
Does not coerce values that are not strings.
Returns a new map with coerced values.
Supported options:
:coerce- a map of option (keyword) names to type keywords (optionally wrapped in a collection).:spec- a spec of options. See spec.:error-fn- error handler, called with a map containing:cause(:coerce),:msg,:option,:value, and:opts.
(dispatch table args)
(dispatch table args opts)Function.
Subcommand dispatcher.
Dispatches on longest matching command entry in table by matching
subcommands to the :cmds vector and invoking the correspondig :fn.
Table is in the form:
[{:cmds ["sub_1" .. "sub_n"] :fn f :args->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:args- concatenation of unparsed commands and args:rest-cmds: DEPRECATED, this will be removed in a future version
Use an empty :cmds vector to always match or to provide global options.
Provide an :error-fn to deal with non-matches.
Each entry in the table may have additional parse-args options.
For more information and examples, see README.md.
(format-opts {:as cfg, :keys [indent], :or {indent 2}})Function.
(format-table {:keys [rows indent], :or {indent 2}})Function.
(merge-opts m & ms)Function.
Merges babashka CLI options.
(number-char? c)Function.
(opts->table {:keys [spec order columns]})Function.
(pad len s)Function.
(pad-cells rows)Function.
(parse-args args)
(parse-args args opts)Function.
Same as parse-opts with return data reshaped.
Returns a map with:
:optsparsed opts:argsremaining unparsedargs
(parse-cmds args)
(parse-cmds args {:keys [no-keyword-opts]})Function.
Parses sub-commands (arguments not starting with an option prefix). Returns a map with:
:cmds- The parsed subcommands:args- The remaining (unparsed) arguments
(parse-keyword s)Function.
Parse keyword from s. Ignores leading :.
(parse-opts args)
(parse-opts args opts)Function.
Returns a map of options parsed from command line arguments args, a seq of strings.
Instead of a leading : either -- or - may be used as well.
Metadata on returned map, under :org.babashka/cli:
:argsremaining unparsedargs(not corresponding to any options)
Supported opts:
:coerce- a map of option (keyword) names to type keywords (optionally wrapped in a collection.):alias- a map of short names to long names.:spec- a spec of options. See spec.:restrict-trueor coll of keys. Throw on first parsed option not in set of keys or keys of:specand:coercecombined.:require- a coll of options that are required. See require.:validate- a map of validator functions. See validate.:exec-args- a map of default args. Will be overridden by args specified inargs. Values from:exec-argsare NOT coerced or auto-coerced; provide them in their final form.:no-keyword-opts-true. Support only--foo-style opts (i.e.:foowill not work).:repeated-opts-true. Forces writing the option name for every value, e.g.--foo a --foo b, rather than--foo a b:args->opts- consume unparsed commands and args as options:collect- a map of collection fns. See custom collection handling.
Examples:
(parse-opts ["foo" ":bar" "1"])
;; => ^{:org.babashka/cli {:args ["foo"]}} {:bar 1}
(parse-opts [":b" "1"] {:aliases {:b :bar} :coerce {:bar parse-long}})
;; => {:bar 1}
(parse-opts ["--baz" "--qux"] {:spec {:baz {:desc "Baz"}} :restrict true})
;; => throws 'Unknown option --qux' exception b/c there is no :qux key in the specSee also: parse-args
(parse-opts* args {:keys [coerce collect no-keyword-opts repeated-opts], :as opts})Function.
Parses CLI args into a raw opts map. Returns string values unchanged
(no coercion), does not apply :exec-args defaults, does not run
:restrict/:require/:validate. Result map includes
:org.babashka/cli metadata and internal ::implicit-true-keys /
::keys-order metadata used by coerce-opts.
Use this when you want to merge other sources (e.g. config files)
before coerce/validate. Pipeline: parse-opts* -> merge -> apply-defaults
-> coerce-opts -> validate-opts.
Supported options (subset of parse-opts): :alias/:aliases, :coerce,
:collect, :no-keyword-opts, :repeated-opts, :args->opts, :spec.
(spec->opts spec)
(spec->opts spec {:keys [exec-args]})Function.
Converts spec into opts format. Pass existing opts as optional second argument.
(validate-opts m)
(validate-opts m opts)Function.
Validates the map m using the provided configuration. Returns m.
Supported options:
:restrict-trueor coll of keys. Error on keys inmnot in the restrict set or not derivable from:specand:coerce.:require- a coll of options that are required.:validate- a map of option keys to validator functions (or maps with:predand:ex-msg).:spec- a spec of options (restrict, require, validate extracted from it).:coerce- used with:restrict trueto derive the set of known keys.:error-fn- error handler, called with a map containing:cause,:msg,:option, and:opts.
(-main & args)Function.
Main entrypoint for command line usage. Expects a namespace and var name followed by zero or more key value pair arguments that will be parsed and passed to the var. If the first argument is map-shaped, it is read as an EDN map containing parse instructions.
Example when used as a clojure CLI alias:
clojure -M:exec clojure.core prn :a 1 :b 2
;;=> {:a "1" :b "2"}(main & args)Function.