1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
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 }