(auto-coerce s)Auto-coerces s to data. Does not coerce when s is not a string.
If s:
true or false, it is coerced as booleanedn/read-string):, it is coerced as a keyword (through parse-keyword)Auto-coerces `s` to data. Does not coerce when `s` is not a string. If `s`: * is `true` or `false`, it is coerced as boolean * starts with number, it is coerced as a number (through `edn/read-string`) * starts with `:`, it is coerced as a keyword (through `parse-keyword`)
(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 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 versionUse 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.
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:
```clojure
[{: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](README.md#subcommands).(merge-opts m & ms)Merges babashka CLI options.
Merges babashka CLI options.
(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-cmds args)(parse-cmds args {:keys [no-keyword-opts]})Parses sub-commands (arguments not starting with an option prefix) and returns a map with:
:cmds - The parsed subcommands:args - The remaining (unparsed) argumentsParses sub-commands (arguments not starting with an option prefix) and returns a map with: * `:cmds` - The parsed subcommands * `:args` - The remaining (unparsed) arguments
(parse-keyword s)Parse keyword from s. Ignores leading :.
Parse keyword from `s`. Ignores leading `:`.
(parse-opts args)(parse-opts args opts)Parse the command line arguments args, a seq of strings.
Instead of a leading : either -- or - may be used as well.
Return value: a map with parsed opts.
Additional data such as arguments (not corresponding to any options)
are available under the :org.babashka/cli key in the metadata.
Supported options:
: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 - true or coll of keys. Throw on first parsed option not in set of keys or keys of :spec and :coerce combined.: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 in args.:no-keyword-opts - true. Support only --foo-style opts (i.e. :foo will not work).:args->opts - consume unparsed commands and args as optionsExamples:
(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-args ["--baz" "--qux"] {:spec {:baz {:desc "Baz"} :restrict true})
;; => throws 'Unknown option --qux' exception b/c there is no :qux key in the spec
Parse the command line arguments `args`, a seq of strings.
Instead of a leading `:` either `--` or `-` may be used as well.
Return value: a map with parsed opts.
Additional data such as arguments (not corresponding to any options)
are available under the `:org.babashka/cli` key in the metadata.
Supported options:
* `: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](https://github.com/babashka/cli#spec).
* `:restrict` - `true` or coll of keys. Throw on first parsed option not in set of keys or keys of `:spec` and `:coerce` combined.
* `:require` - a coll of options that are required. See [require](https://github.com/babashka/cli#restrict).
* `:validate` - a map of validator functions. See [validate](https://github.com/babashka/cli#validate).
* `:exec-args` - a map of default args. Will be overridden by args specified in `args`.
* `:no-keyword-opts` - `true`. Support only `--foo`-style opts (i.e. `:foo` will not work).
* `:args->opts` - consume unparsed commands and args as options
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}
(parse-args ["--baz" "--qux"] {:spec {:baz {:desc "Baz"} :restrict true})
;; => throws 'Unknown option --qux' exception b/c there is no :qux key in the spec
```
(spec->opts spec)(spec->opts spec {:keys [exec-args]})Converts spec into opts format. Pass existing opts as optional second argument.
Converts spec into opts format. Pass existing opts as optional second argument.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |