Skip to content

Commit

Permalink
Fix an issue where cli.FlagCount wasn't being parsed correctly (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Nov 17, 2024
1 parent 6360e79 commit 59bda57
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
12 changes: 12 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,18 @@ func TestHelp(t *testing.T) {
golden: "with-named-arguments.txt",
wantErr: false,
},
{
name: "with verbosity count",
options: []cli.Option{
cli.OverrideArgs([]string{"--help"}),
cli.Arg("src", "The file to copy", ""), // This one is required
cli.Arg("dest", "Destination to copy to", "./dest"), // This one is optional
cli.Flag(new(cli.FlagCount), "verbosity", 'v', 0, "Increase the verbosity level"),
cli.Run(func(cmd *cli.Command, args []string) error { return nil }),
},
golden: "with-verbosity-count.txt",
wantErr: false,
},
{
name: "with full description",
options: []cli.Option{
Expand Down
Binary file modified docs/img/quickstart.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion internal/flag/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (s *Set) Usage() (string, error) {
tab.Row(
" %s\t--%s\t%s\t%s\n",
colour.Bold(shorthand),
colour.Bold(flag.Name()),
colour.Bold(name),
flag.Type(),
flag.Usage(),
)
Expand Down
6 changes: 5 additions & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ const NoShortHand = flag.NoShortHand
// as e.g. a [time.Duration] is actually just an int64 underneath, likewise a [net.IP] is actually just []byte.
type Flaggable flag.Flaggable

// Note: this must be a type alias (FlagCount = flag.Count), not a newtype (FlagCount flag.Count)
// otherwise parsing does not work correctly as the flag package does not know how to parse
// a new type declared here.

// FlagCount is a type used for a flag who's job is to increment a counter, e.g. a "verbosity"
// flag may be passed "-vvv" which should increase the verbosity level to 3.
type FlagCount flag.Count
type FlagCount = flag.Count

// Option is a functional option for configuring a [Command].
type Option interface {
Expand Down
12 changes: 12 additions & 0 deletions testdata/TestHelp/with-verbosity-count.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
A placeholder for something cool

Usage: test [OPTIONS] SRC [DEST]

Arguments:
src The file to copy
dest Destination to copy to [default ./dest]

Options:
-h --help bool Show help for test
-v --verbosity count Increase the verbosity level
-V --version bool Show version info for test

0 comments on commit 59bda57

Please sign in to comment.