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

devonfw / IDEasy / 12876082916

20 Jan 2025 09:43PM UTC coverage: 68.126% (-0.2%) from 68.372%
12876082916

push

github

web-flow
#919: Require user to agree to license (#948)

2739 of 4397 branches covered (62.29%)

Branch coverage included in aggregate %.

7080 of 10016 relevant lines covered (70.69%)

3.08 hits per line

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

94.12
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.get());
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
  }
1✔
78

79
  private void printCommandletHelp(NlsBundle bundle, Commandlet cmd) {
80

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

117
  private void printCommandlets(NlsBundle bundle) {
118

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

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

144
  private void collectOptions(Args options, Commandlet cmd, NlsBundle bundle) {
145

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

161
  private static class Arg implements Comparable<Arg> {
162

163
    private final String key;
164

165
    private final String description;
166

167
    private Arg(String key, String description) {
168

169
      super();
2✔
170
      this.key = key;
3✔
171
      this.description = description;
3✔
172
    }
1✔
173

174
    @Override
175
    public int compareTo(Arg arg) {
176

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

184
  private class Args {
185

186
    private final List<Arg> args;
187

188
    private int maxKeyLength;
189

190
    private Args() {
3✔
191

192
      super();
2✔
193
      this.args = new ArrayList<>();
5✔
194
    }
1✔
195

196
    private void add(String key, String description) {
197

198
      add(new Arg(key, description));
7✔
199
    }
1✔
200

201
    private void add(Arg arg) {
202

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

210
    String format(Arg arg) {
211

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

224
    void print() {
225

226
      print(IdeLogLevel.INFO);
3✔
227
    }
1✔
228

229
    void print(IdeLogLevel level) {
230

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

237
    public List<Arg> get() {
238

239
      Collections.sort(this.args);
3✔
240
      return this.args;
3✔
241
    }
242
  }
243
}
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