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

Flag Action should be called when value is set by alternative sources. #2041

Open
3 tasks done
project0 opened this issue Jan 6, 2025 · 4 comments
Open
3 tasks done
Labels
area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this

Comments

@project0
Copy link

project0 commented Jan 6, 2025

Checklist

  • Are you running the latest v3 release? The list of releases is here.
    v3.0.0-beta1
  • Did you check the manual for your release? The v3 manual is here.
  • Did you perform a search about this feature? Here's the GitHub guide about searching.

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:

		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:    flag_log_level,
				Usage:   "The log level (trace, debug, info, warn, error, fatal, panic)",
				Value:   "info",
				Sources: cli.EnvVars(flagEnv(flag_log_level)),
				Action: func(ctx context.Context, cmd *cli.Command, value string) error {
					log.Info("Setting log level to ", value)
					level, err := log.ParseLevel(value)
					if err != nil {
						return err
					}
					log.SetLevel(level)
					return nil
				},
			},

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.

@project0 project0 added area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this labels Jan 6, 2025
@project0 project0 changed the title Flag Action should be called when value is by alternative sources. Flag Action should be called when value is set by alternative sources. Jan 6, 2025
@dearchap
Copy link
Contributor

@project0 Can you set Local: true for cli.StringFlag and try ?

@project0
Copy link
Author

@dearchap indeed, setting Local true populates the value. Is this the intended behavior? If so, documenting it would be beneficial in this case.

@dearchap
Copy link
Contributor

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

@dearchap
Copy link
Contributor

dearchap commented Jan 12, 2025

In this case suppose your flag is "non local" you could invoke it something like this

root cmd subcmd --log-level debug

we can run action for all flags(including log level ) for subcmd

but say you invoke it like this

LOG_LEVEL=debug root cmd subcmd

the log-level flag is set from environment when should the flag action be run ? or for example

LOG_LEVEL=debug root cmd --log-level info subcmd

It starts getting complicated.

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
Projects
None yet
Development

No branches or pull requests

2 participants