Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Commons Wiki" for 
change notification.

The following page has been changed by Andrew Oxenburgh:
http://wiki.apache.org/commons/CLI2/sampleApplication

------------------------------------------------------------------------------
  3. call command with long option (eg 'calledCommand --length') which should 
display "found '-l'"
  
  I have included the source for both the application and the unit tests as 
attachements here.
- #!java
- {{{
- package com.wategan.cli.demo;
  
+ inline:CLIDemoWithOneArgument.java
- import org.apache.commons.cli2.CommandLine;
- import org.apache.commons.cli2.Group;
- import org.apache.commons.cli2.builder.DefaultOptionBuilder;
- import org.apache.commons.cli2.builder.GroupBuilder;
- import org.apache.commons.cli2.commandline.Parser;
- import org.apache.commons.cli2.option.DefaultOption;
  
- /**
-  * Very simple program which takes an optional '-l' or '--length' argument.
-  * It then prints out whether it found the option or not.
-  */
- public class CLIDemoWithOneArgument {
-     private final String LENGTH_OPTION_SHORT_NAME = "l";
-     private final String LENGTH_OPTION_LONG_NAME = "length";
- 
-     CLIDemoWithOneArgument() {
-     }
- 
-     private CommandLine parseArgs(String[] args) {
-         DefaultOptionBuilder optionBuilder = new DefaultOptionBuilder();
-         GroupBuilder groupBuilder = new GroupBuilder();
- 
-         DefaultOption longOption = optionBuilder
-                 .withShortName(LENGTH_OPTION_SHORT_NAME)
-                 .withLongName(LENGTH_OPTION_LONG_NAME)
-                 .create();
- 
-         Group group = groupBuilder.withOption(longOption).create();
- 
-         Parser parser = new Parser();
-         parser.setGroup(group);
- 
-         return parser.parseAndHelp(args);
-     }
- 
-     public void parseArgsAndTestOptions(String[] args) {
-         CommandLine cmd = parseArgs(args);
-         checkForOptionWithSmallName(cmd, "l");
-     }
- 
-     private void checkForOptionWithSmallName(CommandLine cmd, String 
optionSmallName) {
-         if (cmd.hasOption("-" +
-                 optionSmallName)) {
-             System.out.println("found '" + optionSmallName + "'");
-         } else {
-             System.out.println("didn't find '" + optionSmallName + "'");
-         }
-     }
- 
-     public static void main(String[] args) {
-         new CLIDemoWithOneArgument().parseArgsAndTestOptions(args);
-     }
- }
- }}}
- 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to