@@ -14,6 +14,8 @@ import (
1414 "unicode"
1515 "unicode/utf8"
1616 "unsafe"
17+
18+ "go.followtheprocess.codes/cli/flag"
1719)
1820
1921const (
@@ -63,18 +65,10 @@ const (
6365 boolTrue = "true"
6466)
6567
66- // NoShortHand should be passed as the "short" argument to [New] if the desired flag
67- // should be the long hand version only e.g. --count, not -c/--count.
68- const NoShortHand = rune (- 1 )
69-
7068var _ Value = Flag [string ]{} // This will fail if we violate our Value interface
7169
72- // Count is a type used for a flag who's job is to increment a counter, e.g. a "verbosity"
73- // flag may be passed "-vvv" which should increase the verbosity level to 3.
74- type Count uint
75-
7670// Flag represents a single command line flag.
77- type Flag [T Flaggable ] struct {
71+ type Flag [T flag. Flaggable ] struct {
7872 value * T // The actual stored value
7973 name string // The name of the flag as appears on the command line, e.g. "force" for a --force flag
8074 usage string // One line description of the flag, e.g. "Force deletion without confirmation"
@@ -90,7 +84,7 @@ type Flag[T Flaggable] struct {
9084//
9185// var force bool
9286// flag.New(&force, "force", 'f', false, "Force deletion without confirmation")
93- func New [T Flaggable ](p * T , name string , short rune , value T , usage string ) (Flag [T ], error ) {
87+ func New [T flag. Flaggable ](p * T , name string , short rune , value T , usage string ) (Flag [T ], error ) {
9488 if err := validateFlagName (name ); err != nil {
9589 return Flag [T ]{}, fmt .Errorf ("invalid flag name %q: %w" , name , err )
9690 }
@@ -173,7 +167,7 @@ func (f Flag[T]) String() string { //nolint:cyclop // No other way of doing this
173167 return formatInt (typ )
174168 case int64 :
175169 return formatInt (typ )
176- case Count :
170+ case flag. Count :
177171 return formatUint (typ )
178172 case uint :
179173 return formatUint (typ )
@@ -249,7 +243,7 @@ func (f Flag[T]) Type() string { //nolint:cyclop // No other way of doing this r
249243 return typeInt32
250244 case int64 :
251245 return typeInt64
252- case Count :
246+ case flag. Count :
253247 return typeCount
254248 case uint :
255249 return typeUint
@@ -360,11 +354,11 @@ func (f Flag[T]) Set(str string) error { //nolint:gocognit,cyclop // No other wa
360354 * f .value = * cast [T ](& val )
361355
362356 return nil
363- case Count :
357+ case flag. Count :
364358 // We have to do a bit of custom stuff here as an increment is a read and write op
365359 // First read the current value of the flag and cast it to a Count so we
366360 // can increment it
367- current , ok := any (* f .value ).(Count )
361+ current , ok := any (* f .value ).(flag. Count )
368362 if ! ok {
369363 // This basically shouldn't ever happen but it's easy enough to handle nicely
370364 return errBadType (* f .value )
@@ -378,7 +372,7 @@ func (f Flag[T]) Set(str string) error { //nolint:gocognit,cyclop // No other wa
378372 return errParse (f .name , str , typ , err )
379373 }
380374
381- newValue := current + Count (val )
375+ newValue := current + flag . Count (val )
382376 * f .value = * cast [T ](& newValue )
383377
384378 return nil
@@ -698,7 +692,7 @@ type signed interface {
698692// unsigned is the same as constraints.Unsigned (with Count mixed in) but we don't have to depend
699693// on golang/x/exp.
700694type unsigned interface {
701- uint | uint8 | uint16 | uint32 | uint64 | uintptr | Count
695+ uint | uint8 | uint16 | uint32 | uint64 | uintptr | flag. Count
702696}
703697
704698// cast converts a *T1 to a *T2, we use it here when we know (via generics and compile time checks)
@@ -774,7 +768,7 @@ func validateFlagName(name string) error {
774768// it enforces only a single character, so all we have to do is make sure it's a valid ASCII character.
775769func validateFlagShort (short rune ) error {
776770 // If it's the marker for long hand only, this is fine
777- if short == NoShortHand {
771+ if short == flag . NoShortHand {
778772 return nil
779773 }
780774
@@ -791,7 +785,7 @@ func validateFlagShort(short rune) error {
791785
792786// errParse is a helper to quickly return a consistent error in the face of flag
793787// value parsing errors.
794- func errParse [T Flaggable ](name , str string , typ T , err error ) error {
788+ func errParse [T flag. Flaggable ](name , str string , typ T , err error ) error {
795789 return fmt .Errorf (
796790 "flag %q received invalid value %q (expected %T), detail: %w" ,
797791 name ,
@@ -803,7 +797,7 @@ func errParse[T Flaggable](name, str string, typ T, err error) error {
803797
804798// errParseSlice is like errParse but for []T flags where the error message
805799// needs to be a bit more specific.
806- func errParseSlice [T Flaggable ](name , str string , typ T , err error ) error {
800+ func errParseSlice [T flag. Flaggable ](name , str string , typ T , err error ) error {
807801 return fmt .Errorf (
808802 "flag %q (type %T) cannot append element %q: %w" ,
809803 name ,
@@ -815,7 +809,7 @@ func errParseSlice[T Flaggable](name, str string, typ T, err error) error {
815809
816810// errBadType makes a consistent error in the face of a bad type
817811// assertion.
818- func errBadType [T Flaggable ](value T ) error {
812+ func errBadType [T flag. Flaggable ](value T ) error {
819813 return fmt .Errorf ("bad value %v, could not cast to %T" , value , value )
820814}
821815
@@ -911,15 +905,15 @@ func formatStringSlice(slice []string) string {
911905// zero value being nil. The primary use of isZeroIsh is to determine whether or not
912906// a default value is worth displaying to the user in the help text, and an empty slice
913907// is probably not.
914- func isZeroIsh [T Flaggable ](value T ) bool { //nolint:cyclop // Not much else we can do here
908+ func isZeroIsh [T flag. Flaggable ](value T ) bool { //nolint:cyclop // Not much else we can do here
915909 // Note: all the slice values ([]T) are in their own separate branches because if you
916910 // combine them, the resulting value in the body of the case block is 'any' and
917911 // you cannot do len(any)
918912 switch typ := any (value ).(type ) {
919913 case int , int8 , int16 , int32 , int64 , uint , uint8 , uint16 , uint32 , uint64 , uintptr , float32 , float64 :
920914 return typ == 0
921- case Count :
922- return typ == Count (0 )
915+ case flag. Count :
916+ return typ == flag . Count (0 )
923917 case string :
924918 return typ == ""
925919 case bool :
0 commit comments