We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Is set should work even when the persistent flag is used with it:
package main_test import ( "context" "github.com/urfave/cli/v3" "testing" ) func TestCommonFlags(t *testing.T) { result := "" resultIsSet := false app := &cli.Command{ Name: "root", Flags: []cli.Flag{ &cli.StringFlag{ Name: "result", Persistent: true, }, }, Commands: []*cli.Command{ { Name: "sub", Action: func(ctx *cli.Context) error { result = ctx.String("result") resultIsSet = ctx.IsSet("result") return nil }, }, }, } if err := app.Run(context.Background(), []string{"root", "sub", "--result", "after"}); err != nil { t.Fatal(err) } if result != "after" { t.Fatalf("result is not after") } if !resultIsSet { t.Fatalf("result is not set after") } if err := app.Run(context.Background(), []string{"root", "--result", "before", "sub"}); err != nil { t.Fatal(err) } if result != "before" { t.Fatalf("result is not before") } if !resultIsSet { t.Fatalf("result is not set before") } }
The above test should pass.
Avoid using the IsSet function.
The text was updated successfully, but these errors were encountered:
@chirino cli.Context was removed sometime in v3. Can you update to latest v3 ?
Sorry, something went wrong.
here ya go.. I think you can add this to your unit tests...
func TestPersistentFlagIsSet(t *testing.T) { result := "" resultIsSet := false app := &Command{ Name: "root", Flags: []Flag{ &StringFlag{ Name: "result", Persistent: true, }, }, Commands: []*Command{ { Name: "sub", Action: func(_ context.Context, cmd *Command) error { result = cmd.String("result") resultIsSet = cmd.IsSet("result") return nil }, }, }, } r := require.New(t) err := app.Run(context.Background(), []string{"root", "--result", "before", "sub"}) r.NoError(err) r.Equal("before", result) r.True(resultIsSet) err = app.Run(context.Background(), []string{"root", "sub", "--result", "after"}) r.NoError(err) r.Equal("after", result) r.True(resultIsSet) }
That was quick work @dearchap, thanks!
Successfully merging a pull request may close this issue.
Checklist
What problem does this solve?
Is set should work even when the persistent flag is used with it:
Solution description
The above test should pass.
Describe alternatives you've considered
Avoid using the IsSet function.
The text was updated successfully, but these errors were encountered: