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

Fix an issue where cli.FlagCount wasn't being parsed correctly #116

Merged
merged 1 commit into from
Nov 17, 2024
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
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
Loading