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

Enable option with empty argument #5

Merged
merged 1 commit into from
Feb 28, 2024
Merged

Conversation

plar
Copy link
Collaborator

@plar plar commented Feb 27, 2024

This PR will allow for passing an empty argument to the option and will delegate the logic for managing the empty argument to the user side. See the full discussion here.

Now, we can call command.NewOptionWithArgument("--opt", ""), and it will generate --opt=, which is a legitimate case. The previous implementation skipped generating --opt if argument was an empty string ("").

This logic has now been transferred to the user side for handling empty arguments.

// OptWithArg("") => --opt=
// OptWithArg("custom") => --opt=custom
func OptWithArg(arg string) command.Applier {
	return command.NewOptionWithArgument("--opt", arg)
}

// OptWithOptionalArgAndDefault("") => --opt=default
// OptWithOptionalArgAndDefault("custom") => --opt=custom
func OptWithOptionalArgAndDefault(arg string) command.Applier {
	if arg == "" {
		arg = "default"
	}
	return command.NewOptionWithArgument("--opt", arg)
}

// OptWithOptionalArgAndNoop("") => nothing will be generated
// OptWithOptionalArgAndNoop("custom") => --opt=custom
func OptWithOptionalArgAndNoop(arg string) command.Applier {
	if arg == "" {
		return command.NewNoopArgument()
	}
	return command.NewOptionWithArgument("--opt", arg)
}

// OptWithRequiredArgAndError("") => error
// OptWithRequiredArgAndError("custom") => --opt=custom
func OptWithRequiredArgAndError(arg string) command.Applier {
	if arg == "" {
		return command.NewErrorArgument(fmt.Errorf("arg is required"))
	}
	return command.NewOptionWithArgument("--opt", arg)
}

Signed-off-by: plar <pavel.larkin@veeam.com>
@plar plar self-assigned this Feb 27, 2024
@plar plar merged commit 67a841c into main Feb 28, 2024
4 checks passed
@plar plar deleted the enable-opt-with-empty-arg branch February 28, 2024 00:22
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.

None yet

2 participants