go-arg icon indicating copy to clipboard operation
go-arg copied to clipboard

Description() doesn't work for sub commands

Open gpie3k opened this issue 4 years ago • 3 comments

Description() defined for subcommands is not used for help text.

I think only root arg is considered.

parse.go line 232:

if dest, ok := dest.(Described); ok {
	p.description = dest.Description()
}

gpie3k avatar Feb 04 '21 15:02 gpie3k

Yes, it's true. It is a significant shortcoming of the API. Thank you for investigating it. I need to find time to develop an API that can support this.

alexflint avatar Apr 17 '21 04:04 alexflint

Fixed in #156

alexflint avatar Sep 24 '21 16:09 alexflint

I was looking at this, and the main issue seems to be that description exists on Parser, instead of on command.

Moving the field is fairly easy, but then we still need to populate to it somehow. This means we'd probably need to do it in cmdFromStruct, but I can't quite figure out how to get access to Description() there, as I have a name string, dest path, and t reflect.Type.

I can get to the method with meth, ok := t.MethodByName("Description") and if the struct you passed in has a Description on it we'll now have the method. The thing I can't figure out is how to then correctly do meth.Func.Call([]reflect.Value{reflect.ValueOf(...what goes here...)}).

If that can be made to work, then the output of the Description() can de assigned to cmd.description and it looks like all you then need is to change writeHelpForSubcommand to do

if p.cmd.description != "" {
		fmt.Fprintln(w, p.cmd.description)
}

Epilogue can probably be made to work the same way, so those too would be per command instead of global on the parser.

daenney avatar Oct 13 '22 10:10 daenney