-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplified flag value access #1367
Simplified flag value access #1367
Conversation
@toaster What do you think of claiming |
I like the idea and added a respective commit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it! I'll wait a bit before merging to make sure no other @urfave/cli humans have issues with the increased API surface area 👍🏼
@dearchap when you get time, I'd appreciate your thoughts on this 🙇🏼 |
@meatballhat I have no problem with the new interface. This API is not within most people's workflow wherein they get the flag value from context but if users wants to use flag.GetFromContext thats fine with me. |
Is there a minimal example on how to use it? On a minimal example from the docs, how could the new approach be used to access the value of the
|
@xoxys You will need a reference to the flag in order to use this type of access. For example, to take your code and put it into an example app to show the typical way of using package main
import (
"log"
"os"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "lang",
Aliases: []string{"l"},
Value: "english",
Usage: "language for the greeting",
EnvVars: []string{"LEGACY_COMPAT_LANG", "APP_LANG", "LANG"},
},
},
Action: func(cCtx *cli.Context) error {
notLang := cCtx.String("1ang")
log.Println("is this lang?:", notLang)
lang := cCtx.String("lang")
log.Println("is this lang?:", lang)
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
} and then modify it to use the access provided via the flag's package main
import (
"log"
"os"
"github.com/urfave/cli/v2"
)
func main() {
langFlag := &cli.StringFlag{
Name: "lang",
Aliases: []string{"l"},
Value: "english",
Usage: "language for the greeting",
EnvVars: []string{"LEGACY_COMPAT_LANG", "APP_LANG", "LANG"},
}
app := &cli.App{
Flags: []cli.Flag{langFlag},
Action: func(cCtx *cli.Context) error {
notLang := langFlag.Get(cCtx)
log.Println("is this lang?:", notLang)
lang := langFlag.Get(cCtx)
log.Println("is this lang?:", lang)
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
} Here's the diff, if helpful: --- a/1367.go
+++ b/1367.go
@@ -8,21 +8,21 @@
)
func main() {
+ langFlag := &cli.StringFlag{
+ Name: "lang",
+ Aliases: []string{"l"},
+ Value: "english",
+ Usage: "language for the greeting",
+ EnvVars: []string{"LEGACY_COMPAT_LANG", "APP_LANG", "LANG"},
+ }
+
app := &cli.App{
- Flags: []cli.Flag{
- &cli.StringFlag{
- Name: "lang",
- Aliases: []string{"l"},
- Value: "english",
- Usage: "language for the greeting",
- EnvVars: []string{"LEGACY_COMPAT_LANG", "APP_LANG", "LANG"},
- },
- },
+ Flags: []cli.Flag{langFlag},
Action: func(cCtx *cli.Context) error {
- notLang := cCtx.String("1ang")
+ notLang := langFlag.Get(cCtx)
log.Println("is this lang?:", notLang)
- lang := cCtx.String("lang")
+ lang := langFlag.Get(cCtx)
log.Println("is this lang?:", lang)
return nil I hope this is helpful 😁 |
That helps, thanks a lot! |
What type of PR is this?
What this PR does / why we need it:
This PR implements convenience accessors to access flag values in a context.
Instead of writing
ctx.Int(f.Name)
orctx.Value(f.Name).(int)
one can writef.Get(ctx)
.Thus, you don’t have to use the correct type method or the appropriate type assertion. The flag picks the correct name and gives you the correct type.
Which issue(s) this PR fixes:
Fixes #1316
Special notes for your reviewer:
I’m happy to give the accessor a shorter name if one comes up with a good idea :).(done! ~ @meatballhat)Release Notes