Skip to content

Commit

Permalink
Better type support for positional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
leaanthony committed Jan 23, 2023
1 parent 5bcd656 commit b80a127
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,8 @@ func TestCli_PositionalArgs(t *testing.T) {

e := c.Run("create", "bob", "true")

if e == nil {
t.Errorf("expected error, got nil")
if e != nil {
t.Errorf("unexpected error")
}
}

Expand Down
21 changes: 6 additions & 15 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,33 +476,24 @@ func (c *Command) parsePositionalArgs(args []string) error {
}
fieldType := field.Type()
switch fieldType.Kind() {
case reflect.Bool:
// set value of field to true
field.SetBool(true)
case reflect.String:
field.SetString(posArg)
case reflect.Int:
value, err := strconv.Atoi(posArg)
if err != nil {
return err
}
field.SetInt(int64(value))
case reflect.Int64:
case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
value, err := strconv.ParseInt(posArg, 10, 64)
if err != nil {
return err
}
field.SetInt(value)
case reflect.Uint:
case reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint:
value, err := strconv.ParseUint(posArg, 10, 64)
if err != nil {
return err
}
field.SetUint(value)
case reflect.Uint64:
value, err := strconv.ParseUint(posArg, 10, 64)
if err != nil {
return err
}
field.SetUint(value)
case reflect.Float64:
case reflect.Float64, reflect.Float32:
value, err := strconv.ParseFloat(posArg, 64)
if err != nil {
return err
Expand Down

0 comments on commit b80a127

Please sign in to comment.