-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix:(issue_2234): Include empty argument #2235
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
Open
bbadour
wants to merge
5
commits into
urfave:main
Choose a base branch
from
bbadour:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+166
−10
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ed40bdb
Make externally-defined flags more robust.
bbadour c0f321b
chore(deps): bump actions/checkout from 5 to 6
dependabot[bot] f9ae5da
chore(deps): bump mkdocs-material in the python-packages group
dependabot[bot] 33f33f4
Fix:(issue_2234): Include empty argument
bbadour 5905455
Merge branch 'main' into main
dearchap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,14 +1,72 @@ | ||
| package cli | ||
|
|
||
| import "flag" | ||
| import ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this from PR |
||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "reflect" | ||
| "strings" | ||
| ) | ||
|
|
||
| var _ Value = (*externalValue)(nil) | ||
|
|
||
| // -- Value Value | ||
| type externalValue struct { | ||
| e *extFlag | ||
| } | ||
|
|
||
| // Below functions are to satisfy the flag.Value interface | ||
|
|
||
| func (ev *externalValue) Set(s string) error { | ||
| if ev != nil && ev.e.f != nil { | ||
| return ev.e.f.Value.Set(s) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (ev *externalValue) Get() any { | ||
| if ev != nil && ev.e.f != nil { | ||
| return ev.e.f.Value.(flag.Getter).Get() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (ev *externalValue) String() string { | ||
| if ev != nil && ev.e.f != nil { | ||
| return ev.e.String() | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| func (ev *externalValue) IsBoolFlag() bool { | ||
| if ev == nil || ev.e.f == nil { | ||
| return false | ||
| } | ||
| bf, ok := ev.e.f.Value.(boolFlag) | ||
| return ok && bf.IsBoolFlag() | ||
| } | ||
|
|
||
| var _ Flag = (*extFlag)(nil) | ||
| var _ ActionableFlag = (*extFlag)(nil) | ||
| var _ CategorizableFlag = (*extFlag)(nil) | ||
| var _ DocGenerationFlag = (*extFlag)(nil) | ||
| var _ DocGenerationMultiValueFlag = (*extFlag)(nil) | ||
| var _ LocalFlag = (*extFlag)(nil) | ||
| var _ RequiredFlag = (*extFlag)(nil) | ||
| var _ VisibleFlag = (*extFlag)(nil) | ||
|
|
||
| type extFlag struct { | ||
| f *flag.Flag | ||
| f *flag.Flag | ||
| category string | ||
| } | ||
|
|
||
| func (e *extFlag) PreParse() error { | ||
| if e.f.DefValue != "" { | ||
| return e.Set("", e.f.DefValue) | ||
| // suppress errors for write-only external flags that always return nil | ||
| if err := e.Set("", e.f.DefValue); err != nil && e.f.Value.(flag.Getter).Get() != nil { | ||
| // wrap error with some context for the user | ||
| return fmt.Errorf("external flag --%s default %q: %w", e.f.Name, e.f.DefValue, err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
|
|
@@ -30,6 +88,43 @@ | |
| return []string{e.f.Name} | ||
| } | ||
|
|
||
| // IsBoolFlag returns whether the flag doesn't need to accept args | ||
| func (e *extFlag) IsBoolFlag() bool { | ||
| if e == nil || e.f == nil { | ||
| return false | ||
| } | ||
| return (&externalValue{e}).IsBoolFlag() | ||
| } | ||
|
|
||
| // IsDefaultVisible returns true if the flag is not hidden, otherwise false | ||
| func (e *extFlag) IsDefaultVisible() bool { | ||
| return true | ||
| } | ||
|
|
||
| // IsLocal returns false if flag needs to be persistent across subcommands | ||
| func (e *extFlag) IsLocal() bool { | ||
| return false | ||
| } | ||
|
|
||
| // IsMultiValueFlag returns true if the value type T can take multiple | ||
| // values from cmd line. This is true for slice and map type flags | ||
| func (e *extFlag) IsMultiValueFlag() bool { | ||
| if e == nil || e.f == nil { | ||
| return false | ||
| } | ||
| // TBD how to specify | ||
| if reflect.TypeOf(e.f.Value) == nil { | ||
| return false | ||
| } | ||
| kind := reflect.TypeOf(e.f.Value).Kind() | ||
| return kind == reflect.Slice || kind == reflect.Map | ||
| } | ||
|
|
||
| // IsRequired returns whether or not the flag is required | ||
| func (e *extFlag) IsRequired() bool { | ||
| return false | ||
| } | ||
|
|
||
| func (e *extFlag) IsSet() bool { | ||
| return false | ||
| } | ||
|
|
@@ -61,3 +156,61 @@ | |
| func (e *extFlag) GetEnvVars() []string { | ||
| return nil | ||
| } | ||
|
|
||
| // RunAction executes flag action if set | ||
| func (e *extFlag) RunAction(ctx context.Context, cmd *Command) error { | ||
| return nil | ||
| } | ||
|
|
||
| // TypeName returns the type of the flag. | ||
| func (e *extFlag) TypeName() string { | ||
| if e == nil || e.f == nil { | ||
| return "" | ||
| } | ||
| ty := reflect.TypeOf(e.f.Value) | ||
| if ty == nil { | ||
| return "" | ||
| } | ||
| // convert the typename to generic type | ||
| convertToGenericType := func(name string) string { | ||
| prefixMap := map[string]string{ | ||
| "float": "float", | ||
| "int": "int", | ||
| "uint": "uint", | ||
| } | ||
| for prefix, genericType := range prefixMap { | ||
| if strings.HasPrefix(name, prefix) { | ||
| return genericType | ||
| } | ||
| } | ||
| return strings.ToLower(name) | ||
| } | ||
|
|
||
| switch ty.Kind() { | ||
| // if it is a Slice, then return the slice's inner type. Will nested slices be used in the future? | ||
| case reflect.Slice: | ||
| elemType := ty.Elem() | ||
| return convertToGenericType(elemType.Name()) | ||
| // if it is a Map, then return the map's key and value types. | ||
| case reflect.Map: | ||
| keyType := ty.Key() | ||
| valueType := ty.Elem() | ||
| return fmt.Sprintf("%s=%s", convertToGenericType(keyType.Name()), convertToGenericType(valueType.Name())) | ||
| default: | ||
| return convertToGenericType(ty.Name()) | ||
| } | ||
| } | ||
|
|
||
| // GetCategory returns the category of the flag | ||
| func (e *extFlag) GetCategory() string { | ||
| if e == nil { | ||
| return "" | ||
| } | ||
| return e.category | ||
| } | ||
|
|
||
| func (e *extFlag) SetCategory(c string) { | ||
| if e != nil { | ||
| e.category = c | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Please remove this change from this PR.