As it stands the behaviour of the library when user inputs undefined flags is to print a. the error b. help.
While the above described approach is thoughtful default behaviour and I could achieve parity with say kubectl
or docker
or git
for non existent commands like so
cli.Action(func() error {
w := cli.OtherArgs()
return fmt.Errorf(`unknown command %q for %q
Run '%[2]s --help' for usage`, w[0], cli.Name())
})
if err := cli.Run(); err != nil {
fmt.Printf("Error: %s\n", err)
}
$ go run ./cmd/pilates wrong
Error: unknown command "wrong" for "pilates"
Run 'pilates --help' for usage
if I attempt to run $ go run ./cmd/pilates -w
I can not suppress the help being printed or return custom messages. Maybe worse the error is hardcoded so I end up with the error printed twice without me being able to stop it.
$ go run ./cmd/pilates -w
Error: flag provided but not defined: -w <--- #1
Pilates
v0.0.1 - Ultimate Pilates Machine
Flags:
-help
Get help on the 'pilates' command.
Error: flag provided but not defined: -w <--- #2
my proposal is all or anything of the following:
a. have an option to suppress printing help on undefined flags.
cli.SuppresHelp()
b. custom undefined flag errors. Return golang err followed by a custom error message. examples:
$ git -w
unknown option: -w
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
or
$ kubectl -w
Error: unknown shorthand flag: 'w' in -w
See 'kubectl --help' for usage.
c. fix what seems to be buggy behaviour in line 83 command.go
by removing the fmt.Print. https://github.com/leaanthony/clir/blob/3b847b276c5042f799993c62d1219896677c2d26/command.go#L83