Skip to content

Commit

Permalink
Support Int64, Uint, Uint64, Float64
Browse files Browse the repository at this point in the history
  • Loading branch information
leaanthony committed Dec 19, 2022
1 parent c9b2112 commit faabf69
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
28 changes: 26 additions & 2 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,13 @@ func TestCli_CommandOtherCLIArgs(t *testing.T) {
}

type Person struct {
Name string `description:"The name of the person"`
Name string `description:"The name of the person"`
Age int `description:"The age of the person"`
SSN uint `description:"The SSN of the person"`
Age64 int64 `description:"The age of the person"`
SSN64 uint64 `description:"The SSN of the person"`
BankBalance float64 `description:"The bank balance of the person"`
Married bool `description:"Whether the person is married"`
}

func TestCli_CommandAddFunction(t *testing.T) {
Expand All @@ -257,12 +263,30 @@ func TestCli_CommandAddFunction(t *testing.T) {
if person.Name != "Bob" {
t.Errorf("expected name flag to be 'Bob', got %v", person.Name)
}
if person.Age != 30 {
t.Errorf("expected age flag to be 30, got %v", person.Age)
}
if person.SSN != 123456789 {
t.Errorf("expected ssn flag to be 123456789, got %v", person.SSN)
}
if person.Age64 != 30 {
t.Errorf("expected age64 flag to be 30, got %v", person.Age64)
}
if person.SSN64 != 123456789 {
t.Errorf("expected ssn64 flag to be 123456789, got %v", person.SSN64)
}
if person.BankBalance != 123.45 {
t.Errorf("expected bankbalance flag to be 123.45, got %v", person.BankBalance)
}
if person.Married != true {
t.Errorf("expected married flag to be true, got %v", person.Married)
}
return nil
}

c.NewSubCommandFunction("create", "create a person", createPerson)

e := c.Run("create", "-name", "Bob")
e := c.Run("create", "-name", "Bob", "-age", "30", "-ssn", "123456789", "-age64", "30", "-ssn64", "123456789", "-bankbalance", "123.45", "-married")
if e != nil {
t.Errorf("expected no error, got %v", e)
}
Expand Down
52 changes: 45 additions & 7 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,6 @@ func (c *Command) NewSubCommandInheritFlags(name, description string) *Command {
return result
}

// BoolFlag - Adds a boolean flag to the command
func (c *Command) BoolFlag(name, description string, variable *bool) *Command {
c.flags.BoolVar(variable, name, *variable, description)
c.flagCount++
return c
}

func (c *Command) AddFlags(optionStruct interface{}) *Command {
// use reflection to determine if this is a pointer to a struct
// if not, panic
Expand Down Expand Up @@ -264,12 +257,29 @@ func (c *Command) AddFlags(optionStruct interface{}) *Command {
c.StringFlag(name, description, field.Addr().Interface().(*string))
case reflect.Int:
c.IntFlag(name, description, field.Addr().Interface().(*int))
case reflect.Int64:
c.Int64Flag(name, description, field.Addr().Interface().(*int64))
case reflect.Uint:
c.UintFlag(name, description, field.Addr().Interface().(*uint))
case reflect.Uint64:
c.UInt64Flag(name, description, field.Addr().Interface().(*uint64))
case reflect.Float64:
c.Float64Flag(name, description, field.Addr().Interface().(*float64))
default:
println("WARNING: Unsupported type for flag: ", fieldType.Type.Kind())
}
}

return c
}

// BoolFlag - Adds a boolean flag to the command
func (c *Command) BoolFlag(name, description string, variable *bool) *Command {
c.flags.BoolVar(variable, name, *variable, description)
c.flagCount++
return c
}

// StringFlag - Adds a string flag to the command
func (c *Command) StringFlag(name, description string, variable *string) *Command {
c.flags.StringVar(variable, name, *variable, description)
Expand All @@ -284,6 +294,34 @@ func (c *Command) IntFlag(name, description string, variable *int) *Command {
return c
}

// Int64Flag - Adds an int flag to the command
func (c *Command) Int64Flag(name, description string, variable *int64) *Command {
c.flags.Int64Var(variable, name, *variable, description)
c.flagCount++
return c
}

// UintFlag - Adds an int flag to the command
func (c *Command) UintFlag(name, description string, variable *uint) *Command {
c.flags.UintVar(variable, name, *variable, description)
c.flagCount++
return c
}

// UInt64Flag - Adds an int flag to the command
func (c *Command) UInt64Flag(name, description string, variable *uint64) *Command {
c.flags.Uint64Var(variable, name, *variable, description)
c.flagCount++
return c
}

// Float64Flag - Adds a float64 flag to the command
func (c *Command) Float64Flag(name, description string, variable *float64) *Command {
c.flags.Float64Var(variable, name, *variable, description)
c.flagCount++
return c
}

// LongDescription - Sets the long description for the command
func (c *Command) LongDescription(longdescription string) *Command {
c.longdescription = longdescription
Expand Down

0 comments on commit faabf69

Please sign in to comment.