-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Flag Action should be called when value is set by alternative sources. #2041
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
Comments
@project0 Can you set Local: true for cli.StringFlag and try ? |
@dearchap indeed, setting Local true populates the value. Is this the intended behavior? If so, documenting it would be beneficial in this case. |
The problem is that by default we arent running FlagAction for "non local" flags. Flags which are "non local" are available to all subcommands as well and there was a discussion as to how flag actions should be run for these kinds of flags. Local means that the flag is local only to current command and will have its flag actions run. Let me think how to proceed with this. cc @urfave/cli |
In this case suppose your flag is "non local" you could invoke it something like this
we can run action for all flags(including log level ) for subcmd but say you invoke it like this
the log-level flag is set from environment when should the flag action be run ? or for example
It starts getting complicated. |
I've run into this same situation. This seems to be an undocumented breaking change in behaviour between
I've also tried adding Click to expand codepackage main
import (
"context"
"fmt"
"log"
"os"
"github.com/urfave/cli/v3"
)
func main() {
app := &cli.Command{
Name: "my app",
Commands: []*cli.Command{
{
Name: "serve",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "custom-var",
Sources: cli.EnvVars("CUSTOM_VAR"),
Local: true,
Required: false,
Action: func(_ context.Context, _ *cli.Command, s string) error {
fmt.Println("CUSTOM_VAR Action")
return nil
},
},
},
Action: func(_ context.Context, cmd *cli.Command) error {
fmt.Println("Serve")
return nil
},
},
},
}
if err := app.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
} Output: go run . serve
Serve I have the following questions:
Thanks! |
Checklist
v3.0.0-beta1
What problem does this solve?
Fix action beeing called for all circumstances when a value is set by either a flag or alternative source. Currently the flag action is only executed when given as argument.
example:
Solution description
When a env var is set, the action should be called.
Describe alternatives you've considered
not using in this case as it is has no consistent experience for users.
The text was updated successfully, but these errors were encountered: