Skip to content

Commit

Permalink
Merge pull request #124 from kchason/custom-help-text
Browse files Browse the repository at this point in the history
Custom Help Text
  • Loading branch information
Mzack9999 authored Jul 24, 2023
2 parents dbf9da1 + 9ad0a8d commit cde1514
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
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 @@ -38,6 +38,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

0 comments on commit cde1514

Please sign in to comment.