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
Feature:(issue_269) Allow external package flag definitions #1540
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9d99742
Feature:(issue_269) Add compatibility with external package flag defi…
dearchap f909a31
Add tests
dearchap 7793b0a
Add defer to remove global flag
dearchap 7554e6b
Use ptr to receiver for extFlag
dearchap aa79343
Add const for flag prefix to ignore
dearchap File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cli | ||
|
||
import "flag" | ||
|
||
type extFlag struct { | ||
f *flag.Flag | ||
} | ||
|
||
func (e extFlag) Apply(fs *flag.FlagSet) error { | ||
fs.Var(e.f.Value, e.f.Name, e.f.Usage) | ||
return nil | ||
} | ||
|
||
func (e extFlag) Names() []string { | ||
return []string{e.f.Name} | ||
} | ||
|
||
func (e extFlag) IsSet() bool { | ||
return false | ||
} | ||
|
||
func (e extFlag) String() string { | ||
return FlagStringer(e) | ||
} | ||
|
||
func (e extFlag) IsVisible() bool { | ||
return true | ||
} | ||
|
||
func (e extFlag) TakesValue() bool { | ||
return false | ||
} | ||
|
||
func (e extFlag) GetUsage() string { | ||
return e.f.Usage | ||
} | ||
|
||
func (e extFlag) GetValue() string { | ||
return e.f.Value.String() | ||
} | ||
|
||
func (e extFlag) GetDefaultText() string { | ||
return e.f.DefValue | ||
} | ||
|
||
func (e extFlag) GetEnvVars() []string { | ||
return nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'm good with specifically ignoring flags with
"test."
prefix in this implementation 👍🏼 I looked briefly and couldn't find any exported code that could be used to get this prefix from go libraries 🤷🏼What's the reasoning behind using a
extFlag{f}
instead of&extFlag{f}
and having theextFlag
methods use a pointer receiver?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.
We need the test prefix check because otherwise when we run unit tests those flags get added !!!!
Also extFlag seemed better since we are not necessarily modifying any aspect of the underlying flag
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.
@dearchap I'm in favor of filtering out `"test." in general 👍🏼 My thinking was more around if we could read this value from somewhere instead of having it hard-coded here 🤷🏼
As for
extFlag
, I meant that I wondered if you meant to use the struct and method receivers as structs rather than pointers to structs. In this implementation, there's going to be a lot more implicit copying of structs, IIUC.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.
@meatballhat do you want it to be defined as an App field or as a library var ?
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.
@meatballhat It seems that the go is also the hard-coded way to use directly
https://github.com/golang/go/blob/7e72d384d66f48a78289edc6a7d1dc6ab878f990/src/cmd/go/internal/test/test.go#L1351-L1367
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.
@dearchap Maybe he means use pointer receivers(*extFlag) rather than value receivers(extFlag) implement
Flag
interface to avoid unnecessary data duplication.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.
No no that is fine. I meant the "test" prefix.
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.
@meatballhat I updated the extFlag to be pointer receiver.