Add more flag types and add flag set

This commit is contained in:
Evan Fiordeliso 2023-11-10 14:59:37 -05:00
parent 5519d58908
commit 8f98961c46
8 changed files with 219 additions and 7 deletions

View File

@ -13,7 +13,7 @@ type Command struct {
LongDescription string
aliases []string
arguments []*Argument
flags []flags.Flag
flags flags.Set
subcommands []*Command
parent *Command
run func(args []string)
@ -59,6 +59,11 @@ func (c *Command) Execute(args []string) {
args = args[1:]
}
if err := c.flags.Parse(args); err != nil {
c.ShowHelp()
return
}
if len(args) > 0 {
for _, subcommand := range c.subcommands {
if subcommand.Name == args[0] {

56
flags/bool.go Normal file
View File

@ -0,0 +1,56 @@
package flags
import "strconv"
type BoolFlag struct {
name string
shortName string
description string
value bool
}
var _ Flag = (*StringFlag)(nil)
func NewBoolFlag(name, shortName string, defaultValue bool, description string) *BoolFlag {
return &BoolFlag{
name: name,
shortName: shortName,
description: description,
value: defaultValue,
}
}
// Description implements Flag.
func (f *BoolFlag) Description() string {
return f.description
}
// Name implements Flag.
func (f *BoolFlag) Name() string {
return f.name
}
// ShortName implements Flag.
func (f *BoolFlag) ShortName() string {
return f.shortName
}
// Value implements Flag.
func (f *BoolFlag) Parse(raw string) error {
if raw == "" {
return nil
}
boolVal, err := strconv.ParseBool(raw)
if err != nil {
return err
}
f.value = boolVal
return nil
}
func (f *BoolFlag) Value() bool {
return f.value
}

View File

@ -4,5 +4,5 @@ type Flag interface {
Name() string
ShortName() string
Description() string
Parse(raw string) (any, error)
Parse(raw string) error
}

56
flags/float.go Normal file
View File

@ -0,0 +1,56 @@
package flags
import "strconv"
type FloatFlag struct {
name string
shortName string
description string
value float64
}
var _ Flag = (*FloatFlag)(nil)
func NewFloatFlag(name, shortName string, defaultValue float64, description string) *FloatFlag {
return &FloatFlag{
name: name,
shortName: shortName,
description: description,
value: defaultValue,
}
}
// Description implements Flag.
func (f *FloatFlag) Description() string {
return f.description
}
// Name implements Flag.
func (f *FloatFlag) Name() string {
return f.name
}
// ShortName implements Flag.
func (f *FloatFlag) ShortName() string {
return f.shortName
}
// Value implements Flag.
func (f *FloatFlag) Parse(raw string) error {
if raw == "" {
return nil
}
floatVal, err := strconv.ParseFloat(raw, 64)
if err != nil {
return err
}
f.value = floatVal
return nil
}
func (f *FloatFlag) Value() float64 {
return f.value
}

11
flags/global.go Normal file
View File

@ -0,0 +1,11 @@
package flags
var globalFlags Set
func Global(fs ...Flag) {
globalFlags = append(globalFlags, fs...)
}
func Globals() Set {
return globalFlags
}

56
flags/int.go Normal file
View File

@ -0,0 +1,56 @@
package flags
import "strconv"
type IntFlag struct {
name string
shortName string
description string
value int
}
var _ Flag = (*StringFlag)(nil)
func NewIntFlag(name, shortName string, defaultValue int, description string) *IntFlag {
return &IntFlag{
name: name,
shortName: shortName,
description: description,
value: defaultValue,
}
}
// Description implements Flag.
func (f *IntFlag) Description() string {
return f.description
}
// Name implements Flag.
func (f *IntFlag) Name() string {
return f.name
}
// ShortName implements Flag.
func (f *IntFlag) ShortName() string {
return f.shortName
}
// Value implements Flag.
func (f *IntFlag) Parse(raw string) error {
if raw == "" {
return nil
}
intVal, err := strconv.Atoi(raw)
if err != nil {
return err
}
f.value = intVal
return nil
}
func (f *IntFlag) Value() int {
return f.value
}

29
flags/set.go Normal file
View File

@ -0,0 +1,29 @@
package flags
type Set []Flag
func NewSet() Set {
return Set{}
}
func (s Set) Add(f Flag) Set {
return append(s, f)
}
func (s Set) Get(name string) (Flag, bool) {
for _, f := range s {
if f.Name() == name {
return f, true
}
if f.ShortName() == name {
return f, true
}
}
return nil, false
}
// TODO: Implement
func (s Set) Parse(args []string) error {
return nil
}

View File

@ -9,7 +9,7 @@ type StringFlag struct {
var _ Flag = (*StringFlag)(nil)
func NewStringFlag(name, shortName, description, defaultValue string) *StringFlag {
func NewStringFlag(name, shortName, defaultValue, description string) *StringFlag {
return &StringFlag{
name: name,
shortName: shortName,
@ -35,14 +35,13 @@ func (f *StringFlag) ShortName() string {
}
// Value implements Flag.
func (f *StringFlag) Parse(raw string) (any, error) {
func (f *StringFlag) Parse(raw string) error {
if raw == "" {
return f.value, nil
return nil
}
f.value = raw
return raw, nil
return nil
}
func (f *StringFlag) Value() string {