Tuesday 2 February 2021

Spring Shell: Optional Parameters and Default Values

Using 'defaultValue' attribute of @ShellOption annotation, you can provide default value to a parameter. If user do not specify any value to a parameter, then default value is taken in that place.

 

Example

@ShellComponent(value = "Specify Default Values using using @ShellOption")
public class DefaultValues {

	@ShellMethod(value = "Commands to greet user", key = "greet-user", prefix = "-")
	public String greetUser(@ShellOption(defaultValue = "User") String name) {

		StringBuilder builder = new StringBuilder();

		builder.append("Hello ").append(name).append("!!!!");

		return builder.toString();
	}
}

 

In the above snippet, I given default value as "User" for the parameter name.

 

So if you call the command greet-user, then by default it takes the string "User".

 

shell:>greet-user
Hello User!!!!

 

If you pass any string to greet-user command, then this command takes provided value and ignore default value.

shell:>greet-user Ram
Hello Ram!!!!

 

You can download complete application from this link.

https://github.com/harikrishna553/springboot/tree/master/shell/hello-world

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment