Home » geronimo-2.2-source-release » org.apache.geronimo.cli » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.geronimo.cli;
   18   
   19   import java.io.OutputStream;
   20   
   21   import org.apache.commons.cli.CommandLine;
   22   import org.apache.commons.cli.CommandLineParser;
   23   import org.apache.commons.cli.GnuParser;
   24   import org.apache.commons.cli.Option;
   25   import org.apache.commons.cli.OptionGroup;
   26   import org.apache.commons.cli.Options;
   27   import org.apache.commons.cli.ParseException;
   28   
   29   
   30   /**
   31    * @version $Rev: 476049 $ $Date: 2006-11-17 15:35:17 +1100 (Fri, 17 Nov 2006) $
   32    */
   33   public class BaseCLParser implements CLParser {
   34       private final static String ARGUMENT_HELP_SHORTFORM = "h";
   35       private final static String ARGUMENT_HELP = "help";
   36   
   37       private final static String ARGUMENT_VERBOSE_INFO_SHORTFORM = "v";
   38       private final static String ARGUMENT_VERBOSE_INFO = "verbose";
   39       
   40       private final static String ARGUMENT_VERBOSE_DEBUG_SHORTFORM = "vv";
   41       private final static String ARGUMENT_VERBOSE_DEBUG = "veryverbose";
   42       
   43       private final static String ARGUMENT_VERBOSE_TRACE_SHORTFORM = "vvv";
   44       private final static String ARGUMENT_VERBOSE_TRACE = "veryveryverbose";
   45       
   46       protected final OutputStream out;
   47       protected final Options options;
   48       protected CommandLine commandLine;
   49       
   50       public BaseCLParser(OutputStream out) {
   51           if (null == out) {
   52               throw new IllegalArgumentException("out is required");
   53           }
   54           this.out = out;
   55           options = new Options();
   56           
   57           addVerboseOptions();
   58           addHelp();
   59       }
   60   
   61       public void parse(String[] args) throws CLParserException {
   62           CommandLineParser parser = new GnuParser();
   63           try {
   64               commandLine = parser.parse(options, args, true);
   65           } catch (ParseException e) {
   66               throw new CLParserException(e);
   67           }
   68           
   69           validateOptions();
   70           validateRemainingArgs();
   71       }
   72   
   73       public boolean isHelp() {
   74           return commandLine.hasOption(ARGUMENT_HELP_SHORTFORM);
   75       }
   76   
   77       public boolean isVerboseInfo() {
   78           return commandLine.hasOption(ARGUMENT_VERBOSE_INFO_SHORTFORM);
   79       }
   80       
   81       public boolean isVerboseDebug() {
   82           return commandLine.hasOption(ARGUMENT_VERBOSE_DEBUG_SHORTFORM);
   83       }
   84   
   85       public boolean isVerboseTrace() {
   86           return commandLine.hasOption(ARGUMENT_VERBOSE_TRACE_SHORTFORM);
   87       }
   88       
   89       public String[] getArgs() {
   90           return commandLine.getArgs();
   91       }
   92   
   93       public void displayHelp() {
   94           throw new UnsupportedOperationException();
   95       }
   96   
   97       protected void displayHelp(String[] args) {
   98           throw new UnsupportedOperationException();
   99       }
  100   
  101       protected void validateOptions() throws CLParserException {
  102       }
  103   
  104       protected void validateRemainingArgs() throws CLParserException {
  105       }
  106       
  107       protected void addHelp() {
  108           options.addOption(ARGUMENT_HELP_SHORTFORM, ARGUMENT_HELP, false, "Display this help.");
  109       }
  110   
  111       protected void addVerboseOptions() {
  112           OptionGroup optionGroup = new OptionGroup();
  113   
  114           Option option = new Option(ARGUMENT_VERBOSE_INFO_SHORTFORM,
  115                   ARGUMENT_VERBOSE_INFO,
  116                   false,
  117                   "Reduces the console log level to INFO, resulting in more console output than is normally present.");
  118           optionGroup.addOption(option);
  119   
  120           option = new Option(ARGUMENT_VERBOSE_DEBUG_SHORTFORM,
  121                   ARGUMENT_VERBOSE_DEBUG,
  122                   false,
  123                   "Reduces the console log level to DEBUG, resulting in still more console output.");
  124           optionGroup.addOption(option);
  125   
  126           option = new Option(ARGUMENT_VERBOSE_TRACE_SHORTFORM,
  127                   ARGUMENT_VERBOSE_TRACE,
  128                   false,
  129                   "Reduces the console log level to TRACE, resulting in still more console output.");
  130           optionGroup.addOption(option);
  131   
  132           options.addOptionGroup(optionGroup);
  133       }
  134   
  135   }

Home » geronimo-2.2-source-release » org.apache.geronimo.cli » [javadoc | source]