0

I have switched to spring shell version 2. Consider I have a command accepting one argument:

@shellMethod
public void greet(@ShellOption String name){
   System.out.println(String.format("Hello %s", name);
}

The behavior in case of entering greet --name Bakir and greet Bakir is the same. Meaning that the spring shell assigns Bakir to name even if it is not specified by the user. In case there are multiple arguments and the user forgets to enter one switch in the right place (depending on the method's args list order) would lead to a silent error. Is there any way to stop this kind of assignment in spring shell 2.x?

kvs
  • 178
  • 2
  • 12

1 Answers1

1

You can declare method that have multiple parameters. For example you can try this below.

@ShellMethod
public String example(
    @ShellOption(value = { "-a" }) String arg1,
    @ShellOption(value = { "-b" }) String arg2,
    @ShellOption(value = { "-c" }) String arg3
) {
    return "Hello " + arg1;
}

For details, you can read the documents in Spring Shell Documentation

Mehmet Bektaş
  • 173
  • 1
  • 2
  • 15
  • that changes nothing. if I run `example one two three` it works. What I want is that spring shell complains it doesn't expect such parameters by saying e.g. I only accept such flags otherwise it's an error. (`example -a p1 -b p2 -c p3` => OK, `example p1 p2 p3` => ERROR) – kvs Sep 29 '22 at 17:56
  • Ok, you're right. What do you think to use array string. For example: example p1,p2,p3 Then split by "," and access the parameters? – Mehmet Bektaş Sep 29 '22 at 18:26
  • I'm not sure if you have used Spring Shell 1.x. Back then if you omitted the flag it would respond `Option '' is not available for this command. Use tab assist or the "help" command to see the legal options` I don't really get it why they have changed that behavior and fallen into this problematic and confusing one. – kvs Sep 29 '22 at 18:38