You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2002/03/19 12:13:35 UTC

cvs commit: jakarta-avalon-excalibur/cli/examples/basic BasicCLI.java README.txt

donaldp     02/03/19 03:13:35

  Modified:    cli/examples/basic README.txt
  Added:       cli/examples/basic BasicCLI.java
  Log:
  Add a basic example yoinked from the package.html
  
  Revision  Changes    Path
  1.3       +16 -9     jakarta-avalon-excalibur/cli/examples/basic/README.txt
  
  Index: README.txt
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/cli/examples/basic/README.txt,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- README.txt	18 Mar 2002 12:53:17 -0000	1.2
  +++ README.txt	19 Mar 2002 11:13:35 -0000	1.3
  @@ -5,15 +5,15 @@
   1) Make sure you followed any directions
   2) Review documentation included in this package, or online at
         http://jakarta.apache.org/avalon/excalibur
  -3) Ask on the avalon-dev list.  This is a great source of support 
  -   information. To join, read http://jakarta.apache.org/site/mail.html 
  +3) Ask on the avalon-dev list.  This is a great source of support
  +   information. To join, read http://jakarta.apache.org/site/mail.html
      and then follow the link at the bottom to join the lists.
   
  -basic 
  +basic
   -----
   This simple example shows how to use the CLI Component for basic command
  -line parsing. It demonstrates displaying basic construction of parser and 
  -just prints out the command line arguments passed by user.
  +line parsing. It also demonstrates generating a usage message from list
  +of options.
   
   Compile it by running
   
  @@ -25,13 +25,20 @@
   
   Run it using the scripts provided:
   
  -  ./basic.sh (Unix)
  +  java -classpath ../../build/lib/@dist.name@.jar:. BasicCLI (Unix)
   
   or
   
  -  .\basic.bat (Windows)
  +  java -classpath ..\..\build\lib\@dist.name@.jar;. BasicCLI (Windows)
   
   
  -Try passing it the "--help" command line option to see which other 
  -command line options are available. The experiment by passing in 
  +Try passing it the "--help" command line option to see which other
  +command line options are available. The experiment by passing in
   different options.
  +
  +Some combinations to try out include
  +
  +--help
  +-h
  +h
  +--version
  
  
  
  1.1                  jakarta-avalon-excalibur/cli/examples/basic/BasicCLI.java
  
  Index: BasicCLI.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  
  import java.util.List;
  import org.apache.avalon.excalibur.cli.CLArgsParser;
  import org.apache.avalon.excalibur.cli.CLOption;
  import org.apache.avalon.excalibur.cli.CLOptionDescriptor;
  import org.apache.avalon.excalibur.cli.CLUtil;
  
  /**
   * Demonstrates Basic example of command line utilities.
   *
   * @author <a href="jeff@socialchange.net.au">Jeff Turner</a>
   * @author <a href="peter@apache.org">Peter Donald</a>
   */
  public class BasicCLI
  {
      // Define our short one-letter option identifiers.
      private static final int HELP_OPT = 'h';
      private static final int VERSION_OPT = 'v';
  
      /**
       *  Define the understood options. Each CLOptionDescriptor contains:
       * - The "long" version of the option. Eg, "help" means that "--help" will
       * be recognised.
       * - The option flags, governing the option's argument(s).
       * - The "short" version of the option. Eg, 'h' means that "-h" will be
       * recognised.
       * - A description of the option.
       */
      private static final CLOptionDescriptor[] OPTIONS = new CLOptionDescriptor[]
      {
          new CLOptionDescriptor( "help",
                                  CLOptionDescriptor.ARGUMENT_DISALLOWED,
                                  HELP_OPT,
                                  "print this message and exit" ),
          new CLOptionDescriptor( "version",
                                  CLOptionDescriptor.ARGUMENT_DISALLOWED,
                                  VERSION_OPT,
                                  "print the version information and exit" )
      };
  
      public static void main( final String[] args )
      {
          System.out.println( "Starting BasicCLI..." );
          System.out.println();
  
          // Parse the arguments
          final CLArgsParser parser = new CLArgsParser( args, OPTIONS );
  
          //Make sure that there was no errors parsing
          //arguments
          if( null != parser.getErrorString() )
          {
              System.err.println( "Error: " + parser.getErrorString() );
              return;
          }
  
          // Get a list of parsed options
          final List options = parser.getArguments();
          final int size = options.size();
  
          for( int i = 0; i < size; i++ )
          {
              final CLOption option = (CLOption)options.get( i );
  
              switch( option.getId() )
              {
                  case CLOption.TEXT_ARGUMENT:
                      //This occurs when a user supplies an argument that
                      //is not an option
                      System.out.println( "Unknown arg: " + option.getArgument() );
                      break;
  
                  case HELP_OPT:
                      printUsage();
                      break;
  
                  case VERSION_OPT:
                      printVersion();
                      break;
              }
          }
      }
  
      /**
       * Print out a dummy version
       */
      private static void printVersion()
      {
          System.out.println( "1.0" );
          System.exit( 0 );
      }
  
      /**
       * Print out a usage statement
       */
      private static void printUsage()
      {
          final String lineSeparator = System.getProperty( "line.separator" );
  
          final StringBuffer msg = new StringBuffer();
  
          msg.append( lineSeparator );
          msg.append( "Excalibur command-line arg parser demo" );
          msg.append( lineSeparator );
          msg.append( "Usage: java " + IncompatOptions.class.getName() + " [options]" );
          msg.append( lineSeparator );
          msg.append( lineSeparator );
          msg.append( "Options: " );
          msg.append( lineSeparator );
  
          /*
           * Notice that the next line uses CLUtil.describeOptions to generate the
           * list of descriptions for each option
           */
          msg.append( CLUtil.describeOptions( BasicCLI.OPTIONS ).toString() );
  
          System.out.println( msg.toString() );
  
          System.exit( 0 );
      }
  }
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>