parse-opts
function
babashka.cli/parse-opts
(parse-opts [args] [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](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).
* `: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](https://github.com/babashka/cli#custom-collection-handling).
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
```
Examples
No examples yet. Be the first to add one!