CLI Release 2 is not available as a download yet, though it is via a snapshot though SVN.

It differs greatly from CLI version 1 and deserves to be released. Here is a sample application that demonstrates the most simple use case.

story

"I want to add a single option to a program and have it print out on System.out whether that option was added on the command line. The option may be entered as '-l', or '--length'".

Use Cases

I have included JUnit tests but here are the possibilities.

1. call command without an option (eg calledCommand) which should display "didn't find '-l'"

2. call command with short option (eg calledCommand -l) which should display "found '-l'"

3. call command with long option (eg calledCommand --length) which should display "found '-l'"

4. call command with invalid long option (eg calledCommand --invalid) which should display some error.

5. call command with invalid short option (eg calledCommand -i) which should display some error.

I have included the source for both the application and the unit tests as attachements here.

   1 package com.wategan.cli.demo;
   2 
   3 import org.apache.commons.cli2.CommandLine;
   4 import org.apache.commons.cli2.Group;
   5 import org.apache.commons.cli2.OptionException;
   6 import org.apache.commons.cli2.builder.DefaultOptionBuilder;
   7 import org.apache.commons.cli2.builder.GroupBuilder;
   8 import org.apache.commons.cli2.commandline.Parser;
   9 import org.apache.commons.cli2.option.DefaultOption;
  10 
  11 /**
  12  * Very simple program which takes an optional '-l' or '--length' argument.
  13  * It then prints out whether it found the option or not.
  14  */
  15 public class CLIDemoWithOneOptionNoArgument {
  16     private final String LENGTH_OPTION_SHORT_NAME = "l";
  17     private final String LENGTH_OPTION_LONG_NAME = "length";
  18 
  19     CLIDemoWithOneOptionNoArgument() {
  20     }
  21 
  22     private CommandLine parseArgs(String[] args) {
  23         DefaultOptionBuilder optionBuilder = new DefaultOptionBuilder();
  24         GroupBuilder groupBuilder = new GroupBuilder();
  25 
  26         DefaultOption longOption = optionBuilder
  27                 .withShortName(LENGTH_OPTION_SHORT_NAME)
  28                 .withLongName(LENGTH_OPTION_LONG_NAME)
  29                 .create();
  30 
  31         Group group = groupBuilder.withOption(longOption).create();
  32 
  33         Parser parser = new Parser();
  34         parser.setGroup(group);
  35 
  36         CommandLine cmdLine = null;
  37         try {
  38             cmdLine = parser.parse(args);
  39         } catch (OptionException e) {
  40             System.out.println(e.getMessage());
  41         } finally {
  42         }
  43         return cmdLine;
  44     }
  45 
  46     public void parseArgsAndTestOptions(String[] args) {
  47         CommandLine cmd = parseArgs(args);
  48         if (cmd != null) {
  49             checkForOptionWithSmallName(cmd, "l");
  50         }
  51     }
  52 
  53     private void checkForOptionWithSmallName(CommandLine cmd, String optionSmallName) {
  54         if (cmd.hasOption("-" +
  55                 optionSmallName)) {
  56             System.out.println("found '" + optionSmallName + "'");
  57         } else {
  58             System.out.println("didn't find '" + optionSmallName + "'");
  59         }
  60     }
  61 
  62     public static void main(String[] args) {
  63         new CLIDemoWithOneOptionNoArgument().parseArgsAndTestOptions(args);
  64     }
  65 }
CLIDemoWithOneOptionNoArgument.java

   1 package com.wategan.cli.demo;
   2 
   3 import junit.framework.TestCase;
   4 
   5 import java.io.ByteArrayOutputStream;
   6 import java.io.PrintStream;
   7 
   8 public class CLIDemoWithOneOptionNoArgumentTest extends TestCase {
   9     public void testZeroOptions() throws Exception {
  10         String[] linesOutput = runTestsReturnArrayOfStrings(new String[]{});
  11         checkDidntFindOption("l", linesOutput[0]);
  12     }
  13 
  14     public void testLengthShortOption() throws Exception {
  15         String[] linesOutput = runTestsReturnArrayOfStrings(new String[]{"-l"});
  16         checkFoundOption("l", linesOutput[0]);
  17     }
  18 
  19     public void testLengthLongOption() throws Exception {
  20         String[] strings = runTestsReturnArrayOfStrings(new String[]{"--length"});
  21         checkFoundOption("l", strings[0]);
  22     }
  23 
  24     public void testInvalidLongOption() throws Exception {
  25         String[] strings = runTestsReturnArrayOfStrings(new String[]{"--invalid"});
  26         assertEquals("Unexpected --invalid while processing -l".trim(), strings[0].trim());
  27     }
  28 
  29     public void testInvalidShortOption() throws Exception {
  30         String[] strings = runTestsReturnArrayOfStrings(new String[]{"-i"});
  31         assertEquals("Unexpected -i while processing -l".trim(), strings[0].trim());
  32     }
  33 
  34     private void checkFoundOption(String option, String string) {
  35         assertEquals(("found '" + option + "'").trim(), string.trim());
  36     }
  37 
  38     private String[] runTestsReturnArrayOfStrings(String[] args) {
  39         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  40         PrintStream printStream = new PrintStream(baos);
  41         PrintStream origStream = System.out ;
  42         System.setOut(printStream);
  43         new CLIDemoWithOneOptionNoArgument().parseArgsAndTestOptions(args);
  44         printStream.flush();
  45         System.setOut(origStream);
  46         String[] strings = baos.toString().split("\\n");
  47         return strings;
  48     }
  49 
  50     private void checkDidntFindOption(String option, String string) {
  51         assertEquals(("didn't find '" + option + "'").trim(), string.trim());
  52     }
  53 }
CLIDemoWithOneOptionNoArgumentTest.java

CLI2/SampleCodeSingleArgument (last edited 2009-09-20 23:48:17 by localhost)