kong
kong copied to clipboard
Print usage on input without commands/args/flags
In the scenario of app invocation from terminal without any command/flags
> my-app
I'd expect the same output of:
> my-app --help
Instead, I have the error about the missing command.
my-app: error: expected one of "cmd1", "cmd2"
Is there a way/configuration to achieve my expected outcome?
The closest function I've found is kong.UsageOnError(), except it prints usage help for any error (when for any other type of error I'd expect to just print the specific error).
UsageOnError() is the solution, but what do you mean by "any other type of error"?
Hey, Thanks for the quick response!
Let me give you an example to clarify:
if I type:
> my-app
I'd expect to see the equivalent of passing the --help flag.
If instead I type an invalid command:
> my-app invalid-command
I'd expect to print the specific error message about using an invalid command but not the full usage help.
Does it make sense?
The behaviour I'm describing is also present in kingpin.
For the latter case, Kong should be printing the error followed by the help for that command, right?
But I think what you're suggesting is that if nothing is passed at all, print the help, otherwise just print an error. Kong doesn't have that facility, no. I'm not particularly against it, but I also don't find it that useful. As a user, 99% of the time if I've made an error I want to be able to see the help so I know what to do to fix my error.
I see.
You are right that often people need the help regardless.
I am also thinking about scenarios where the error is more like a typo; in that case the user just really needs a reminder of the most similar command (something kong already does) rather than the full help that is somewhat visually distracting from the core message (the recommendation).
Anyway thanks for the clarification! Fell free to close the issue if not relevant/keep if it's worthwhile exploring what we've been discussing.
I've subsequently moved the error message itself below the help, which makes it much clearer that there's an error.
Reopening because I think this would be useful as an option.
@alecthomas is this a feature you would still consider supporting? I would be happy to add it if so
Sure, if you'd like to make it an option i'd happily merge it!
Awesome, thanks! If you would like to reopen this I'll get started right away.
There's a simple workaround for this:
// If running without any extra arguments, default to the --help flag
if len(os.Args) < 2 {
os.Args = append(os.Args, "--help")
}
// Continue your usual Kong initialization
ctx := kong.Parse(&cli)
err := ctx.Run(&commands.Context{Debug: cli.Debug})
ctx.FatalIfErrorf(err)
...
Is there a way I can get the help message in the string, I need to print somewhere else instead of the stdout.