Sunday 31 January 2021

Spring Shell: Customize named parameter keys to use given prefix

In my previous post, I explained how to call the command using named parameters. As you remember, we called named parameters using the prefix --.

 

get-user --name "Krishna" --age 29 --city "Bangalore"

 

We can customize this prefix using the ‘prefix’ attribute of ShellMethod annotation.

 

Example

@ShellComponent(value = "Customize Named Parameters Prefix")
public class CustomizeNamedParamKeysPrefix {
	@ShellMethod(value = "Commands to print Student Details", key = "print-student", prefix="-")
	public String printStudentDetails(String name, int age, String city) {

		StringBuilder builder = new StringBuilder();

		builder.append("Hello ").append(name).append("!!!!").append(". You are ").append(age)
				.append(" years old and you are from ").append(city);

		return builder.toString();
	}
}

  Now you can pass named arguments to the command ‘print-student’ using the prefix -.

 

shell:>print-student -name "Krishna" -age 23 -city Hyderabad
Hello Krishna!!!!. You are 23 years old and you are from Hyderabad

 

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