cli icon indicating copy to clipboard operation
cli copied to clipboard

Support `WithDefault` method

Open ocavue opened this issue 6 years ago • 1 comments

This issue is a feature request

Both Arg and Option don't support default value now. In order to do that, I can write something like below:

installCommand := cli.NewCommand("install", "Install a package").
	WithOption(
		cli.NewOption("retry", "maximal retry times (default: 3)").WithType(cli.TypeInt),
	).
	WithArg(
		cli.NewArg("package", "package name").WithType(cli.TypeString),
	).
	WithAction(func(args []string, options map[string]string) int {
		if _, ok := options["retry"]; !ok {
			options["retry"] = "3"
		}
		// do something
	})

With WithDefault method, I can write the default value 3 for only one time and avert a conflict between the actual code and the doc.

ocavue avatar Apr 09 '19 02:04 ocavue

Should be easy to implement. Want to submit a pull request?

osklyar avatar Apr 09 '19 22:04 osklyar