Skip to content

Consider having --no-foo trigger a validation error for non boolean options #166

@lread

Description

@lread

Currently

It's nice that bb cli supports --no-foo:

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

(cli/parse-opts ["--no-foo"])
;; => {:foo false}

(cli/parse-opts ["--no-foo"] {:spec {:foo {:coerce :boolean}}})
;; => {:foo false}

But what if foo is not a boolean?

When false is coercible, it doesn't fail...

(cli/parse-opts ["--no-foo"] {:spec {:foo {:coerce :symbol}}})
;; => {:foo false}

(cli/parse-opts ["--no-foo"] {:spec {:foo {:coerce :string}}})
;; => {:foo "false"}

(cli/parse-opts ["--no-foo"] {:spec {:foo {:coerce :keyword}}})
;; => {:foo :false}

But since this makes no sense, I'm thinking specifying --no-foo here merits an error.

We do get an error when false is not coercible...

(cli/parse-opts ["--no-foo"] {:spec {:foo {:coerce :long}}})
;; => Execution error (ExceptionInfo) at babashka.cli/->error-fn$fn (cli.cljc:263).
;;    Invalid value for option --no-foo: cannot transform input "false" to long

But the error message is not the user-friendliest.

Proposal

Generate a helpful error message when --no- is applied to a non-boolean option.

For the :cause we could create a new :not-negatable or reuse the existing :coerce but with a better message: Cannot negate --foo.

Out of scope

I think the :negatable option is still fine as it is.
It only affects generated help.
If a developer slaps this on a non-boolean option, that's a developer error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions