Skip to content
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

Closed
wants to merge 1 commit into from
Closed

Support Description() on subcommands #167

wants to merge 1 commit into from

Conversation

xvello
Copy link

@xvello xvello commented Oct 2, 2021

A follow-up on #156 to address #139.

writeHelpForSubcommand checks whether the given subcommand implements the Described interface. If it does not, we fallback to the main command's description.

Testing it

package main
import "github.com/alexflint/go-arg"

func main() {
	arg.MustParse(new(rootCommand))
}

type rootCommand struct {
	One *firstCmd `arg:"subcommand:one"`
	Two *secondCmd `arg:"subcommand:two"`
}
func (*rootCommand) Description() string {
	return "main description"
}

type firstCmd struct {}
func (c *firstCmd) Description() string {
	return "subcommand description"
}

type secondCmd struct {}
± go run test/main.go --help | grep desc
main description
± go run test/main.go one --help | grep desc
subcommand description
± go run test/main.go two --help | grep desc
main description

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.

± go run test/main.go --help one | grep desc
main description

@xvello
Copy link
Author

xvello commented Oct 14, 2021

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.

@alexflint
Copy link
Owner

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.

@xvello
Copy link
Author

xvello commented Dec 16, 2021

Hello @alexflint, did you get a chance to take a look at this PR?

Copy link
Owner

@alexflint alexflint left a 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) {
Copy link
Owner

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

@xvello xvello closed this Feb 7, 2022
@xvello xvello deleted the feature/subcommand-description branch February 7, 2022 16:11
@xvello
Copy link
Author

xvello commented Feb 7, 2022

Closing as I moved to https://github.com/alecthomas/kong in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants