provide better error on invalid flag
Add a extra `See 'podman command --help'` to the error output. With this patch you now get: ``` $ podman run -h Error: flag needs an argument: 'h' in -h See 'podman run --help' ``` Fixes #13082 Fixes #13002 Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
parent
d224a0f8ac
commit
44d037898e
|
@ -72,6 +72,8 @@ func parseCommands() *cobra.Command {
|
||||||
}
|
}
|
||||||
parent.AddCommand(c.Command)
|
parent.AddCommand(c.Command)
|
||||||
|
|
||||||
|
c.Command.SetFlagErrorFunc(flagErrorFuncfunc)
|
||||||
|
|
||||||
// - templates need to be set here, as PersistentPreRunE() is
|
// - templates need to be set here, as PersistentPreRunE() is
|
||||||
// not called when --help is used.
|
// not called when --help is used.
|
||||||
// - rootCmd uses cobra default template not ours
|
// - rootCmd uses cobra default template not ours
|
||||||
|
@ -84,5 +86,11 @@ func parseCommands() *cobra.Command {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rootCmd.SetFlagErrorFunc(flagErrorFuncfunc)
|
||||||
return rootCmd
|
return rootCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func flagErrorFuncfunc(c *cobra.Command, e error) error {
|
||||||
|
e = fmt.Errorf("%w\nSee '%s --help'", e, c.CommandPath())
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
|
@ -88,7 +88,8 @@ function setup() {
|
||||||
|
|
||||||
# ...but no matter what, --remote is never allowed after subcommand
|
# ...but no matter what, --remote is never allowed after subcommand
|
||||||
PODMAN="${podman_non_remote} ${podman_args[@]}" run_podman 125 version --remote
|
PODMAN="${podman_non_remote} ${podman_args[@]}" run_podman 125 version --remote
|
||||||
is "$output" "Error: unknown flag: --remote" "podman version --remote"
|
is "$output" "Error: unknown flag: --remote
|
||||||
|
See 'podman version --help'" "podman version --remote"
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "podman-remote: defaults" {
|
@test "podman-remote: defaults" {
|
||||||
|
|
|
@ -12,4 +12,18 @@ load helpers
|
||||||
run_podman run --rm --label 'true="false"' $IMAGE true
|
run_podman run --rm --label 'true="false"' $IMAGE true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "podman flag error" {
|
||||||
|
local name="podman"
|
||||||
|
if is_remote; then
|
||||||
|
name="podman-remote"
|
||||||
|
fi
|
||||||
|
run_podman 125 run -h
|
||||||
|
is "$output" "Error: flag needs an argument: 'h' in -h
|
||||||
|
See '$name run --help'" "expected error output"
|
||||||
|
|
||||||
|
run_podman 125 bad --invalid
|
||||||
|
is "$output" "Error: unknown flag: --invalid
|
||||||
|
See '$name --help'" "expected error output"
|
||||||
|
}
|
||||||
|
|
||||||
# vim: filetype=sh
|
# vim: filetype=sh
|
||||||
|
|
Loading…
Reference in New Issue