| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 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 | |
import org.apache.commons.cli2.resource.ResourceHelper; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
public class Command |
| 43 | |
extends ParentImpl { |
| 44 | |
|
| 45 | |
private final String preferredName; |
| 46 | |
|
| 47 | |
|
| 48 | |
private final Set aliases; |
| 49 | |
|
| 50 | |
|
| 51 | |
private final Set triggers; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
public Command(final String preferredName, |
| 74 | |
final String description, |
| 75 | |
final Set aliases, |
| 76 | |
final boolean required, |
| 77 | |
final Argument argument, |
| 78 | |
final Group children, |
| 79 | |
final int id) { |
| 80 | 98 | super(argument, children, description, id, required); |
| 81 | |
|
| 82 | |
|
| 83 | 98 | if ((preferredName == null) || (preferredName.length() < 1)) { |
| 84 | 2 | throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.COMMAND_PREFERRED_NAME_TOO_SHORT)); |
| 85 | |
} |
| 86 | |
|
| 87 | 96 | this.preferredName = preferredName; |
| 88 | |
|
| 89 | |
|
| 90 | 96 | this.aliases = |
| 91 | |
(aliases == null) ? Collections.EMPTY_SET |
| 92 | |
: Collections.unmodifiableSet(new HashSet(aliases)); |
| 93 | |
|
| 94 | |
|
| 95 | 96 | final Set newTriggers = new HashSet(); |
| 96 | 96 | newTriggers.add(preferredName); |
| 97 | 96 | newTriggers.addAll(this.aliases); |
| 98 | 96 | this.triggers = Collections.unmodifiableSet(newTriggers); |
| 99 | 96 | } |
| 100 | |
|
| 101 | |
public void processParent(final WriteableCommandLine commandLine, |
| 102 | |
final ListIterator arguments) |
| 103 | |
throws OptionException { |
| 104 | |
|
| 105 | 20 | final String arg = (String) arguments.next(); |
| 106 | |
|
| 107 | |
|
| 108 | 20 | if (canProcess(commandLine, arg)) { |
| 109 | |
|
| 110 | 19 | commandLine.addOption(this); |
| 111 | |
|
| 112 | |
|
| 113 | 19 | arguments.set(preferredName); |
| 114 | |
} else { |
| 115 | 1 | throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, arg); |
| 116 | |
} |
| 117 | 19 | } |
| 118 | |
|
| 119 | |
public Set getTriggers() { |
| 120 | 646 | return triggers; |
| 121 | |
} |
| 122 | |
|
| 123 | |
public void validate(WriteableCommandLine commandLine) |
| 124 | |
throws OptionException { |
| 125 | 19 | if (isRequired() && !commandLine.hasOption(this)) { |
| 126 | 1 | throw new OptionException(this, ResourceConstants.OPTION_MISSING_REQUIRED, |
| 127 | |
getPreferredName()); |
| 128 | |
} |
| 129 | |
|
| 130 | 18 | super.validate(commandLine); |
| 131 | 17 | } |
| 132 | |
|
| 133 | |
public void appendUsage(final StringBuffer buffer, |
| 134 | |
final Set helpSettings, |
| 135 | |
final Comparator comp) { |
| 136 | |
|
| 137 | 23 | final boolean optional = |
| 138 | |
!isRequired() && helpSettings.contains(DisplaySetting.DISPLAY_OPTIONAL); |
| 139 | 23 | final boolean displayAliases = helpSettings.contains(DisplaySetting.DISPLAY_ALIASES); |
| 140 | |
|
| 141 | 23 | if (optional) { |
| 142 | 6 | buffer.append('['); |
| 143 | |
} |
| 144 | |
|
| 145 | 23 | buffer.append(preferredName); |
| 146 | |
|
| 147 | 23 | if (displayAliases && !aliases.isEmpty()) { |
| 148 | 6 | buffer.append(" ("); |
| 149 | |
|
| 150 | 6 | final List list = new ArrayList(aliases); |
| 151 | 6 | Collections.sort(list); |
| 152 | |
|
| 153 | 6 | for (final Iterator i = list.iterator(); i.hasNext();) { |
| 154 | 10 | final String alias = (String) i.next(); |
| 155 | 10 | buffer.append(alias); |
| 156 | |
|
| 157 | 10 | if (i.hasNext()) { |
| 158 | 4 | buffer.append(','); |
| 159 | |
} |
| 160 | 10 | } |
| 161 | |
|
| 162 | 6 | buffer.append(')'); |
| 163 | |
} |
| 164 | |
|
| 165 | 23 | super.appendUsage(buffer, helpSettings, comp); |
| 166 | |
|
| 167 | 23 | if (optional) { |
| 168 | 6 | buffer.append(']'); |
| 169 | |
} |
| 170 | 23 | } |
| 171 | |
|
| 172 | |
public String getPreferredName() { |
| 173 | 820 | return preferredName; |
| 174 | |
} |
| 175 | |
} |