View Javadoc

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.commandline;
18  
19  import java.util.Collections;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.apache.commons.cli2.CommandLine;
24  import org.apache.commons.cli2.Option;
25  import org.apache.commons.cli2.resource.ResourceConstants;
26  import org.apache.commons.cli2.resource.ResourceHelper;
27  
28  /**
29   * Instances of CommandLine represent a command line that has been processed
30   * according to the definition supplied to the parser.
31   */
32  public abstract class CommandLineImpl implements CommandLine {
33      public final boolean hasOption(final String trigger) {
34          return hasOption(getOption(trigger));
35      }
36  
37      public final List getValues(final String trigger) {
38          return getValues(getOption(trigger), Collections.EMPTY_LIST);
39      }
40  
41      public final List getValues(final String trigger,
42                                  final List defaultValues) {
43          return getValues(getOption(trigger), defaultValues);
44      }
45  
46      public final List getValues(final Option option) {
47          return getValues(option, Collections.EMPTY_LIST);
48      }
49  
50      public final Object getValue(final String trigger) {
51          return getValue(getOption(trigger), null);
52      }
53  
54      public final Object getValue(final String trigger,
55                                   final Object defaultValue) {
56          return getValue(getOption(trigger), defaultValue);
57      }
58  
59      public final Object getValue(final Option option) {
60          return getValue(option, null);
61      }
62  
63      public final Object getValue(final Option option,
64                                   final Object defaultValue) {
65          final List values;
66  
67          if (defaultValue == null) {
68              values = getValues(option);
69          } else {
70              values = getValues(option, Collections.singletonList(defaultValue));
71          }
72  
73          if (values.size() > 1) {
74              throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.ARGUMENT_TOO_MANY_VALUES));
75          }
76  
77          if (values.isEmpty()) {
78              return defaultValue;
79          }
80  
81          return values.get(0);
82      }
83  
84      public final Boolean getSwitch(final String trigger) {
85          return getSwitch(getOption(trigger), null);
86      }
87  
88      public final Boolean getSwitch(final String trigger,
89                                     final Boolean defaultValue) {
90          return getSwitch(getOption(trigger), defaultValue);
91      }
92  
93      public final Boolean getSwitch(final Option option) {
94          return getSwitch(option, null);
95      }
96  
97      public final String getProperty(final Option option, final String property) {
98          return getProperty(option, property, null);
99      }
100 
101     public final int getOptionCount(final String trigger) {
102         return getOptionCount(getOption(trigger));
103     }
104 
105     public final int getOptionCount(final Option option) {
106         if (option == null) {
107             return 0;
108         }
109 
110         int count = 0;
111 
112         for (Iterator i = getOptions().iterator(); i.hasNext();) {
113             if (option.equals(i.next())) {
114                 ++count;
115             }
116         }
117 
118         return count;
119     }
120 }