kong icon indicating copy to clipboard operation
kong copied to clipboard

Print usage on input without commands/args/flags

Open lucagrulla opened this issue 6 years ago • 16 comments

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).

lucagrulla avatar Jan 28 '19 20:01 lucagrulla

UsageOnError() is the solution, but what do you mean by "any other type of error"?

alecthomas avatar Jan 28 '19 21:01 alecthomas

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.

lucagrulla avatar Jan 28 '19 21:01 lucagrulla

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.

alecthomas avatar Jan 29 '19 00:01 alecthomas

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.

lucagrulla avatar Jan 29 '19 07:01 lucagrulla

I've subsequently moved the error message itself below the help, which makes it much clearer that there's an error.

alecthomas avatar Sep 10 '19 08:09 alecthomas

Reopening because I think this would be useful as an option.

alecthomas avatar Sep 11 '19 00:09 alecthomas

@alecthomas is this a feature you would still consider supporting? I would be happy to add it if so

hasheddan avatar Sep 09 '20 21:09 hasheddan

Sure, if you'd like to make it an option i'd happily merge it!

alecthomas avatar Sep 09 '20 22:09 alecthomas

Awesome, thanks! If you would like to reopen this I'll get started right away.

hasheddan avatar Sep 09 '20 22:09 hasheddan

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)
...

Dids avatar Aug 07 '22 09:08 Dids

Is there a way I can get the help message in the string, I need to print somewhere else instead of the stdout.

tommynanny avatar Apr 10 '23 23:04 tommynanny