Skip to content

Commit

Permalink
refactor: FlagValues int, bool, string, string-slice can return err
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluisq committed Mar 27, 2021
1 parent ddb3bc5 commit 78d7897
Show file tree
Hide file tree
Showing 3 changed files with 549 additions and 73 deletions.
48 changes: 35 additions & 13 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,52 @@ func main() {
},
Handler: func(ctx *cli.CmdContext) error {
fmt.Printf("Cmd `%s` executed!\n", ctx.Cmd.Name)
fmt.Printf("App Flag `file`: `%s`\n", ctx.AppContext.Flags.String("file").Value())
b, _ := ctx.AppContext.Flags.Bool("verbose").Value()
fmt.Printf("App Flag `verbose`: `%v`\n", b)

i, err := ctx.Flags.Int("trace").Value()
if err != nil {
fmt.Println(err)
os.Exit(1)
if file, err := ctx.AppContext.Flags.String("file"); err != nil {
fmt.Printf("App Flag `file`: `%s`\n", file.Value())
}

if verbose, err := ctx.AppContext.Flags.Bool("verbose"); err != nil {
b, _ := verbose.Value()
fmt.Printf("App Flag `verbose`: `%v`\n", b)
}

if trace, err := ctx.AppContext.Flags.Int("trace"); err != nil {
i, err := trace.Value()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Cmd Flag `trace`: `%d` (%T)\n", i, i)
}

if detailed, err := ctx.AppContext.Flags.Bool("detailed"); err != nil {
d, _ := detailed.Value()
fmt.Printf("Cmd Flag `detailed`: `%v` (%T)\n", d, d)
}
fmt.Printf("Cmd Flag `trace`: `%d` (%T)\n", i, i)

d, _ := ctx.Flags.Bool("detailed").Value()
fmt.Printf("Cmd Flag `detailed`: `%v` (%T)\n", d, d)
fmt.Printf("Cmd Tail arguments: %#v\n", ctx.TailArgs)
return nil
},
},
}
app.Handler = func(ctx *cli.AppContext) error {
fmt.Printf("App `%s` executed!\n", ctx.App.Name)
fmt.Printf("App Flag `file`: `%s`\n", ctx.Flags.String("file").Value())

if file, err := ctx.Flags.Bool("file"); err != nil {
if b, err := file.Value(); err != nil {
fmt.Printf("App Flag `file`: `%t`\n", b)
}
}

fmt.Printf("App Flag `int`: `%v`\n", ctx.Flags.Any("int"))
b, _ := ctx.Flags.Bool("verbose").Value()
fmt.Printf("App Flag `verbose`: `%v`\n", b)

if verbose, err := ctx.Flags.Bool("verbose"); err != nil {
if b, err := verbose.Value(); err != nil {
fmt.Printf("App Flag `verbose`: `%v`\n", b)
}
}

fmt.Printf("App Tail arguments: %#v\n", ctx.TailArgs)
fmt.Printf("App Provided flags: %v\n", ctx.Flags.GetProvided())
fmt.Printf("App Provided flags (long): %v\n", ctx.Flags.GetProvidedLong())
Expand Down
67 changes: 35 additions & 32 deletions flag_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,70 +287,73 @@ func (v *FlagValues) Any(longFlagName string) AnyValue {
}

// Bool gets a `bool` flag value which value type should match
// with its flag definition type, otherwise it just panics.
func (v *FlagValues) Bool(longFlagName string) *FlagBoolValue {
// with its flag definition type, otherwise it returns an error.
func (v *FlagValues) Bool(longFlagName string) (val *FlagBoolValue, err error) {
switch f := v.findByKey(longFlagName).(type) {
case FlagBool:
return &FlagBoolValue{flag: f}
val = &FlagBoolValue{flag: f}
return
default:
t := strings.ReplaceAll(fmt.Sprintf("%T", f), "cline.", "")
panic(
fmt.Sprintf(
"error: flag `--%s` value used as `FlagBoolValue` but declared as `%s`.\n",
longFlagName,
t),
err = fmt.Errorf(
"flag `--%s` value used as `FlagBoolValue` but declared as `%s`",
longFlagName,
t,
)
return
}
}

// Int finds a `int` flag value which value type should match
// with its flag definition type, otherwise it just panics.
func (v *FlagValues) Int(longFlagName string) *FlagIntValue {
// with its flag definition type, otherwise it returns an error.
func (v *FlagValues) Int(longFlagName string) (val *FlagIntValue, err error) {
switch f := v.findByKey(longFlagName).(type) {
case FlagInt:
return &FlagIntValue{flag: f}
val = &FlagIntValue{flag: f}
return
default:
t := strings.ReplaceAll(fmt.Sprintf("%T", f), "cline.", "")
panic(
fmt.Sprintf(
"error: flag `--%s` value used as `FlagIntValue` but declared as `%s`.\n",
longFlagName,
t,
),
err = fmt.Errorf(
"flag `--%s` value used as `FlagIntValue` but declared as `%s`",
longFlagName,
t,
)
return
}
}

// String finds a `string` flag value which value type should match
// with its flag definition type, otherwise it just panics.
func (v *FlagValues) String(longFlagName string) *FlagStringValue {
// with its flag definition type, otherwise it returns an error.
func (v *FlagValues) String(longFlagName string) (val *FlagStringValue, err error) {
switch f := v.findByKey(longFlagName).(type) {
case FlagString:
return &FlagStringValue{flag: f}
val = &FlagStringValue{flag: f}
return
default:
t := strings.ReplaceAll(fmt.Sprintf("%T", f), "cline.", "")
panic(fmt.Sprintf(
"error: flag `--%s` value used as `FlagStringValue` but declared as `%s`.\n",
err = fmt.Errorf(
"flag `--%s` value used as `FlagStringValue` but declared as `%s`",
longFlagName,
t,
))
)
return
}
}

// StringSlice finds a string slice which value type should match
// with its flag definition type, otherwise it just panics.
func (v *FlagValues) StringSlice(longFlagName string) *FlagStringSliceValue {
// with its flag definition type, otherwise it returns an error.
func (v *FlagValues) StringSlice(longFlagName string) (val *FlagStringSliceValue, err error) {
switch f := v.findByKey(longFlagName).(type) {
case FlagStringSlice:
return &FlagStringSliceValue{flag: f}
val = &FlagStringSliceValue{flag: f}
return
default:
t := strings.ReplaceAll(fmt.Sprintf("%T", f), "cline.", "")
panic(
fmt.Sprintf(
"error: flag `--%s` value used as `FlagStringSliceValue` but declared as `%s`.\n",
longFlagName,
t,
),
err = fmt.Errorf(
"flag `--%s` value used as `FlagStringSliceValue` but declared as `%s`",
longFlagName,
t,
)
return
}
}
Loading

0 comments on commit 78d7897

Please sign in to comment.