• 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

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

3
import com.devonfw.tools.ide.context.IdeContext;
4
import com.devonfw.tools.ide.log.IdeLogLevel;
5
import com.devonfw.tools.ide.log.IdeSubLogger;
6
import com.devonfw.tools.ide.nls.NlsBundle;
7
import com.devonfw.tools.ide.property.CommandletProperty;
8
import com.devonfw.tools.ide.property.KeywordProperty;
9
import com.devonfw.tools.ide.property.Property;
10
import com.devonfw.tools.ide.version.IdeVersion;
11

12
import java.util.ArrayList;
13
import java.util.Collections;
14
import java.util.List;
15

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

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

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

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

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

44
  @Override
45
  public String getName() {
46

47
    return "help";
2✔
48
  }
49

50
  @Override
51
  public boolean isIdeHomeRequired() {
52

53
    return false;
2✔
54
  }
55

56
  private void printLogo() {
57

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

61
  @Override
62
  public void run() {
63

64
    printLogo();
2✔
65
    NlsBundle bundle = NlsBundle.of(this.context);
4✔
66
    this.context.success(bundle.get("version-banner"), IdeVersion.get());
12✔
67
    Commandlet cmd = this.commandlet.getValue();
5✔
68
    if (cmd == null) {
2✔
69
      this.context.info(bundle.get("usage") + " ide [option]* [[commandlet] [arg]*]");
7✔
70
      this.context.info("");
4✔
71
      this.context.info(bundle.get("commandlets"));
6✔
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
    for (Commandlet cmd : this.context.getCommandletManager().getCommandlets()) {
13✔
133
      String key = cmd.getName();
3✔
134
      String keyword = cmd.getKeyword();
3✔
135
      if ((keyword != null) && !keyword.equals(key)) {
6!
136
        key = key + "(" + keyword + ")";
×
137
      }
138
      commandlets.add(key, bundle.get(cmd));
6✔
139
    }
1✔
140
    commandlets.print(IdeLogLevel.INTERACTION);
3✔
141
  }
1✔
142

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

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

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

162
    private final String key;
163

164
    private final String description;
165

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

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

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

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

183
  private class Args {
184

185
    private final List<Arg> args;
186

187
    private int maxKeyLength;
188

189
    private Args() {
3✔
190

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

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

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

200
    private void add(Arg arg) {
201

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

209
    String format(Arg arg) {
210

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

223
    void print() {
224

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

228
    void print(IdeLogLevel level) {
229

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

236
    public List<Arg> get() {
237

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