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:09:30 UTC

cvs commit: jakarta-avalon-excalibur/cli/examples/incompat_options IncompatOptions.java README.txt

donaldp     02/03/19 03:09:29

  Added:       cli/examples/incompat_options IncompatOptions.java
                        README.txt
  Log:
  Add in example with incompatible options
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/cli/examples/incompat_options/IncompatOptions.java
  
  Index: IncompatOptions.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 example of CLI containing incompatible arguments.
   * In this demo the quiet and verbose options are incompatible with
   * each other.
   *
   * @author <a href="peter@apache.org">Peter Donald</a>
   */
  public class IncompatOptions
  {
      // Define our short one-letter option identifiers.
      private static final int VERBOSE_OPT = 'v';
      private static final int QUIET_OPT = 'q';
  
      /**
       *  Define the understood options. Each CLOptionDescriptor contains:
       * - The "long" version of the option. Eg, "quiet" means that "--quiet" will
       * be recognised.
       * - The option flags, governing the option's argument(s).
       * - The "short" version of the option. Eg, 'q' means that "-q" will be
       * recognised.
       * - A description of the option.
       */
      private static final CLOptionDescriptor[] OPTIONS = new CLOptionDescriptor[]
      {
          new CLOptionDescriptor( "verbose",
                                  CLOptionDescriptor.ARGUMENT_DISALLOWED,
                                  VERBOSE_OPT,
                                  "Run command in verbose mode",
                                  new int[]{QUIET_OPT} ),
          new CLOptionDescriptor( "quiet",
                                  CLOptionDescriptor.ARGUMENT_DISALLOWED,
                                  QUIET_OPT,
                                  "Run command in quiet mode",
                                  new int[]{VERBOSE_OPT} )
      };
  
      public static void main( final String[] args )
      {
          System.out.println( "Starting IncompatOptions..." );
          System.out.println( CLUtil.describeOptions( OPTIONS ) );
          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 VERBOSE_OPT:
                      System.out.println( "Verbose mode!" );
                      break;
  
                  case QUIET_OPT:
                      System.out.println( "Quiet mode!" );
                      break;
              }
          }
      }
  }
  
  
  1.1                  jakarta-avalon-excalibur/cli/examples/incompat_options/README.txt
  
  Index: README.txt
  ===================================================================
  Welcome to Excalibur CLI!
  
  As always, the if you have any questions :
  
  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
     and then follow the link at the bottom to join the lists.
  
  incompat_options
  ----------------
  This simple example shows how to make options incompatible with one another.
  In the application it demonstrates how to make two command line options
  (quiet and verbose) incompatible with each other.
  
  Compile it by running
  
  javac -classpath ../../build/lib/@dist.name@.jar *.java (Unix)
  
  or
  
  javac -classpath ..\..\build\lib\@dist.name@.jar *.java (Windows)
  
  Run it using the scripts provided:
  
    java -classpath ../../build/lib/@dist.name@.jar:. IncompatOptions (Unix)
  
  or
  
    java -classpath ..\..\build\lib\@dist.name@.jar;. IncompatOptions (Windows)
  
  Try passing both quiet and verbose options to the command.
  
  
  

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