Coverage Report - org.apache.commons.cli2.option.DefaultOption
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultOption
96%
61/63
88%
37/42
3.75
 
 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.commons.cli2.option;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collections;
 21  
 import java.util.Comparator;
 22  
 import java.util.HashSet;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 import java.util.ListIterator;
 26  
 import java.util.Set;
 27  
 
 28  
 import org.apache.commons.cli2.Argument;
 29  
 import org.apache.commons.cli2.DisplaySetting;
 30  
 import org.apache.commons.cli2.Group;
 31  
 import org.apache.commons.cli2.OptionException;
 32  
 import org.apache.commons.cli2.WriteableCommandLine;
 33  
 import org.apache.commons.cli2.resource.ResourceConstants;
 34  
 
 35  
 /**
 36  
  * A Parent implementation representing normal options.
 37  
  */
 38  
 public class DefaultOption
 39  
     extends ParentImpl {
 40  
     /**
 41  
      * The default token used to prefix a short option
 42  
      */
 43  
     public static final String DEFAULT_SHORT_PREFIX = "-";
 44  
 
 45  
     /**
 46  
      * The default token used to prefix a long option
 47  
      */
 48  
     public static final String DEFAULT_LONG_PREFIX = "--";
 49  
 
 50  
     /**
 51  
      * The default value for the burstEnabled constructor parameter
 52  
      */
 53  
     public static final boolean DEFAULT_BURST_ENABLED = true;
 54  
     private final String preferredName;
 55  
     private final Set aliases;
 56  
     private final Set burstAliases;
 57  
     private final Set triggers;
 58  
     private final Set prefixes;
 59  
     private final String shortPrefix;
 60  
     private final boolean burstEnabled;
 61  
     private final int burstLength;
 62  
 
 63  
     /**
 64  
      * Creates a new DefaultOption
 65  
      *
 66  
      * @param shortPrefix the prefix used for short options
 67  
      * @param longPrefix the prefix used for long options
 68  
      * @param burstEnabled should option bursting be enabled
 69  
      * @param preferredName the preferred name for this Option, this should begin with either shortPrefix or longPrefix
 70  
      * @param description a description of this Option
 71  
      * @param aliases the alternative names for this Option
 72  
      * @param burstAliases the aliases that can be burst
 73  
      * @param required whether the Option is strictly required
 74  
      * @param argument the Argument belonging to this Parent, or null
 75  
      * @param children the Group children belonging to this Parent, ot null
 76  
      * @param id the unique identifier for this Option
 77  
      * @throws IllegalArgumentException if the preferredName or an alias isn't
 78  
      *     prefixed with shortPrefix or longPrefix
 79  
      */
 80  
     public DefaultOption(final String shortPrefix,
 81  
                          final String longPrefix,
 82  
                          final boolean burstEnabled,
 83  
                          final String preferredName,
 84  
                          final String description,
 85  
                          final Set aliases,
 86  
                          final Set burstAliases,
 87  
                          final boolean required,
 88  
                          final Argument argument,
 89  
                          final Group children,
 90  
                          final int id) {
 91  1158
         super(argument, children, description, id, required);
 92  
 
 93  1158
         this.shortPrefix = shortPrefix;
 94  1158
         this.burstEnabled = burstEnabled;
 95  
 
 96  1158
         this.burstLength = shortPrefix.length() + 1;
 97  
 
 98  1158
         this.preferredName = preferredName;
 99  1158
         this.aliases =
 100  
             (aliases == null) ? Collections.EMPTY_SET
 101  
                               : Collections.unmodifiableSet(new HashSet(aliases));
 102  
 
 103  1158
         this.burstAliases =
 104  
             (burstAliases == null) ? Collections.EMPTY_SET
 105  
                                    : Collections.unmodifiableSet(new HashSet(burstAliases));
 106  
 
 107  1158
         final Set newTriggers = new HashSet();
 108  1158
         newTriggers.add(preferredName);
 109  1158
         newTriggers.addAll(this.aliases);
 110  1158
         newTriggers.addAll(this.burstAliases);
 111  1158
         this.triggers = Collections.unmodifiableSet(newTriggers);
 112  
 
 113  1158
         final Set newPrefixes = new HashSet(super.getPrefixes());
 114  1158
         newPrefixes.add(shortPrefix);
 115  1158
         newPrefixes.add(longPrefix);
 116  1158
         this.prefixes = Collections.unmodifiableSet(newPrefixes);
 117  
 
 118  1158
         checkPrefixes(newPrefixes);
 119  1158
     }
 120  
 
 121  
     public boolean canProcess(final WriteableCommandLine commandLine,
 122  
                               final String argument) {
 123  165
         return (argument != null) &&
 124  
                (super.canProcess(commandLine, argument) ||
 125  
                ((argument.length() >= burstLength) &&
 126  
                burstAliases.contains(argument.substring(0, burstLength))));
 127  
     }
 128  
 
 129  
     public void processParent(WriteableCommandLine commandLine,
 130  
                               ListIterator arguments)
 131  
         throws OptionException {
 132  117
         final String argument = (String) arguments.next();
 133  
 
 134  117
         if (triggers.contains(argument)) {
 135  97
             commandLine.addOption(this);
 136  97
             arguments.set(preferredName);
 137  20
         } else if (burstEnabled && (argument.length() >= burstLength)) {
 138  20
             final String burst = argument.substring(0, burstLength);
 139  
 
 140  20
             if (burstAliases.contains(burst)) {
 141  20
                 commandLine.addOption(this);
 142  
 
 143  
                 //HMM test bursting all vs bursting one by one.
 144  20
                 arguments.set(preferredName);
 145  
 
 146  20
                 if (getArgument() == null) {
 147  16
                     arguments.add(shortPrefix + argument.substring(burstLength));
 148  
                 } else {
 149  4
                     arguments.add(argument.substring(burstLength));
 150  
                 }
 151  
 
 152  20
                 arguments.previous();
 153  
             } else {
 154  0
                 throw new OptionException(this, ResourceConstants.CANNOT_BURST, argument);
 155  
             }
 156  20
         } else {
 157  0
             throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, argument);
 158  
         }
 159  117
     }
 160  
 
 161  
     public Set getTriggers() {
 162  6882
         return triggers;
 163  
     }
 164  
 
 165  
     public Set getPrefixes() {
 166  3344
         return prefixes;
 167  
     }
 168  
 
 169  
     public void validate(WriteableCommandLine commandLine)
 170  
         throws OptionException {
 171  87
         if (isRequired() && !commandLine.hasOption(this)) {
 172  3
             throw new OptionException(this, ResourceConstants.OPTION_MISSING_REQUIRED,
 173  
                                       getPreferredName());
 174  
         }
 175  
 
 176  84
         super.validate(commandLine);
 177  83
     }
 178  
 
 179  
     public void appendUsage(final StringBuffer buffer,
 180  
                             final Set helpSettings,
 181  
                             final Comparator comp) {
 182  
         // do we display optionality
 183  194
         final boolean optional =
 184  
             !isRequired() && helpSettings.contains(DisplaySetting.DISPLAY_OPTIONAL);
 185  194
         final boolean displayAliases = helpSettings.contains(DisplaySetting.DISPLAY_ALIASES);
 186  
 
 187  194
         if (optional) {
 188  3
             buffer.append('[');
 189  
         }
 190  
 
 191  194
         buffer.append(preferredName);
 192  
 
 193  194
         if (displayAliases && !aliases.isEmpty()) {
 194  63
             buffer.append(" (");
 195  
 
 196  63
             final List list = new ArrayList(aliases);
 197  63
             Collections.sort(list);
 198  
 
 199  63
             for (final Iterator i = list.iterator(); i.hasNext();) {
 200  73
                 final String alias = (String) i.next();
 201  73
                 buffer.append(alias);
 202  
 
 203  73
                 if (i.hasNext()) {
 204  10
                     buffer.append(',');
 205  
                 }
 206  73
             }
 207  
 
 208  63
             buffer.append(')');
 209  
         }
 210  
 
 211  194
         super.appendUsage(buffer, helpSettings, comp);
 212  
 
 213  194
         if (optional) {
 214  3
             buffer.append(']');
 215  
         }
 216  194
     }
 217  
 
 218  
     public String getPreferredName() {
 219  7102
         return preferredName;
 220  
     }
 221  
 }