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

devonfw / IDEasy / 12185831515

05 Dec 2024 06:28PM UTC coverage: 66.953% (+0.06%) from 66.892%
12185831515

push

github

web-flow
#846: fix version shortopt (-v) (#847)

2532 of 4134 branches covered (61.25%)

Branch coverage included in aggregate %.

6587 of 9486 relevant lines covered (69.44%)

3.06 hits per line

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

94.22
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 {
21

22
  static final String LOGO = """
4✔
23
      __       ___ ___  ___
24
      ╲ ╲     |_ _|   ╲| __|__ _ ____ _
25
       > >     | || |) | _|/ _` (_-< || |
26
      /_/ ___ |___|___/|___╲__,_/__/╲_, |
27
         |___|                       |__/
28
      """.replace('╲', '\\');
2✔
29

30
  /** The optional commandlet to get help about. */
31
  public final CommandletProperty commandlet;
32

33
  /**
34
   * The constructor.
35
   *
36
   * @param context the {@link IdeContext}.
37
   */
38
  public HelpCommandlet(IdeContext context) {
39

40
    super(context);
3✔
41
    addKeyword(getName());
4✔
42
    this.commandlet = add(new CommandletProperty("", false, "commandlet"));
11✔
43
  }
1✔
44

45
  @Override
46
  public String getName() {
47

48
    return "help";
2✔
49
  }
50

51
  @Override
52
  public boolean isIdeRootRequired() {
53

54
    return false;
2✔
55
  }
56

57
  private void printLogo() {
58

59
    this.context.info(LOGO);
4✔
60
  }
1✔
61

62
  @Override
63
  public void run() {
64

65
    printLogo();
2✔
66
    NlsBundle bundle = NlsBundle.of(this.context);
4✔
67
    this.context.success(bundle.get("version-banner"), IdeVersion.get());
12✔
68
    Commandlet cmd = this.commandlet.getValue();
5✔
69
    if (cmd == null) {
2✔
70
      this.context.info(bundle.get("usage") + " ide [option]* [[commandlet] [arg]*]");
7✔
71
      this.context.info("");
4✔
72
      printCommandlets(bundle);
4✔
73
    } else {
74
      printCommandletHelp(bundle, cmd);
4✔
75
    }
76
    this.context.info("");
4✔
77
    this.context.info(bundle.get("options"));
6✔
78
    Args options = new Args();
5✔
79
    ContextCommandlet cxtCmd = new ContextCommandlet();
4✔
80
    collectOptions(options, cxtCmd, bundle);
5✔
81
    if (cmd != null) {
2✔
82
      collectOptions(options, cmd, bundle);
5✔
83
    }
84
    options.print();
2✔
85
    if (cmd == null) {
2✔
86
      this.context.info("");
4✔
87
      this.context.info(bundle.getDetail(this.context.getCommandletManager().getCommandlet(HelpCommandlet.class)));
10✔
88
    }
89
  }
1✔
90

91
  private void printCommandletHelp(NlsBundle bundle, Commandlet cmd) {
92

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

129
  private void printCommandlets(NlsBundle bundle) {
130

131
    Args commandlets = new Args();
5✔
132
    Args toolcommandlets = new Args();
5✔
133
    for (Commandlet cmd : this.context.getCommandletManager().getCommandlets()) {
13✔
134
      String key = cmd.getName();
3✔
135
      KeywordProperty keyword = cmd.getFirstKeyword();
3✔
136
      if (keyword != null) {
2!
137
        String name = keyword.getName();
3✔
138
        if (!name.equals(key)) {
4!
139
          key = key + "(" + keyword + ")";
×
140
        }
141
      }
142
      if (cmd instanceof ToolCommandlet) {
3✔
143
        toolcommandlets.add(key, bundle.get(cmd));
7✔
144
      } else {
145
        commandlets.add(key, bundle.get(cmd));
6✔
146
      }
147
    }
1✔
148

149
    this.context.info(bundle.get("commandlets"));
6✔
150
    commandlets.print(IdeLogLevel.INTERACTION);
3✔
151
    this.context.info("");
4✔
152
    this.context.info(bundle.get("toolcommandlets"));
6✔
153
    toolcommandlets.print(IdeLogLevel.INTERACTION);
3✔
154
  }
1✔
155

156
  private void collectOptions(Args options, Commandlet cmd, NlsBundle bundle) {
157

158
    for (Property<?> property : cmd.getProperties()) {
11✔
159
      if (property.isOption() && !property.isRequired()) {
6!
160
        String id = property.getAlias();
3✔
161
        String name = property.getName();
3✔
162
        if (id == null) {
2✔
163
          id = name;
3✔
164
        } else {
165
          id = id + " | " + name;
4✔
166
        }
167
        String description = bundle.get(cmd, property);
5✔
168
        options.add(id, description);
4✔
169
      }
170
    }
1✔
171
  }
1✔
172

173
  private static class Arg implements Comparable<Arg> {
174

175
    private final String key;
176

177
    private final String description;
178

179
    private Arg(String key, String description) {
180

181
      super();
2✔
182
      this.key = key;
3✔
183
      this.description = description;
3✔
184
    }
1✔
185

186
    @Override
187
    public int compareTo(Arg arg) {
188

189
      if (arg == null) {
2!
190
        return 1;
×
191
      }
192
      return this.key.compareTo(arg.key);
6✔
193
    }
194
  }
195

196
  private class Args {
197

198
    private final List<Arg> args;
199

200
    private int maxKeyLength;
201

202
    private Args() {
3✔
203

204
      super();
2✔
205
      this.args = new ArrayList<>();
5✔
206
    }
1✔
207

208
    private void add(String key, String description) {
209

210
      add(new Arg(key, description));
7✔
211
    }
1✔
212

213
    private void add(Arg arg) {
214

215
      this.args.add(arg);
5✔
216
      int keyLength = arg.key.length();
4✔
217
      if (keyLength > this.maxKeyLength) {
4✔
218
        this.maxKeyLength = keyLength;
3✔
219
      }
220
    }
1✔
221

222
    String format(Arg arg) {
223

224
      StringBuilder sb = new StringBuilder(this.maxKeyLength + 2 + arg.description.length());
12✔
225
      sb.append(arg.key);
5✔
226
      int delta = this.maxKeyLength - arg.key.length();
7✔
227
      while (delta > 0) {
2✔
228
        sb.append(' ');
4✔
229
        delta--;
2✔
230
      }
231
      sb.append("  ");
4✔
232
      sb.append(arg.description);
5✔
233
      return sb.toString();
3✔
234
    }
235

236
    void print() {
237

238
      print(IdeLogLevel.INFO);
3✔
239
    }
1✔
240

241
    void print(IdeLogLevel level) {
242

243
      IdeSubLogger logger = HelpCommandlet.this.context.level(level);
6✔
244
      for (Arg arg : get()) {
11✔
245
        logger.log(format(arg));
5✔
246
      }
1✔
247
    }
1✔
248

249
    public List<Arg> get() {
250

251
      Collections.sort(this.args);
3✔
252
      return this.args;
3✔
253
    }
254
  }
255
}
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