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

flag: no way to access name of flag.FlagSet #21888

Closed
lukechampine opened this issue Sep 14, 2017 · 12 comments
Closed

flag: no way to access name of flag.FlagSet #21888

lukechampine opened this issue Sep 14, 2017 · 12 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@lukechampine
Copy link
Contributor

flag.NewFlagSet takes a name parameter in its constructor, but does not provide a way to access this name afterward. I suggest either exporting the Name field of flag.FlagSet or adding a Name() method. Happy to implement either of these if they are approved.

Motivation:
I wrote a small package that implements CLI subcommands via flag.FlagSet-based lookup trees, where the name of the flag.FlagSet is the name of the subcommand. Since it is currently impossible to access the name of a flag.FlagSet, I resorted to using unsafe.Pointer to read the name field. Obviously I would like to replace this with something more robust as soon as possible.
I acknowledge that this isn't a common problem, but I think the proposal would be reasonable even without a motivating example. The name of a flag.FlagSet is one of its defining properties, so it makes sense for it to have an associated getter. (The same argument could be made for errorHandling, I suppose.)

@ianlancetaylor ianlancetaylor added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Sep 14, 2017
@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Sep 14, 2017
@cznic
Copy link
Contributor

cznic commented Sep 15, 2017

type FlagSet struct {
        *flag.FlagSet
        Name string
}

func NewFlagSet(name string, errorHandling flag.ErrorHandling) *FlagSet {
        return &FlagSet{flag.NewFlagSet(name, errorHandling), name}
}

edit: premature click.

@lukechampine
Copy link
Contributor Author

@cznic: that's a reasonable alternative, but it breaks interoperability with the existing flag.FlagSet type. That is, users of my API now need to wrap their existing flag.FlagSet in a new type, and the new type can't be passed (directly, at least) to functions that take a flag.FlagSet. Not the worst thing in the world, but I don't see why such a workaround should be necessary when the flag.FlagSet type already contains a name field that would serve perfectly well.

@gopherbot
Copy link

Change https://golang.org/cl/70391 mentions this issue: flag: add FlagSet.Name, FlagSet.ErrorHandling

@ghost
Copy link

ghost commented Oct 17, 2017

@ianlancetaylor Would adding the proposal flag on this help in getting a decision made?

@ianlancetaylor
Copy link
Contributor

Hard to say.

@ghost
Copy link

ghost commented Oct 18, 2017

Related: #17628

@rsc
Copy link
Contributor

rsc commented Oct 23, 2017

Per discussion with proposal review, OK.

@rsc rsc added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 23, 2017
@gopherbot gopherbot removed the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Oct 23, 2017
@ghost
Copy link

ghost commented Oct 23, 2017

@rsc Does the approval include exporting (*FlagSet).Output (#17628)?

@ianlancetaylor
Copy link
Contributor

@bontibon I think so.

@rsc
Copy link
Contributor

rsc commented Oct 25, 2017

Yes, that's OK too.

@lukechampine
Copy link
Contributor Author

For future reference: is it typical to submit a patch to Gerrit that implements a proposal before the proposal is accepted? I was kinda hoping to do this one myself and was surprised to see someone else submit a patch preemptively.

@ianlancetaylor
Copy link
Contributor

It is not typical, but also not forbidden. Sorry you didn't get a chance at it.

@golang golang locked and limited conversation to collaborators Oct 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants