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

Fix: Make flags persistent by default #1977

Merged
merged 3 commits into from
Oct 11, 2024

Conversation

dearchap
Copy link
Contributor

@dearchap dearchap commented Oct 6, 2024

What type of PR is this?

(REQUIRED)

  • feature

What this PR does / why we need it:

(REQUIRED)

Which issue(s) this PR fixes:

(REQUIRED)

Fixes #976

Special notes for your reviewer:

(fill-in or delete this section)

Testing

(fill-in or delete this section)

Updated existing tests to work with new features

Release Notes

(REQUIRED)

All flags defined by a command flow to subcommands by default unless the Local field is set for the flag in which case it will apply to that command only. This is better than having to make flags persistent when needed as this is an oft requested feature

@fiatjaf
Copy link

fiatjaf commented Oct 6, 2024

It doesn't work.

I'm on af97b3f. I created a directory example/ and put the following main.go file in it:

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/urfave/cli/v3"
)

var app = &cli.Command{
	Name:                   "example",
	Suggest:                true,
	UseShortOptionHandling: true,
	Commands: []*cli.Command{
		blergh,
	},
}

var blergh = &cli.Command{
	Name: "blergh",
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name: "cool",
		},
		&cli.UintFlag{
			Name: "number",
		},
	},
	Action: func(ctx context.Context, c *cli.Command) error {
		fmt.Println("args:", c.Args())
		fmt.Println("cool?", c.Bool("cool"))
		fmt.Println("number", c.Uint("number"))

		return nil
	},
}

func main() {
	if err := app.Run(context.Background(), os.Args); err != nil {
		os.Exit(1)
	}
}

Then I run it with go run . blergh ok --cool --number 12, expecing the output to be

args: &{[ok]}
cool? true
number 12

But instead I get

args: &{[ok --cool --number 12]}
cool? false
number 0

@abitrolly
Copy link
Contributor

abitrolly commented Oct 6, 2024

I feel like this needs a proper blog post explaining the logic, without Go code.

@dearchap
Copy link
Contributor Author

dearchap commented Oct 6, 2024

Okay I see the problem. I misinterpreted what you are looking for @fiatjaf . I'm going to reopen your PR and make a fix .

@dearchap dearchap merged commit a288be2 into urfave:main Oct 11, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

v2 feature: allow flags after arguments
3 participants