diff options
| author | Tomas Aschan <[email protected]> | 2025-06-28 11:26:14 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-28 11:26:14 +0200 |
| commit | 1c62fb2813da5f1d1b893a49180a41b3f6be3262 (patch) | |
| tree | 289c9ab56575327b848a6dae879737cf9b1edb4d /bool_func_test.go | |
| parent | 8a6c85f2ae488081f953744f9edc414bac10ee45 (diff) | |
| parent | 1a4b5b2e5c7ee4a194cebc579bb34198187df73d (diff) | |
Merge pull request #431 from LeGEC/430-fix-usage-message-for-func-flags
fix usage message for func flags, fix arguments order
Diffstat (limited to 'bool_func_test.go')
| -rw-r--r-- | bool_func_test.go | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/bool_func_test.go b/bool_func_test.go index c81970a..c16be83 100644 --- a/bool_func_test.go +++ b/bool_func_test.go @@ -75,7 +75,7 @@ func TestBoolFuncCompat(t *testing.T) { args := []string{"--bflag", "--bflag=false", "--bflag=1", "--bflag=bar", "--bflag="} // It turns out that, even though the function is called "BoolFunc", - // the stanard flag package does not try to parse the value assigned to + // the standard flag package does not try to parse the value assigned to // that cli flag as a boolean. The string provided on the command line is // passed as is to the callback. // e.g: with "--bflag=not_a_bool" on the command line, the FlagSet does not @@ -106,7 +106,7 @@ func TestBoolFuncCompat(t *testing.T) { flagName := "bflag" args := []string{"--bflag", "--bflag=err", "--bflag=after"} - // test behavior of standard flag.Fset with an error triggere by the callback: + // test behavior of standard flag.Fset with an error triggered by the callback: // (note: as can be seen in 'runCase()', if the callback sees "err" as a value // for the bool flag, it will return an error) stdFSet := flag.NewFlagSet("std test", flag.ContinueOnError) @@ -145,3 +145,33 @@ func TestBoolFuncCompat(t *testing.T) { } }) } + +func TestBoolFuncUsage(t *testing.T) { + t.Run("regular func flag", func(t *testing.T) { + // regular boolfunc flag: + // expect to see '--flag1' followed by the usageMessage, and no mention of a default value + fset := NewFlagSet("unittest", ContinueOnError) + fset.BoolFunc("flag1", "usage message", func(s string) error { return nil }) + usage := fset.FlagUsagesWrapped(80) + + usage = strings.TrimSpace(usage) + expected := "--flag1 usage message" + if usage != expected { + t.Fatalf("unexpected generated usage message\n expected: %s\n got: %s", expected, usage) + } + }) + + t.Run("func flag with placeholder name", func(t *testing.T) { + // func flag, with a placeholder name: + // if usageMesage contains a placeholder, expect '--flag2 {placeholder}'; still expect no mention of a default value + fset := NewFlagSet("unittest", ContinueOnError) + fset.BoolFunc("flag2", "usage message with `name` placeholder", func(s string) error { return nil }) + usage := fset.FlagUsagesWrapped(80) + + usage = strings.TrimSpace(usage) + expected := "--flag2 name usage message with name placeholder" + if usage != expected { + t.Fatalf("unexpected generated usage message\n expected: %s\n got: %s", expected, usage) + } + }) +} |
