001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.geronimo.cli; 018 019 import java.io.OutputStream; 020 021 import org.apache.commons.cli.CommandLine; 022 import org.apache.commons.cli.CommandLineParser; 023 import org.apache.commons.cli.GnuParser; 024 import org.apache.commons.cli.Option; 025 import org.apache.commons.cli.OptionGroup; 026 import org.apache.commons.cli.Options; 027 import org.apache.commons.cli.ParseException; 028 029 030 /** 031 * @version $Rev: 476049 $ $Date: 2006-11-17 15:35:17 +1100 (Fri, 17 Nov 2006) $ 032 */ 033 public class BaseCLParser implements CLParser { 034 private final static String ARGUMENT_HELP_SHORTFORM = "h"; 035 private final static String ARGUMENT_HELP = "help"; 036 037 private final static String ARGUMENT_VERBOSE_INFO_SHORTFORM = "v"; 038 private final static String ARGUMENT_VERBOSE_INFO = "verbose"; 039 040 private final static String ARGUMENT_VERBOSE_DEBUG_SHORTFORM = "vv"; 041 private final static String ARGUMENT_VERBOSE_DEBUG = "veryverbose"; 042 043 private final static String ARGUMENT_VERBOSE_TRACE_SHORTFORM = "vvv"; 044 private final static String ARGUMENT_VERBOSE_TRACE = "veryveryverbose"; 045 046 protected final OutputStream out; 047 protected final Options options; 048 protected CommandLine commandLine; 049 050 public BaseCLParser(OutputStream out) { 051 if (null == out) { 052 throw new IllegalArgumentException("out is required"); 053 } 054 this.out = out; 055 options = new Options(); 056 057 addVerboseOptions(); 058 addHelp(); 059 } 060 061 public void parse(String[] args) throws CLParserException { 062 CommandLineParser parser = new GnuParser(); 063 try { 064 commandLine = parser.parse(options, args, true); 065 } catch (ParseException e) { 066 throw new CLParserException(e); 067 } 068 069 validateOptions(); 070 validateRemainingArgs(); 071 } 072 073 public boolean isHelp() { 074 return commandLine.hasOption(ARGUMENT_HELP_SHORTFORM); 075 } 076 077 public boolean isVerboseInfo() { 078 return commandLine.hasOption(ARGUMENT_VERBOSE_INFO_SHORTFORM); 079 } 080 081 public boolean isVerboseDebug() { 082 return commandLine.hasOption(ARGUMENT_VERBOSE_DEBUG_SHORTFORM); 083 } 084 085 public boolean isVerboseTrace() { 086 return commandLine.hasOption(ARGUMENT_VERBOSE_TRACE_SHORTFORM); 087 } 088 089 public String[] getArgs() { 090 return commandLine.getArgs(); 091 } 092 093 public void displayHelp() { 094 throw new UnsupportedOperationException(); 095 } 096 097 protected void displayHelp(String[] args) { 098 throw new UnsupportedOperationException(); 099 } 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 }