Skip to content
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

PersistentFlags support #1820

Closed
chirino opened this issue Nov 20, 2023 · 6 comments
Closed

PersistentFlags support #1820

chirino opened this issue Nov 20, 2023 · 6 comments
Labels
area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this status/waiting-for-response Waiting for response from original requester

Comments

@chirino
Copy link

chirino commented Nov 20, 2023

Something similar to cobra's PersistentFlag would be a nice addition to save configuration to files.

A persistent flag is a flag defined in a parent command, that can be use in any of the sub commands without having to repeat the flag in the sub command. These a really useful for common flags like --verbose that all sub commands will implement.

See: https://stackoverflow.com/questions/63495992/what-is-the-difference-between-go-cobra-persistentflags-and-flags for other explanations.

@chirino chirino added area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this labels Nov 20, 2023
@chirino
Copy link
Author

chirino commented Nov 20, 2023

This is basically reopening #692 since that was closed due to folks not understanding what persistent flags are. They are NOT about "saving a flag that was set in the previous invocation of the cli".

@chirino
Copy link
Author

chirino commented Nov 20, 2023

Some might think you can just use common flags on the root command and sub commands to get this to work, but sadly it does not. The following test currently fails:

package test

import (
	"github.com/urfave/cli/v2"
	"testing"
)

func TestCommonFlags(t *testing.T) {

	commonFlags := []cli.Flag{
		&cli.StringFlag{
			Name: "result",
		},
	}

	result := ""
	app := &cli.App{
		Name:  "root",
		Flags: commonFlags,
		Commands: []*cli.Command{
			{
				Name:  "sub",
				Flags: commonFlags,
				Action: func(ctx *cli.Context) error {
					result = ctx.String("result")
					return nil
				},
			},
		},
	}

	if err := app.Run([]string{"root", "sub", "--result", "after"}); err != nil {
		t.Fatal(err)
	}
	if result != "after" {
		t.Fatalf("result is not after")
	}

	if err := app.Run([]string{"root", "--result", "before", "sub"}); err != nil {
		t.Fatal(err)
	}
	if result != "before" {
		t.Fatalf("result is not before")
	}

}

@dearchap
Copy link
Contributor

dearchap commented Nov 20, 2023

@chirino Support for persistent flags was added in v3. Please try with that. You can mark a flag explicitly as persistent in which case it will be allowed in subcommands as well.

@dearchap dearchap added the status/waiting-for-response Waiting for response from original requester label Nov 24, 2023
@dearchap
Copy link
Contributor

@chirino I am closing this for now. If you see any problems with persistent flags in v3 we can create a new issue

@chirino
Copy link
Author

chirino commented Nov 29, 2023

Thanks! I Tested v3 out and it's working nice.

@chirino
Copy link
Author

chirino commented Nov 29, 2023

Well mostly working.. found this bug: #1826

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this status/waiting-for-response Waiting for response from original requester
Projects
None yet
Development

No branches or pull requests

2 participants