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

ffcli.DefaultUsageFunc: Support flag help placeholders #106

Merged
merged 1 commit into from
May 9, 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
27 changes: 24 additions & 3 deletions ffcli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,33 @@ func DefaultUsageFunc(c *Command) string {
space = "="
}

def := f.DefValue
if def == "" {
// If the help text contains backticks,
// e.g. "foo `bar` baz"`, we'll get:
//
// argname = "bar"
// usage = "foo bar baz"
//
// Otherwise, it's an educated guess for a placeholder,
// or an empty string if one couldn't be determined.
argname, usage := flag.UnquoteUsage(f)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to be the major change, is there some prior art that uses UnquoteUsage in this way?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently flag.FlagSet lol 😮‍💨


// For the argument name printed in the help,
// the order of preference is:
//
// 1. the default value
// 2. the back-quoted name from the help text
// 3. the '...' placeholder
var def string
switch {
case f.DefValue != "":
def = f.DefValue
case argname != "":
def = argname
default:
def = "..."
}

fmt.Fprintf(tw, " -%s%s%s\t%s\n", f.Name, space, def, f.Usage)
fmt.Fprintf(tw, " -%s%s%s\t%s\n", f.Name, space, def, usage)
})
tw.Flush()
fmt.Fprintf(&b, "\n")
Expand Down
68 changes: 62 additions & 6 deletions ffcli/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,62 @@ func TestIssue57(t *testing.T) {
}
}

func TestDefaultUsageFuncFlagHelp(t *testing.T) {
t.Parallel()

for _, testcase := range []struct {
name string // name of test case
def string // default value, if any
help string // help text for flag
want string // expected usage text
}{
{
name: "plain text",
help: "does stuff",
want: "-x string does stuff",
},
{
name: "placeholder",
help: "reads from `file` instead of stdout",
want: "-x file reads from file instead of stdout",
},
{
name: "default",
def: "www",
help: "path to output directory",
want: "-x www path to output directory",
},
{
name: "default with placeholder",
def: "www",
help: "path to output `directory`",
want: "-x www path to output directory",
},
} {
testcase := testcase
t.Run(testcase.name, func(t *testing.T) {
t.Parallel()

fset := flag.NewFlagSet(t.Name(), flag.ContinueOnError)
fset.String("x", testcase.def, testcase.help)

usage := ffcli.DefaultUsageFunc(&ffcli.Command{
FlagSet: fset,
})

// Discard everything before the FLAGS section.
_, flagUsage, ok := strings.Cut(usage, "\nFLAGS\n")
if !ok {
t.Fatalf("FLAGS section not found in:\n%s", usage)
}

assertMultilineString(t,
strings.TrimSpace(testcase.want),
strings.TrimSpace(flagUsage))
})
}
}

func ExampleCommand_Parse_then_Run() {
// Assume our CLI will use some client that requires a token.
type FooClient struct {
Expand Down Expand Up @@ -543,10 +599,10 @@ USAGE
Some long help.

FLAGS
-b=false bool
-d 0s time.Duration
-f 0 float64
-i 0 int
-s ... string
-x ... collection of strings (repeatable)
-b=false bool
-d 0s time.Duration
-f 0 float64
-i 0 int
-s string string
-x ... collection of strings (repeatable)
`) + "\n\n"