This example shows how to create a command with optional arguments with the annotated approach:
@Command(names = "greet")
public class GreetingCommand implements CommandClass {
@Command(names = "")
public void run(String name, @OptArg("Mr.") String title) {
System.out.println("Hello, " + title + " " + name + "!");
}
}The @OptArg annotation is used to mark an argument as optional, and
it accepts a default value as a parameter, which will be used if the
argument is not present in the input.
- Executing
greet Johnwill printHello, Mr. John! - Executing
greet John Dr.will printHello, Dr. John! - Executing
greet John Mr.will printHello, Mr. John!