I got the code below from a sample code from tutorials point and tweaked it a little bit.
App.java
public static void main(String[] args) throws ParseException {
CommandTest t = new CommandTest();
t.start(args);
}
CommandTest.java
public class CommandTest {
void start(String[] args) throws ParseException {
//***Definition Stage***
// create Options object
Options options = new Options();
// add option "-a"
options.addOption(
Option.builder("a")
.longOpt("add")
.desc("add numbers")
.hasArg(false)
.valueSeparator('=')
.required(false)
.build()
);
// add option "-m"
options.addOption("m", false, "");
options.addOption(
Option.builder("m")
.longOpt("multiply")
.desc("multiply numbers")
.hasArg(false)
.valueSeparator('=')
.required(false)
.build()
);
//***Parsing Stage***
//Create a parser
CommandLineParser parser = new DefaultParser();
//parse the options passed as command line arguments
CommandLine cmd = parser.parse( options, args);
//***Interrogation Stage***
//hasOptions checks if option is present or not
if(cmd.hasOption("a")) {
System.out.println("Sum of the numbers: " + getSum(args));
} else if(cmd.hasOption("m")) {
System.out.println("Multiplication of the numbers: " + getMultiplication(args));
}
}
public static int getSum(String[] args) {
int sum = 0;
for(int i = 1; i < args.length ; i++) {
sum += Integer.parseInt(args[i]);
}
return sum;
}
public static int getMultiplication(String[] args) {
int multiplication = 1;
for(int i = 1; i < args.length ; i++) {
multiplication *= Integer.parseInt(args[i]);
}
return multiplication;
}
}
Now, my question is that, when i try to execute the above code with a parameter of -multi
it will still be accepted? I've already set the options to receive only either -m
or -multiply
. However, it will still accept -multi
I am using commons-cli-1.3.1 (im trying to debug a legacy code by the way)
Note: Above source is a SAMPLE source only, no need to apply actual coding guidelines (good or bad) i just want to know why the behavior happens as it is.
This is the behaviour when a non-matching option gets found (org.apache.commons.cli.Options:233):
public List<String> getMatchingOptions(String opt) {
opt = Util.stripLeadingHyphens(opt);
List<String> matchingOpts = new ArrayList();
if (this.longOpts.keySet().contains(opt)) {
return Collections.singletonList(opt);
} else {
Iterator var3 = this.longOpts.keySet().iterator();
while(var3.hasNext()) {
String longOpt = (String)var3.next();
/******************************************************/
/* longOpt = "multiply" */
/* opt = "multi" */
/******************************************************/
if (longOpt.startsWith(opt)) {
matchingOpts.add(longOpt);
}
/******************************************************/
}
return matchingOpts;
}
}
As you can see in the highlighted block, if a short option isn't matched the library searches for the first long option that partially matches the entered option. It uses startsWith
, and since "multiply".startsWith("multi")
is true
it defaults to option --multiply
.