Skip to content

Consider option format style in error message #140

@lread

Description

@lread

Issue

Babashka cli will report based on the converted option as a keyword:

Some examples:

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

(cli/parse-args ["--foo" "bar"] {:restrict true})
;; => Execution error (ExceptionInfo) at babashka.cli/parse-opts$fn (cli.cljc:328).
;;    Unknown option: :foo

(cli/parse-args ["-f" "bar"] {:restrict true})
;; => Execution error (ExceptionInfo) at babashka.cli/parse-opts$fn (cli.cljc:328).
;;    Unknown option: :f

(cli/parse-args ["--foo" "2"] {:spec {:foo {:validate zero?}}})
;; => Execution error (ExceptionInfo) at babashka.cli/parse-opts$fn (cli.cljc:328).
;;    Invalid value for option :foo: 2

Sometimes the user hasn't typed the problematic option:

(cli/parse-args ["--foo" "bar"] {:spec {:boo {:require true}}})
;; => Execution error (ExceptionInfo) at babashka.cli/parse-opts$fn (cli.cljc:328).
;;    Required option: :boo

(I do realize that --foo is, can be, equivalent to :foo by bb cli, but not by default.
But that is also an implementation detail.)

(I have used bb cli on projects that support both -X invocation and main invocation, but that could be considered very custom).

Option 0: Do nothing

We're OK with the implementation detail leak.
It's not a stretch for folks to understand that :foo means --foo.

Option 1: Always present cli-style

Always convey :foo as long-form --foo.

This seems like a better default?
And it matches the option style that usage help presents via cli/format-opts.

Option 2: Allow user to specify preference

We already have :no-keyword-opts, would that do the trick?
When :no-keyword-opts is true (the default), we could display parsed option :foo as --foo.

That doesn't, though, cover :no-keyword-opts false.

Meh. Probably too complicated.

Option 3: Automatically use syntax user typed

This means remembering what the user typed.

This might become a bit tricky, for example, the user might type alias and long option formats and which can be collected into a single option:

(cli/parse-args ["--foo" "bar" "--foo" "baz" "-f" "buz"]
                {:spec {:foo {:coerce [:keyword]
                              :alias :f}}})
;; => {:opts {:foo [:bar :baz :buz]}}

Meh. Probably too complicated.

Proposal

Option 1: Error message always present option in --foo style.

Next Steps

What do you think? If you like the idea, I'm happy to do up a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions