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

Custom Help Text #124

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# IDE Settings
/.idea
/.vscode
/.vs
1 change: 1 addition & 0 deletions examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func main() {
flagSet.SizeVarP(&testOptions.fileSize, "max-size", "ms", "", "max file size"),
flagSet.DurationVar(&testOptions.duration, "timeout", time.Hour, "timeout"),
)
flagSet.SetCustomHelpText("EXAMPLE USAGE:\ngo run ./examples/basic [OPTIONS]")

if err := flagSet.Parse(); err != nil {
log.Fatal(err)
Expand Down
20 changes: 13 additions & 7 deletions goflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type FlagSet struct {
CaseSensitive bool
Marshal bool
description string
customHelpText string
flagKeys InsertionOrderedMap
groups []groupData
CommandLine *flag.FlagSet
Expand Down Expand Up @@ -82,6 +83,11 @@ func (flagSet *FlagSet) SetDescription(description string) {
flagSet.description = description
}

// SetCustomHelpText sets the help text for a flagSet to a value. This variable appends text to the default help text.
func (flagSet *FlagSet) SetCustomHelpText(helpText string) {
flagSet.customHelpText = helpText
}

// SetGroup sets a group with name and description for the command line options
//
// The order in which groups are passed is also kept as is, similar to flags.
Expand Down Expand Up @@ -501,8 +507,7 @@ func (flagSet *FlagSet) usageFunc() {

writer := tabwriter.NewWriter(cliOutput, 0, 0, 1, ' ', 0)

// If user has specified group with help and we have groups, return
// with it's usage function
// If a user has specified a group with help, and we have groups, return with the tool's usage function
if len(flagSet.groups) > 0 && len(os.Args) == 3 {
group := flagSet.getGroupbyName(strings.ToLower(os.Args[2]))
if group.name != "" {
Expand All @@ -521,6 +526,11 @@ func (flagSet *FlagSet) usageFunc() {
} else {
flagSet.usageFuncInternal(writer)
}

// If there is a custom help text specified, print it
if !isEmpty(flagSet.customHelpText) {
fmt.Fprintf(cliOutput, "\n%s\n", flagSet.customHelpText)
}
}

func (flagSet *FlagSet) getGroupbyName(name string) groupData {
Expand Down Expand Up @@ -641,10 +651,6 @@ func (u *uniqueDeduper) isUnique(data *FlagData) bool {
return true
}

func isNotBlank(value string) bool {
return len(strings.TrimSpace(value)) != 0
}

func createUsageString(data *FlagData, currentFlag *flag.Flag) string {
valueType := reflect.TypeOf(currentFlag.Value)

Expand Down Expand Up @@ -703,7 +709,7 @@ func createUsageFlagNames(data *FlagData) string {

var validFlags []string
addValidParam := func(value string) {
if isNotBlank(value) {
if !isEmpty(value) {
validFlags = append(validFlags, fmt.Sprintf("-%s", value))
}
}
Expand Down