Jakarta Commons CLI

Discussion in 'Java' started by FGB, Aug 6, 2003.

  1. FGB

    FGB Guest

    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();
    }
    }
    }
    FGB, Aug 6, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GaryM
    Replies:
    0
    Views:
    376
    GaryM
    Dec 31, 2003
  2. Joshua
    Replies:
    0
    Views:
    562
    Joshua
    Oct 20, 2004
  3. Replies:
    0
    Views:
    398
  4. Replies:
    0
    Views:
    414
  5. Wendy S
    Replies:
    2
    Views:
    2,576
    Wendy S
    Jan 7, 2005
Loading...

Share This Page