• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 9808947434

05 Jul 2024 01:16PM UTC coverage: 61.755% (-0.04%) from 61.798%
9808947434

push

github

web-flow
#396: Display the tools help output on 'ide help <tool>'  (#434)

1987 of 3535 branches covered (56.21%)

Branch coverage included in aggregate %.

5276 of 8226 relevant lines covered (64.14%)

2.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.2
cli/src/main/java/com/devonfw/tools/ide/commandlet/Commandlet.java
1
package com.devonfw.tools.ide.commandlet;
2

3
import com.devonfw.tools.ide.context.IdeContext;
4
import com.devonfw.tools.ide.nls.NlsBundle;
5
import com.devonfw.tools.ide.property.KeywordProperty;
6
import com.devonfw.tools.ide.property.Property;
7
import com.devonfw.tools.ide.tool.ToolCommandlet;
8
import com.devonfw.tools.ide.version.VersionIdentifier;
9

10
import java.util.ArrayList;
11
import java.util.Collections;
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.Objects;
16

17
/**
18
 * A {@link Commandlet} is a sub-command of the IDE CLI.
19
 */
20
public abstract class Commandlet {
1✔
21

22
  /** The {@link IdeContext} instance. */
23
  protected final IdeContext context;
24

25
  private final List<Property<?>> propertiesList;
26

27
  private final List<Property<?>> properties;
28

29
  private final List<Property<?>> valuesList;
30

31
  private final List<Property<?>> values;
32

33
  private final Map<String, Property<?>> optionMap;
34

35
  private Property<?> multiValued;
36

37
  private String firstKeyword;
38

39
  /**
40
   * The constructor.
41
   *
42
   * @param context the {@link IdeContext}.
43
   */
44
  public Commandlet(IdeContext context) {
45

46
    super();
2✔
47
    this.context = context;
3✔
48
    this.propertiesList = new ArrayList<>();
5✔
49
    this.properties = Collections.unmodifiableList(this.propertiesList);
5✔
50
    this.valuesList = new ArrayList<>();
5✔
51
    this.values = Collections.unmodifiableList(this.valuesList);
5✔
52
    this.optionMap = new HashMap<>();
5✔
53
  }
1✔
54

55
  /**
56
   * @return the {@link List} with all {@link Property properties} of this {@link Commandlet}.
57
   */
58
  public List<Property<?>> getProperties() {
59

60
    return this.properties;
3✔
61
  }
62

63
  /**
64
   * @return the {@link List} of {@link Property properties} that are {@link Property#isValue() values}.
65
   */
66
  public List<Property<?>> getValues() {
67

68
    return this.values;
3✔
69
  }
70

71
  /**
72
   * Clear the set values on all properties of the {@link Commandlet#propertiesList}
73
   */
74
  public void clearProperties() {
75

76
    for (Property<?> property : this.propertiesList) {
×
77
      property.clearValue();
×
78
    }
×
79
  }
×
80

81
  /**
82
   * @param nameOrAlias the potential {@link Property#getName() name} or {@link Property#getAlias() alias} of the
83
   *     requested {@link Property}.
84
   * @return the requested {@link Property property} or {@code null} if not found.
85
   */
86
  public Property<?> getOption(String nameOrAlias) {
87

88
    return this.optionMap.get(nameOrAlias);
6✔
89
  }
90

91
  /**
92
   * @param keyword the {@link KeywordProperty keyword} to {@link #add(Property) add}.
93
   */
94
  protected void addKeyword(String keyword) {
95

96
    if (this.properties.isEmpty()) {
4✔
97
      this.firstKeyword = keyword;
3✔
98
    }
99
    add(new KeywordProperty(keyword, true, null));
9✔
100
  }
1✔
101

102
  /**
103
   * @param property the keyword {@link Property} to {@link #add(Property) add}.
104
   */
105
  protected void addKeyword(Property<?> property) {
106

107
    if (!this.properties.isEmpty()) {
4!
108
      throw new IllegalStateException();
×
109
    }
110
    this.firstKeyword = property.getNameOrAlias();
4✔
111
    add(property);
4✔
112
  }
1✔
113

114
  /**
115
   * @param <P> type of the {@link Property}.
116
   * @param property the {@link Property} to register.
117
   * @return the given {@link Property}.
118
   */
119
  protected <P extends Property<?>> P add(P property) {
120

121
    if (this.multiValued != null) {
3!
122
      throw new IllegalStateException("The multi-valued property " + this.multiValued + " can not be followed by " + property);
×
123
    }
124
    this.propertiesList.add(property);
5✔
125
    if (property.isOption()) {
3✔
126
      add(property.getName(), property, false);
6✔
127
      add(property.getAlias(), property, true);
6✔
128
    }
129
    if (property.isValue()) {
3✔
130
      this.valuesList.add(property);
5✔
131
    }
132
    if (property.isMultiValued()) {
3✔
133
      this.multiValued = property;
3✔
134
    }
135
    return property;
2✔
136
  }
137

138
  private void add(String name, Property<?> property, boolean alias) {
139

140
    if (alias && (name == null)) {
4✔
141
      return;
1✔
142
    }
143
    Objects.requireNonNull(name);
3✔
144
    assert (name.equals(name.trim()));
6!
145
    if (name.isEmpty() && !alias) {
3!
146
      return;
×
147
    }
148
    Property<?> duplicate = this.optionMap.put(name, property);
7✔
149
    if (duplicate != null) {
2!
150
      throw new IllegalStateException("Duplicate name or alias " + name + " for " + property + " and " + duplicate);
×
151
    }
152
  }
1✔
153

154
  /**
155
   * @return the name of this {@link Commandlet} (e.g. "help").
156
   */
157
  public abstract String getName();
158

159
  /**
160
   * @return the first keyword of this {@link Commandlet}. Typically the same as {@link #getName() name} but may also differ (e.g. "set" vs. "set-version").
161
   */
162
  public String getKeyword() {
163

164
    return this.firstKeyword;
3✔
165
  }
166

167
  /**
168
   * @param <C> type of the {@link Commandlet}.
169
   * @param commandletType the {@link Class} reflecting the requested {@link Commandlet}.
170
   * @return the requested {@link Commandlet}.
171
   * @see CommandletManager#getCommandlet(Class)
172
   */
173
  protected <C extends Commandlet> C getCommandlet(Class<C> commandletType) {
174

175
    return this.context.getCommandletManager().getCommandlet(commandletType);
6✔
176
  }
177

178
  /**
179
   * @return {@code true} if {@link IdeContext#getIdeHome() IDE_HOME} is required for this commandlet, {@code false} otherwise.
180
   */
181
  public boolean isIdeHomeRequired() {
182

183
    return true;
×
184
  }
185

186
  /**
187
   * @return {@code true} to suppress the {@link com.devonfw.tools.ide.step.StepImpl#logSummary(boolean) step summary success message}.
188
   */
189
  public boolean isSuppressStepSuccess() {
190

191
    return false;
×
192
  }
193

194
  /**
195
   * Runs this {@link Commandlet}.
196
   */
197
  public abstract void run();
198

199
  /**
200
   * @return {@code true} if this {@link Commandlet} is the valid candidate to be {@link #run()}, {@code false} otherwise.
201
   * @see Property#validate()
202
   */
203
  public boolean validate() {
204

205
    // avoid validation exception if not a candidate to be run.
206
    for (Property<?> property : this.propertiesList) {
×
207
      if (property.isRequired() && (property.getValue() == null)) {
×
208
        return false;
×
209
      }
210
    }
×
211
    for (Property<?> property : this.propertiesList) {
×
212
      if (!property.validate()) {
×
213
        return false;
×
214
      }
215
    }
×
216
    return true;
×
217
  }
218

219
  /**
220
   * Provide additional usage help of this {@link Commandlet} to the user.
221
   */
222
  public void printHelp(NlsBundle bundle) {
223

224
  }
1✔
225

226
  @Override
227
  public String toString() {
228

229
    return getClass().getSimpleName() + "[" + getName() + "]";
7✔
230
  }
231

232
  /**
233
   * @return the {@link ToolCommandlet} set in a {@link Property} of this commandlet used for auto-completion of a {@link VersionIdentifier} or {@code null} if
234
   * not exists or not configured.
235
   */
236
  public ToolCommandlet getToolForVersionCompletion() {
237

238
    return null;
×
239
  }
240
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc