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

while print flag , the placeholder if need but not set. #2043

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func ExampleCommand_Run_appHelp() {
// help, h Shows a list of commands or help for one command
//
// GLOBAL OPTIONS:
// --name value a name to say (default: "bob")
// --name string a name to say (default: "bob")
// --help, -h show help
// --version, -v print the version
}
Expand Down
14 changes: 12 additions & 2 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ type LocalFlag interface {
IsLocal() bool
}

// FlagType is an interface to detect if a flag is a string, bool, etc.
type FlagType interface {
jokemanfire marked this conversation as resolved.
Show resolved Hide resolved
GetFlagType() string
jokemanfire marked this conversation as resolved.
Show resolved Hide resolved
}

func newFlagSet(name string, flags []Flag) (*flag.FlagSet, error) {
set := flag.NewFlagSet(name, flag.ContinueOnError)

Expand Down Expand Up @@ -304,9 +309,14 @@ func stringifyFlag(f Flag) string {
}
placeholder, usage := unquoteUsage(df.GetUsage())
needsPlaceholder := df.TakesValue()

// if needsPlaceholder is true, placeholder is empty
if needsPlaceholder && placeholder == "" {
placeholder = defaultPlaceholder
// try to get type from flag
if v1, ok := f.(FlagType); ok && v1.GetFlagType() != "" {
jokemanfire marked this conversation as resolved.
Show resolved Hide resolved
placeholder = v1.GetFlagType()
jokemanfire marked this conversation as resolved.
Show resolved Hide resolved
} else {
placeholder = defaultPlaceholder
}
}

defaultValueString := ""
Expand Down
14 changes: 14 additions & 0 deletions flag_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ func (f *FlagBase[T, C, V]) GetValue() string {
return fmt.Sprintf("%v", f.Value)
}

// GetFlagType returns the type of the flag.
func (f *FlagBase[T, C, V]) GetFlagType() string {
ty := reflect.TypeOf(f.Value)
if ty == nil {
return ""
}
// if it is a Slice, then return the slice's inner type. Will nested slices be used in the future?
if ty.Kind() == reflect.Slice {
jokemanfire marked this conversation as resolved.
Show resolved Hide resolved
elemType := ty.Elem()
return elemType.Name()
}
return ty.Name()
}

// Apply populates the flag given the flag set and environment
func (f *FlagBase[T, C, V]) Apply(set *flag.FlagSet) error {
tracef("apply (flag=%[1]q)", f.Name)
Expand Down
Loading
Loading