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

devonfw / IDEasy / 19227342529

10 Nov 2025 09:43AM UTC coverage: 68.872% (-0.007%) from 68.879%
19227342529

push

github

web-flow
#1510: Add icd command hint to help output (#1574)

Co-authored-by: jan-vcapgemini <59438728+jan-vcapgemini@users.noreply.github.com>

3487 of 5549 branches covered (62.84%)

Branch coverage included in aggregate %.

9131 of 12772 relevant lines covered (71.49%)

3.14 hits per line

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

94.19
cli/src/main/java/com/devonfw/tools/ide/commandlet/HelpCommandlet.java
1
package com.devonfw.tools.ide.commandlet;
2

3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.List;
6

7
import com.devonfw.tools.ide.context.IdeContext;
8
import com.devonfw.tools.ide.log.IdeLogLevel;
9
import com.devonfw.tools.ide.log.IdeSubLogger;
10
import com.devonfw.tools.ide.nls.NlsBundle;
11
import com.devonfw.tools.ide.property.CommandletProperty;
12
import com.devonfw.tools.ide.property.KeywordProperty;
13
import com.devonfw.tools.ide.property.Property;
14
import com.devonfw.tools.ide.tool.ToolCommandlet;
15
import com.devonfw.tools.ide.version.IdeVersion;
16

17
/**
18
 * {@link Commandlet} to print the environment variables.
19
 */
20
public final class HelpCommandlet extends Commandlet {
1✔
21

22
  /** The optional commandlet to get help about. */
23
  public final CommandletProperty commandlet;
24

25
  /**
26
   * The constructor.
27
   *
28
   * @param context the {@link IdeContext}.
29
   */
30
  public HelpCommandlet(IdeContext context) {
31

32
    super(context);
3✔
33
    addKeyword("--help", "-h");
4✔
34
    this.commandlet = add(new CommandletProperty("", false, "commandlet"));
11✔
35
  }
1✔
36

37
  @Override
38
  public String getName() {
39

40
    return "help";
2✔
41
  }
42

43
  @Override
44
  public boolean isIdeRootRequired() {
45

46
    return false;
2✔
47
  }
48

49

50
  @Override
51
  public void run() {
52

53
    this.context.printLogo();
3✔
54
    NlsBundle bundle = NlsBundle.of(this.context);
4✔
55
    this.context.success(bundle.get("version-banner"), IdeVersion.getVersionString());
12✔
56
    Commandlet cmd = this.commandlet.getValue();
5✔
57
    if (cmd == null) {
2✔
58
      this.context.info(bundle.get("usage") + " ide [option]* [[commandlet] [arg]*]");
7✔
59
      this.context.info("");
4✔
60
      printCommandlets(bundle);
4✔
61
    } else {
62
      printCommandletHelp(bundle, cmd);
4✔
63
    }
64
    this.context.info("");
4✔
65
    this.context.info(bundle.get("options"));
6✔
66
    Args options = new Args();
5✔
67
    ContextCommandlet cxtCmd = new ContextCommandlet();
4✔
68
    collectOptions(options, cxtCmd, bundle);
5✔
69
    if (cmd != null) {
2✔
70
      collectOptions(options, cmd, bundle);
5✔
71
    }
72
    options.print();
2✔
73
    if (cmd == null) {
2✔
74
      this.context.info("");
4✔
75
      this.context.info(bundle.getDetail(this.context.getCommandletManager().getCommandlet(HelpCommandlet.class)));
10✔
76
    }
77

78
    this.context.info("");
4✔
79
    this.context.info(bundle.get("icd-hint"));
6✔
80
  }
1✔
81

82
  private void printCommandletHelp(NlsBundle bundle, Commandlet cmd) {
83

84
    StringBuilder usage = new StringBuilder();
4✔
85
    Args values = new Args();
5✔
86
    usage.append(bundle.get("usage"));
6✔
87
    usage.append(" ide [option]*");
4✔
88
    for (Property<?> property : cmd.getProperties()) {
11✔
89
      if (property.isValue() || property.isRequired()) {
3!
90
        usage.append(" ");
4✔
91
        if (!property.isRequired()) {
3✔
92
          usage.append('[');
4✔
93
        }
94
        String name = property.getName();
3✔
95
        if (name.isEmpty()) {
3✔
96
          assert !(property instanceof KeywordProperty);
4!
97
          String key = "<" + property.getAlias() + ">";
4✔
98
          usage.append(key);
4✔
99
          values.add(key, bundle.get(cmd, property));
7✔
100
        } else {
1✔
101
          usage.append(name);
4✔
102
        }
103
        if (property.isMultiValued()) {
3✔
104
          usage.append('*');
4✔
105
        }
106
        if (!property.isRequired()) {
3✔
107
          usage.append(']');
4✔
108
        }
109
      }
110
    }
1✔
111
    this.context.info(usage.toString());
5✔
112
    this.context.info(bundle.get(cmd));
6✔
113
    this.context.info(bundle.getDetail(cmd));
6✔
114
    this.context.info("");
4✔
115
    this.context.info(bundle.get("values"));
6✔
116
    values.print();
2✔
117
    cmd.printHelp(bundle);
3✔
118
  }
1✔
119

120
  private void printCommandlets(NlsBundle bundle) {
121

122
    Args commandlets = new Args();
5✔
123
    Args toolcommandlets = new Args();
5✔
124
    for (Commandlet cmd : this.context.getCommandletManager().getCommandlets()) {
13✔
125
      String key = cmd.getName();
3✔
126
      KeywordProperty keyword = cmd.getFirstKeyword();
3✔
127
      if (keyword != null) {
2!
128
        String name = keyword.getName();
3✔
129
        if (!name.equals(key)) {
4!
130
          key = key + "(" + keyword + ")";
×
131
        }
132
      }
133
      if (cmd instanceof ToolCommandlet) {
3✔
134
        toolcommandlets.add(key, bundle.get(cmd));
7✔
135
      } else {
136
        commandlets.add(key, bundle.get(cmd));
6✔
137
      }
138
    }
1✔
139

140
    this.context.info(bundle.get("commandlets"));
6✔
141
    commandlets.print(IdeLogLevel.INTERACTION);
3✔
142
    this.context.info("");
4✔
143
    this.context.info(bundle.get("toolcommandlets"));
6✔
144
    toolcommandlets.print(IdeLogLevel.INTERACTION);
3✔
145
  }
1✔
146

147
  private void collectOptions(Args options, Commandlet cmd, NlsBundle bundle) {
148

149
    for (Property<?> property : cmd.getProperties()) {
11✔
150
      if (property.isOption() && !property.isRequired()) {
6!
151
        String id = property.getAlias();
3✔
152
        String name = property.getName();
3✔
153
        if (id == null) {
2✔
154
          id = name;
3✔
155
        } else {
156
          id = id + " | " + name;
4✔
157
        }
158
        String description = bundle.get(cmd, property);
5✔
159
        options.add(id, description);
4✔
160
      }
161
    }
1✔
162
  }
1✔
163

164
  private static class Arg implements Comparable<Arg> {
165

166
    private final String key;
167

168
    private final String description;
169

170
    private Arg(String key, String description) {
171

172
      super();
2✔
173
      this.key = key;
3✔
174
      this.description = description;
3✔
175
    }
1✔
176

177
    @Override
178
    public int compareTo(Arg arg) {
179

180
      if (arg == null) {
2!
181
        return 1;
×
182
      }
183
      return this.key.compareTo(arg.key);
6✔
184
    }
185
  }
186

187
  private class Args {
188

189
    private final List<Arg> args;
190

191
    private int maxKeyLength;
192

193
    private Args() {
3✔
194

195
      super();
2✔
196
      this.args = new ArrayList<>();
5✔
197
    }
1✔
198

199
    private void add(String key, String description) {
200

201
      add(new Arg(key, description));
7✔
202
    }
1✔
203

204
    private void add(Arg arg) {
205

206
      this.args.add(arg);
5✔
207
      int keyLength = arg.key.length();
4✔
208
      if (keyLength > this.maxKeyLength) {
4✔
209
        this.maxKeyLength = keyLength;
3✔
210
      }
211
    }
1✔
212

213
    String format(Arg arg) {
214

215
      StringBuilder sb = new StringBuilder(this.maxKeyLength + 2 + arg.description.length());
12✔
216
      sb.append(arg.key);
5✔
217
      int delta = this.maxKeyLength - arg.key.length();
7✔
218
      while (delta > 0) {
2✔
219
        sb.append(' ');
4✔
220
        delta--;
2✔
221
      }
222
      sb.append("  ");
4✔
223
      sb.append(arg.description);
5✔
224
      return sb.toString();
3✔
225
    }
226

227
    void print() {
228

229
      print(IdeLogLevel.INFO);
3✔
230
    }
1✔
231

232
    void print(IdeLogLevel level) {
233

234
      IdeSubLogger logger = HelpCommandlet.this.context.level(level);
6✔
235
      for (Arg arg : get()) {
11✔
236
        logger.log(format(arg));
5✔
237
      }
1✔
238
    }
1✔
239

240
    public List<Arg> get() {
241

242
      Collections.sort(this.args);
3✔
243
      return this.args;
3✔
244
    }
245
  }
246
}
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

© 2025 Coveralls, Inc