Jakarta Commons CLI

F

FGB

Does anyone know how to make Jakarta Commons CLI accept a negative
integer on the command line?


The example code, run as

java Test -t 4

runs as expected.



But java Test -t -3

throws an exception.



But I want to take negative ints from the command line...


Fred Burkley




(This may not be the best place to post this, but I have not been able
to find much elsewhere).




import org.apache.commons.cli.*;

public class Test {
public static void main(String args[]) {
Option testOption = new Option("t", "test", true, "Enter an int.");
Options options = new Options();
options.addOption(testOption);

CommandLineParser parser = new PosixParser();
try {
CommandLine commandLine = parser.parse(options, args);
if (commandLine.hasOption("t")) {
String opt = commandLine.getOptionValue("t");
System.out.println("Option is " + opt);
System.exit(0);
}
}
catch (ParseException pe) {
pe.printStackTrace();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top