Example 1: Option and an argument

story

I want to pick up a command line that has both an option and an argument. They are not connected so the following command lines are all valid:

command -l width
command -l
command width
command

   1 package com.wategan.cli.demo;
   2 
   3 import junit.framework.TestCase;
   4 import org.apache.commons.cli2.*;
   5 import org.apache.commons.cli2.builder.ArgumentBuilder;
   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 public class CLIDemoWithOneOptionAndAnArgumentTest extends TestCase {
  12 
  13     public void testOptionAndArgument() throws Exception {
  14         CommandLine cmd = parseArgs(new String[]{"-l", "hell"});
  15         assertNotNull(cmd);
  16         Option longOption = cmd.getOption("-l");
  17         Object widthOption = cmd.getValue("width");
  18         assertNotNull(longOption);
  19         assertNotNull(widthOption);
  20 
  21         assertEquals(2, cmd.getOptions().size());
  22         assertEquals("hell", widthOption.toString().trim());
  23         assertEquals("[-l (--length)]", longOption.toString().trim());
  24     }
  25 
  26     public void testDemoWithOneOption() throws Exception {
  27         CommandLine cmd = parseArgs(new String[]{"-l"});
  28         assertNotNull(cmd);
  29         assertEquals(1, cmd.getOptions().size());
  30         Option longOption = cmd.getOption("-l");
  31         assertNotNull(longOption);
  32         assertEquals("[-l (--length)]", longOption.toString().trim());
  33     }
  34 
  35     public void testWithOneArgument() throws Exception {
  36         CommandLine cmd = parseArgs(new String[]{"hell"});
  37         assertNotNull(cmd);
  38         assertEquals(1, cmd.getOptions().size());
  39         Option widthOption = cmd.getOption("width");
  40         assertNotNull(widthOption);
  41         assertEquals("hell", widthOption.toString().trim());
  42     }
  43 
  44     private CommandLine parseArgs(String[] args) {
  45         DefaultOptionBuilder optionBuilder = new DefaultOptionBuilder();
  46         GroupBuilder groupBuilder = new GroupBuilder();
  47 
  48         ArgumentBuilder argumentBuilder = new ArgumentBuilder();
  49 
  50         Argument widthOption = argumentBuilder
  51                 .withMinimum(0)
  52                 .withMaximum(1)
  53                 .withName("width")
  54                 .create();
  55 
  56         DefaultOption longOption = optionBuilder
  57                 .withShortName("l")
  58                 .withLongName("length")
  59                 .create();
  60 
  61 
  62         Group group = groupBuilder.withOption(longOption).withOption(widthOption).create();
  63 
  64         Parser parser = new Parser();
  65         parser.setGroup(group);
  66 
  67         CommandLine cmdLine = null;
  68         try {
  69             cmdLine = parser.parse(args);
  70         } catch (OptionException e) {
  71             System.out.println(e.getMessage());
  72         } finally {
  73         }
  74         return cmdLine;
  75     }
  76 
  77 }
CLIDemoWithOneOptionAndAnArgumentTest.java

Example 2: Option with a argument, and picking it up

story

I want to pick up a command line that has both an option and that option accepts an argument. They are connected so the following command lines are all valid:

command -l width
command -l
command

   1 package com.wategan.cli.demo;
   2 
   3 import junit.framework.TestCase;
   4 import org.apache.commons.cli2.*;
   5 import org.apache.commons.cli2.builder.ArgumentBuilder;
   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 public class CLIDemoWithOneOptionWithAnArgumentTest extends TestCase {
  12 
  13     public void testOptionAndArgument() throws Exception {
  14         CommandLine cmd = parseArgs(new String[]{"-l", "hell"});
  15         assertNotNull(cmd);
  16         assertEquals(1, cmd.getOptions().size());
  17         Option longOption = cmd.getOption("-l");
  18         assertNotNull(longOption);
  19         assertEquals("hell", cmd.getValue("-l"));
  20     }
  21 
  22     public void testOptionAndNoArgument() throws Exception{
  23         CommandLine cmd = parseArgs(new String[]{"-l"});
  24         assertNotNull(cmd);
  25         assertEquals(1, cmd.getOptions().size());
  26         Option longOption = cmd.getOption("-l");
  27         assertNotNull(longOption);
  28         assertEquals("hell", cmd.getValue("-l"));
  29     }
  30 
  31     private CommandLine parseArgs(String[] args) {
  32         DefaultOptionBuilder optionBuilder = new DefaultOptionBuilder();
  33         GroupBuilder groupBuilder = new GroupBuilder();
  34 
  35         ArgumentBuilder argumentBuilder = new ArgumentBuilder();
  36 
  37         Argument widthOption = argumentBuilder
  38                 .withMinimum(0)
  39                 .withMaximum(1)
  40                 .withName("width")
  41                 .create();
  42 
  43         DefaultOption longOption = optionBuilder
  44                 .withShortName("l")
  45                 .withLongName("length")
  46                 .withArgument(widthOption)
  47                 .create();
  48 
  49         Group group = groupBuilder
  50                 .withOption(longOption)
  51                 .create();
  52 
  53         Parser parser = new Parser();
  54         parser.setGroup(group);
  55 
  56         CommandLine cmdLine = null;
  57         try {
  58             cmdLine = parser.parse(args);
  59         } catch (OptionException e) {
  60             System.out.println(e.getMessage());
  61         } finally {
  62         }
  63         return cmdLine;
  64     }
  65 
  66 }
CLIDemoWithOneOptionWithAnArgumentTest.java

CLI2/SampleCodeSingleOptionAndAnArgument (last edited 2009-09-20 23:48:28 by localhost)