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
|
Help string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Flag struct {
|
// type constraint
|
||||||
Long string
|
type Val interface {
|
||||||
Short string
|
string | bool
|
||||||
Val string
|
|
||||||
Help string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// there's got to be a way to generalize the val field to string|bool
|
// with generic Val field, instantiate like Flag[string]
|
||||||
type FlagBool struct {
|
type Flag[V Val] struct {
|
||||||
Long string
|
Long string
|
||||||
Short string
|
Short string
|
||||||
Val bool
|
Val V
|
||||||
Help string
|
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 {
|
type options struct {
|
||||||
query flags.Arg
|
query flags.Arg
|
||||||
date flags.Flag
|
date flags.Flag[string]
|
||||||
invert flags.FlagBool
|
invert flags.Flag[bool]
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitOpts() options {
|
func InitOpts() options {
|
||||||
return options{
|
return options{
|
||||||
flags.Arg{"", "tag query"},
|
flags.Arg{"", "tag query"},
|
||||||
flags.Flag{"--date", "-d", "", "date range"},
|
flags.Flag[string]{"--date", "-d", "", "date range"},
|
||||||
flags.FlagBool{"--invert", "-i", false, "invert match"},
|
flags.Flag[bool]{"--invert", "-i", false, "invert match"},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -25,14 +25,14 @@ func ParseArgs(args []string) options {
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
if flags.HasDashPrefix(args[0]) {
|
if !flags.HasDashPrefix(args[0]) {
|
||||||
opts.query.Val = args[0]
|
opts.query.Val = args[0]
|
||||||
}
|
}
|
||||||
for i, arg := range args {
|
for i, arg := range args {
|
||||||
switch arg {
|
switch arg {
|
||||||
case opts.invert.Long, opts.invert.Short:
|
case opts.invert.Long, opts.invert.Short:
|
||||||
opts.invert.Val = true
|
opts.invert.Val = true
|
||||||
case opts.date.Long:
|
case opts.date.Long, opts.date.Short:
|
||||||
if flags.MissingValueArg(args, i) {
|
if flags.MissingValueArg(args, i) {
|
||||||
flags.HelpMissingVal(arg)
|
flags.HelpMissingVal(arg)
|
||||||
break
|
break
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue