If user input contains a string with multiple spaces in it, then always quote the string in single or double quotes.
@ShellComponent(value = "Handle Single and Double Quotes")
public class QuotesDemo {
@ShellMethod(value = "Echo Message", key = "echo", prefix = "-")
public void echo(String message) {
System.out.println(message);
}
}
Above snippet define a command ‘echo’ that takes a string argument and print the message as it is.
shell:>echo hello hello
Now echo a string that contain space in it.
shell:>echo hello world
[31mToo many arguments: the following could not be mapped to parameters: 'world'[0m
[31mDetails of the error have been omitted. You can use the [1mstacktrace[22m command to print the full stacktrace.[0m
As you see the output, you can observe that Spring shell unable to parse the command. To solve this problem either encode the string in single or double quotes.
shell:>echo 'Hello World'
Hello World
shell:>
shell:>echo "Hello World"
Hello World
Using single and double quotes, you can easily embed one type of quotes into a value.
shell:>echo "Hello 'Krishna', How Are You?"
Hello 'Krishna', How Are You?
shell:>
shell:>echo 'Hello "Krishna", How Are You?'
Hello "Krishna", How Are You?
You can download complete application from this link.
https://github.com/harikrishna553/springboot/tree/master/shell/hello-world
No comments:
Post a Comment