-
-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Description() on subcommands #167
Conversation
Hello @alexflint, I'll understand if your time is short, but I think this will be a quick review, to close a long-lasting feature request. Don't hesitate to directly push adjustments on the branch if your time is limited. Closing this issue is more important to me than being right. |
Thank you for putting this together @xvello. I should be able to find time to think this through soon, though I will be on silent retreat for a week starting tomorrow. |
Hello @alexflint, did you get a chance to take a look at this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR @xvello but I think we need a somewhat different approach. See my comment below.
@@ -218,7 +218,15 @@ func (p *Parser) writeHelpForSubcommand(w io.Writer, cmd *command) { | |||
} | |||
} | |||
|
|||
if p.description != "" { | |||
var description string | |||
if v := p.val(cmd.dest); v.IsValid() && !isZero(v) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it makes sense to include !isZero(v)
in here because v.IsValid
already checks that (see https://cs.opensource.google/go/go/+/refs/tags/go1.17.5:src/reflect/value.go;l=1435)
The basic issue with this approach is that it won't work if the struct for this subcommand has a parent that is nil, since in that case we will end up here and then v.IsValid()
will return true.
It would be better to store the type for each subcommand in the command
struct and then instantiate a new struct of that type right here and check whether it satisfies the Described
interface
Closing as I moved to https://github.com/alecthomas/kong in the meantime. |
A follow-up on #156 to address #139.
writeHelpForSubcommand
checks whether the given subcommand implements theDescribed
interface. If it does not, we fallback to the main command's description.Testing it
Limitation
Due to how the commands and args are tokenized, the invocation below describes the main command instead of the
one
subcommand. I am not sure whether that's desirable or not.