Wednesday 3 February 2021

Spring Shell: Boolean Values

If a command takes any Boolean parameter and you do not specify the Boolean parameter explicitly, then it takes false by default else true. Boolean parameters behave like they have an arity() of 0 by default, allowing users to set their values using a "flag" approach.

@ShellComponent(value = "Handle Boolean Values")
public class BooleanTypeDemo {

	@ShellMethod(value = "Shutdown the system", key = "shutdown", prefix = "-")
	public void shutdown(boolean force) {
		if(force) {
			System.out.println("Shuting Down the System Forcefully");
			return;
		}
		
		System.out.println("Shuting Down the System Gracefully");
	}
}

 

When you call the command ‘shutdown’ without passing any option/argument then the flag force is set to false.

shell:>shutdown
Shuting Down the System Gracefully

When you call the command ‘shutdown -force’ then the flag force is set to true. You no need to explicitly set the value.

shell:>shutdown -force
Shuting Down the System Forcefully


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