commando icon indicating copy to clipboard operation
commando copied to clipboard

Add optional argument with empty default value

Open ruudk opened this issue 5 years ago • 4 comments

First of all: this library looks amazing! Great work!

Currently, there is no way to register an optional argument, unless you add an argument with a default value that is not an empty string.

So for example:

// $ reactor create <name> [version]
commando.
	Register("create").
	SetShortDescription("creates a component").
	AddArgument("name", "name of the component to create", "").                                  // required
	AddArgument("version", "version of the component", "1.0.0").     

Version is optional here because it has a default value that is not an empty string.

For my use case, I want to add an optional value that itself is an empty string by default.

That doesn't work because of this rule:

IsRequired: defaultValue == "" && !clpArg.IsVariadic, // variadic arguments are always optional

I would like to have something like this:

// $ reactor create <name> [version]
commando.
	Register("create").
	SetShortDescription("creates a component").
	AddArgument("name", "name of the component to create", "").                                  // required
	AddOptionalArgument("version", "optional version to create", "").     

Any idea how to solve this?

ruudk avatar Nov 04 '20 17:11 ruudk

I think it can also be solved by using the same trick as AddFlag. There, it uses defaultValue interface{} which allows to pass in nil. Then nil can be for something that has no default value, and a string value will be the default value.

ruudk avatar Nov 04 '20 18:11 ruudk

Hmm, turns out that it's also impossible to add a string flag with an empty default value.

AddFlag("default", "desc", commando.String, nil).
# Error: value of the --default flag can not be empty.

AddFlag("default", "desc", commando.String, "").
# Error: value of the --default flag can not be empty.

I think this needs some more thought :)

ruudk avatar Nov 04 '20 19:11 ruudk

@thatisuday Any idea how to solve this or improve this?

ruudk avatar Dec 14 '20 18:12 ruudk

Same issue, any solutions for this? It's bug???

tranphuquy19 avatar Apr 07 '21 12:04 tranphuquy19