|
7 | 7 | "io/fs"
|
8 | 8 | "math"
|
9 | 9 | "os"
|
| 10 | + "reflect" |
10 | 11 | "strconv"
|
11 | 12 | "strings"
|
12 | 13 | "time"
|
@@ -37,8 +38,25 @@ type (
|
37 | 38 | }
|
38 | 39 | sharedOption func(*sharedSettings) error
|
39 | 40 | sharedOptions []sharedOption
|
| 41 | + |
| 42 | + // standard [flag.funcValue] extended |
| 43 | + // for [command.ValueNamer]. |
| 44 | + // (Because standard uses internal types |
| 45 | + // in a way we can't access; |
| 46 | + // see: [flag.UnquoteUsage]'s implementation.) |
| 47 | + genericFuncValue[T any] func(string) error |
40 | 48 | )
|
41 | 49 |
|
| 50 | +func (gf genericFuncValue[T]) Set(s string) error { return gf(s) } |
| 51 | +func (gf genericFuncValue[T]) String() string { return "" } |
| 52 | +func (gf genericFuncValue[T]) Name() string { |
| 53 | + name := reflect.TypeOf((*T)(nil)).Elem().String() |
| 54 | + if index := strings.LastIndexByte(name, '.'); index != -1 { |
| 55 | + name = name[index+1:] // Remove [QualifiedIdent] prefix. |
| 56 | + } |
| 57 | + return strings.ToLower(name) |
| 58 | +} |
| 59 | + |
42 | 60 | const (
|
43 | 61 | permMaximum = 0o7777
|
44 | 62 | permReadAll = 0o444
|
@@ -515,11 +533,15 @@ func flagSetFunc[
|
515 | 533 | })
|
516 | 534 | return
|
517 | 535 | }
|
518 |
| - flagSet.Func(name, usage, func(parameter string) error { |
| 536 | + funcFlag[VT](flagSet, name, usage, func(parameter string) error { |
519 | 537 | return parseAndSet(parameter, options, setter)
|
520 | 538 | })
|
521 | 539 | }
|
522 | 540 |
|
| 541 | +func funcFlag[T any](flagSet *flag.FlagSet, name, usage string, fn func(string) error) { |
| 542 | + flagSet.Var(genericFuncValue[T](fn), name, usage) |
| 543 | +} |
| 544 | + |
523 | 545 | func parseAndSet[
|
524 | 546 | OSR optionsReference[OS, OT, ST],
|
525 | 547 | OS optionSlice[OT, ST],
|
|
0 commit comments