google / subcommands

Go subcommand library.
Apache License 2.0
749 stars 48 forks source link

help command incorrectly locates flags in its result #37

Open fgm opened 2 years ago

fgm commented 2 years ago

How to reproduce

When, on program foo, a command bar usage includes both a flag (e.g. qux string) and a list of non-flag arguments, (e.g. foo <pid>), run foo help bar

What will happen

The command will display:

foo <pid> -qux string

But running the command that way, e.g. foo 42 -qux meaning will actually pass -qux and meaning as args in addition to 42, instead of interpreting them as flags, so the help is wrong.

Users actually need to run foo -qux meaning 42 instead.

What should have happened

The help should look like:

food -qux string <pid>

Problem cause

Due to the way explain is currently implemented, the only way to have flags be shown correctly in usage is to avoid including the command arguments, which is still wrong.

Suggested solutions

The first solution is more consistent with the existing package documentation, which explains that "Usage returns a long string explaining the command and giving usage information."

The second form requires less changes from existing users, for whom existing code will suddenly display the correct flags usage string.

The third solution would affect existing code the least but is a bit more complex and may lead to false positives when usage includes other references to the flags in the "long string" it returns, so may cause more errors in the end.