Skip to content
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
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ linters:
- lll # Auto formatters do this and what they can't do I don't care about
- maintidx # This is just the inverse of complexity... which is cyclop
- nestif # cyclop does this
- nlreturn # Similar to wsl, I think best left to judgement
- nonamedreturns # Named returns are often helpful, it's naked returns that are the issue
- paralleltest # I've never had Go tests take longer than a few seconds, it's fine
- varnamelen # Lots of false positives of things that are fine
- wrapcheck # Not every error must be wrapped
- wsl # Very aggressive, some of this I like but tend to do anyway

issues:
exclude-rules:
Expand Down
Binary file modified docs/img/namedargs.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
Binary file modified docs/img/subcommands.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions internal/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@

*p = value

// If the default value is not the zero value for the type, it is treated as
// significant and shown to the user
if !isZero(value) {
usage += fmt.Sprintf(" [default: %v]", value)
}

flag := Flag[T]{
value: p,
name: name,
Expand Down Expand Up @@ -625,3 +631,29 @@
return strconv.FormatFloat(float64(in), 'g', -1, bits)
}
}

// isZero reports whether value is the zero value for it's type.
func isZero[T Flaggable](value T) bool {
switch typ := any(value).(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64:
return typ == 0
case Count:
return typ == Count(0)
case string:
return typ == ""
case bool:
return !typ
case []byte:
return typ == nil
case time.Time:
var zero time.Time
return typ.Equal(zero)
case time.Duration:
var zero time.Duration
return typ == zero
case net.IP:
return typ == nil
default:
return false

Check warning on line 657 in internal/flag/flag.go

View check run for this annotation

Codecov / codecov/patch

internal/flag/flag.go#L656-L657

Added lines #L656 - L657 were not covered by tests
}
}
4 changes: 0 additions & 4 deletions internal/flag/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import (

// Set is a set of command line flags.
type Set struct {
// Note: flags and shorthands are different "views" to the same *Entry, the Entry
// is not duplicated, it's just two maps to the same pointer so we can look up
// using either

flags map[string]Value // The actual stored flags, can lookup by name
shorthands map[rune]Value // The flags by shorthand
args []string // Arguments minus flags or flag values
Expand Down
9 changes: 7 additions & 2 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,15 @@ func Allow(validator ArgValidator) Option {
return option(f)
}

// Flag is an [Option] that adds a flag to a [Command], storing its value in a variable.
// Flag is an [Option] that adds a flag to a [Command], storing its value in a variable via it's
// pointer 'p'.
//
// The variable is set when the flag is parsed during command execution. The value provided
// in the call to [Flag] is used as the default value.
// by the 'value' argument to [Flag] is used as the default value, which will be used if the
// flag value was not given via the command line.
//
// If the default value is not the zero value for the type T, the flags usage message will
// show the default value in the commands help text.
//
// To add a long flag only (e.g. --delete with no -d option), pass [NoShortHand] for "short".
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Commands:
sub2 Do another thing

Options:
N/A --count int Count something
N/A --count int Count something [default: -1]
-d --delete bool Delete something
-h --help bool Show help for test
-V --version bool Show version info for test
Expand Down
Loading