babashka/cli

Cannot override booleans with a default of false without spelling out the boolean

corasaurus-hex opened this issue · 6 comments

This is the current behavior:

(require '[babashka.cli :as cli])

(def cli-opts {:coerce {:help :boolean}
               :restrict [:help]
               :alias {:h :help}
               :exec-args {:help false}})

(cli/parse-opts [] cli-opts)
;; => {:help false}
(cli/parse-opts ["--help"] cli-opts)
;; => {:help false}
(cli/parse-opts ["-h"] cli-opts)
;; => {:help false}
(cli/parse-opts ["--help" "true"] cli-opts)
;; => {:help true}
(cli/parse-opts ["-h" "true"] cli-opts)
;; => {:help true}

I expected it to be:

(cli/parse-opts [] cli-opts)
;; => {:help false}
(cli/parse-opts ["--help"] cli-opts)
;; => {:help true}
(cli/parse-opts ["-h"] cli-opts)
;; => {:help true}
(cli/parse-opts ["--help" "true"] cli-opts)
;; => {:help true, :args ["true"]}
(cli/parse-opts ["-h" "true"] cli-opts)
;; => {:help true, :args ["true"]}

The fix in the interim is to not give it a default value and just use the absence of :help as a false value.

Fixed (cli/parse-opts ["--help"] cli-opts) but (cli/parse-opts ["-h" "true"] cli-opts) is still interpreted as {:help true} since you can also provide --help false:

user=> (cli/parse-opts ["--help" "false"] {:exec-args {:help true}})
{:help false}

That's just a feature of this library but maybe not what you expected.

Published as 0.4.37

Is this working right?

user=> (cli/parse-opts ["-h" "hi"] cli-opts)
{:help true}
user=> (cli/parse-opts ["-h" "--"  "hi"] cli-opts)
{:help true}

I'd expect {:help true, :args ["hi"]} to be the return value.

Ope, disregard, I'm using parse-opts

It still seems strange to me that the string "true" gets parsed:

user=> (cli/parse-args ["-h" "hi" "bye"] cli-opts)
{:args ["hi" "bye"], :opts {:help true}}
user=> (cli/parse-args ["-h" "true" "bye"] cli-opts)
{:args ["bye"], :opts {:help true}}
user=> (cli/parse-args ["-h" "--" "true" "bye"] cli-opts)
{:args ["true" "bye"], :opts {:help true}}

But as you said, that's on purpose. 🤷‍♀️