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

devonfw / IDEasy / 10142209796

29 Jul 2024 10:03AM UTC coverage: 61.2% (+0.03%) from 61.174%
10142209796

push

github

web-flow
#308: Group commandlets into non-tool and tool (#485)

2003 of 3601 branches covered (55.62%)

Branch coverage included in aggregate %.

5308 of 8345 relevant lines covered (63.61%)

2.8 hits per line

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

94.15
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
      String keyword = cmd.getKeyword();
3✔
136
      if ((keyword != null) && !keyword.equals(key)) {
6!
137
        key = key + "(" + keyword + ")";
×
138
      }
139
      if (cmd instanceof ToolCommandlet) {
3✔
140
        toolcommandlets.add(key, bundle.get(cmd));
7✔
141
      } else {
142
        commandlets.add(key, bundle.get(cmd));
6✔
143
      }
144
    }
1✔
145

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

153
  private void collectOptions(Args options, Commandlet cmd, NlsBundle bundle) {
154

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

170
  private static class Arg implements Comparable<Arg> {
171

172
    private final String key;
173

174
    private final String description;
175

176
    private Arg(String key, String description) {
177

178
      super();
2✔
179
      this.key = key;
3✔
180
      this.description = description;
3✔
181
    }
1✔
182

183
    @Override
184
    public int compareTo(Arg arg) {
185

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

193
  private class Args {
194

195
    private final List<Arg> args;
196

197
    private int maxKeyLength;
198

199
    private Args() {
3✔
200

201
      super();
2✔
202
      this.args = new ArrayList<>();
5✔
203
    }
1✔
204

205
    private void add(String key, String description) {
206

207
      add(new Arg(key, description));
7✔
208
    }
1✔
209

210
    private void add(Arg arg) {
211

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

219
    String format(Arg arg) {
220

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

233
    void print() {
234

235
      print(IdeLogLevel.INFO);
3✔
236
    }
1✔
237

238
    void print(IdeLogLevel level) {
239

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

246
    public List<Arg> get() {
247

248
      Collections.sort(this.args);
3✔
249
      return this.args;
3✔
250
    }
251
  }
252
}
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