flags : generic string|bool value field
This commit is contained in:
parent
f23a98fb5d
commit
d89b48adc3
3 changed files with 12 additions and 14 deletions
|
|
@ -10,18 +10,16 @@ type Arg struct {
|
|||
Help string
|
||||
}
|
||||
|
||||
type Flag struct {
|
||||
Long string
|
||||
Short string
|
||||
Val string
|
||||
Help string
|
||||
// type constraint
|
||||
type Val interface {
|
||||
string | bool
|
||||
}
|
||||
|
||||
// there's got to be a way to generalize the val field to string|bool
|
||||
type FlagBool struct {
|
||||
// with generic Val field, instantiate like Flag[string]
|
||||
type Flag[V Val] struct {
|
||||
Long string
|
||||
Short string
|
||||
Val bool
|
||||
Val V
|
||||
Help string
|
||||
}
|
||||
|
||||
|
|
|
|||
BIN
go
Executable file
BIN
go
Executable file
Binary file not shown.
12
tag/tag.go
12
tag/tag.go
|
|
@ -8,15 +8,15 @@ import (
|
|||
|
||||
type options struct {
|
||||
query flags.Arg
|
||||
date flags.Flag
|
||||
invert flags.FlagBool
|
||||
date flags.Flag[string]
|
||||
invert flags.Flag[bool]
|
||||
}
|
||||
|
||||
func InitOpts() options {
|
||||
return options{
|
||||
flags.Arg{"", "tag query"},
|
||||
flags.Flag{"--date", "-d", "", "date range"},
|
||||
flags.FlagBool{"--invert", "-i", false, "invert match"},
|
||||
flags.Flag[string]{"--date", "-d", "", "date range"},
|
||||
flags.Flag[bool]{"--invert", "-i", false, "invert match"},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -25,14 +25,14 @@ func ParseArgs(args []string) options {
|
|||
if len(args) == 0 {
|
||||
return opts
|
||||
}
|
||||
if flags.HasDashPrefix(args[0]) {
|
||||
if !flags.HasDashPrefix(args[0]) {
|
||||
opts.query.Val = args[0]
|
||||
}
|
||||
for i, arg := range args {
|
||||
switch arg {
|
||||
case opts.invert.Long, opts.invert.Short:
|
||||
opts.invert.Val = true
|
||||
case opts.date.Long:
|
||||
case opts.date.Long, opts.date.Short:
|
||||
if flags.MissingValueArg(args, i) {
|
||||
flags.HelpMissingVal(arg)
|
||||
break
|
||||
|
|
|
|||
Loading…
Reference in a new issue