cli icon indicating copy to clipboard operation
cli copied to clipboard

Support nullable cli.Flag

Open Einsfier opened this issue 5 years ago • 6 comments

Checklist

  • [x] Are you running the latest v2 release? The list of releases is here.
  • [x] Did you check the manual for your release? The v2 manual is here
  • [x] Did you perform a search about this feature? Here's the Github guide about searching.

What problem does this solve?

This will allow users only specifying a flag without any value.

For example, I defined a StringFlag

&cli.StringFlag{
	Name:    "write",
	Aliases: []string{"w"},
	Usage:   "Write a template config file to given place",
	Nullable: true,
	// This opt makes the "myCLI_App subCmd -w" legal. Now it pops "ERR flag needs an argument: -w"
	Value: "template.json"
	// If Nullable is true, and user didn't specify any value. Then this will be the default value.
	// But one thing is different. the HasBeenSet field for this flag should be true.
}

Solution description

This may looks tricky. But it do helps on certain circumstance.

For example. I have a command with two flags: "--config" and "--write". "config" is used to read config file. "write" is used to generate a config template.

For convenience, the behavior of my application is controlled by flag, not command.

I want to have a default place for "write", users only need to provide flag "--write", then I know they want a template in default location. Meanwhile, I also want this flag configurable. So that user can provide a destination for template file.

For now. it's impossible to only specify a flag without value.

Describe alternatives you've considered

Any suggestion or my design is wrong ?

Einsfier avatar Sep 14 '20 09:09 Einsfier

I think the problem is that you may be overloading the flags with too much functionality. Here you are changing behaviour (read vs write) and providing an optional parameter.

Assuming you have some flexibility when designing your app, I see several ways to solve this.

One way is to set the --write flag as a BoolFlag, and have a second StringFlag for the destination, such as --file or --path. If a second flag is not an option, you could also get the destination from ctx.App.Args.

Personally I would prefer to separate the two behaviours into two subcommands, each with their own set of flags, or you would probably end up with too much if-else to deal with all the different combinations of flag, especially as the app grows and gains functionality.

stellirin avatar Oct 04 '20 00:10 stellirin

anyone working on this?

6543 avatar Oct 11 '20 22:10 6543

This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.

stale[bot] avatar Jan 10 '21 03:01 stale[bot]

ping

6543 avatar Jan 10 '21 05:01 6543

This issue or PR has been bumped and is no longer marked as stale! Feel free to bump it again in the future, if it's still relevant.

stale[bot] avatar Jan 10 '21 05:01 stale[bot]

This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.

stale[bot] avatar Apr 18 '21 22:04 stale[bot]

Duplicate of #424

dearchap avatar Oct 23 '22 23:10 dearchap