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

devonfw / IDEasy / 12181235589

05 Dec 2024 01:58PM UTC coverage: 66.902% (-0.02%) from 66.917%
12181235589

push

github

web-flow
#508: enabled autocompletion for commandlet options (#833)

2527 of 4130 branches covered (61.19%)

Branch coverage included in aggregate %.

6577 of 9478 relevant lines covered (69.39%)

3.06 hits per line

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

67.92
cli/src/main/java/com/devonfw/tools/ide/cli/CliArguments.java
1
package com.devonfw.tools.ide.cli;
2

3
import java.util.Iterator;
4

5
/**
6
 * Wraps {@link CliArgument} as state object allowing to consume arguments.
7
 */
8
public class CliArguments implements Iterator<CliArgument> {
9

10
  private final CliArgument initialArgument;
11

12
  private CliArgument currentArg;
13

14
  private boolean endOptions;
15

16
  private boolean splitShortOpts;
17

18
  /**
19
   * The constructor.
20
   *
21
   * @param args the {@link CliArgument#of(String...) command line arguments}.
22
   */
23
  public CliArguments(String... args) {
24

25
    this(CliArgument.of(args));
4✔
26
  }
1✔
27

28
  /**
29
   * The constructor.
30
   *
31
   * @param arg the {@link #current() initial} {@link CliArgument}.
32
   */
33
  public CliArguments(CliArgument arg) {
34

35
    this(arg, false, true);
5✔
36
  }
1✔
37

38
  CliArguments(CliArgument arg, boolean endOpts, boolean splitShortOpts) {
39

40
    super();
2✔
41
    this.initialArgument = arg;
3✔
42
    this.endOptions = endOpts;
3✔
43
    this.splitShortOpts = splitShortOpts;
3✔
44
    setCurrent(arg);
3✔
45
  }
1✔
46

47
  /**
48
   * Marks the end of the options so no further {@link CliArgument#getNext(boolean) option splitting} will be performed.
49
   *
50
   * @see #stopSplitShortOptions()
51
   */
52
  public void endOptions() {
53

54
    this.endOptions = true;
3✔
55
    this.splitShortOpts = false;
3✔
56
  }
1✔
57

58
  /**
59
   * Stops splitting of short options.
60
   *
61
   * @see CliArgument#getNext(boolean)
62
   * @see #endOptions()
63
   */
64
  public void stopSplitShortOptions() {
65

66
    this.splitShortOpts = false;
3✔
67
  }
1✔
68

69
  /**
70
   * @return {@code true} if short options (e.g. "-bdf") should not be split (e.g. into "-b -d -f" for "--batch --debug --force"), {@code false} otherwise.
71
   */
72
  public boolean isSplitShortOpts() {
73

74
    return splitShortOpts;
3✔
75
  }
76

77
  /**
78
   * @return {@code true} if the options have ended, {@code false} otherwise.
79
   * @see CliArgument#isEndOptions()
80
   * @see com.devonfw.tools.ide.property.Property#isEndOptions()
81
   * @see #endOptions()
82
   */
83
  public boolean isEndOptions() {
84

85
    return this.endOptions;
3✔
86
  }
87

88
  private void setCurrent(CliArgument arg) {
89

90
    if (arg.isEndOptions()) {
3!
91
      endOptions();
×
92
      this.currentArg = arg.getNext();
×
93
    } else {
94
      this.currentArg = arg;
3✔
95
    }
96
  }
1✔
97

98
  /**
99
   * @return {@code true} if the last argument shall be {@link CliArgument#isCompletion() completed}, {@code false}.
100
   */
101
  public boolean isCompletion() {
102

103
    CliArgument arg = this.currentArg;
×
104
    while ((arg != null) && !arg.isEnd()) {
×
105
      if (arg.isCompletion()) {
×
106
        return true;
×
107
      }
108
      arg = arg.next;
×
109
    }
110
    return false;
×
111
  }
112

113
  /**
114
   * @return the initial {@link CliArgument}.
115
   */
116
  public CliArgument getInitialArgument() {
117

118
    return this.initialArgument;
×
119
  }
120

121
  /**
122
   * @return the current {@link CliArgument}.
123
   * @see #hasNext()
124
   * @see #next()
125
   */
126
  public CliArgument current() {
127

128
    return this.currentArg;
3✔
129
  }
130

131
  @Override
132
  public boolean hasNext() {
133

134
    if (this.currentArg.isEnd()) {
4✔
135
      return false;
2✔
136
    }
137
    return !this.currentArg.next.isEnd();
9✔
138
  }
139

140
  /**
141
   * Consumes the {@link #current() current argument} and proceeds to the next one.
142
   *
143
   * @return the next {@link CliArgument}.
144
   */
145
  @Override
146
  public CliArgument next() {
147

148
    if (!this.currentArg.isEnd()) {
4!
149
      setCurrent(this.currentArg.getNext(this.splitShortOpts));
7✔
150
    }
151
    return this.currentArg;
3✔
152
  }
153

154
  /**
155
   * @return a copy of this {@link CliArguments} to fork a CLI matching of auto-completion.
156
   */
157
  public CliArguments copy() {
158

159
    return new CliArguments(this.currentArg, this.endOptions, this.splitShortOpts);
10✔
160
  }
161

162
  @Override
163
  public String toString() {
164

165
    return this.currentArg.getArgs();
4✔
166
  }
167

168
  /**
169
   * @param args the {@link CliArgument#of(String...) command line arguments}.
170
   * @return the {@link CliArguments}.
171
   */
172
  public static CliArguments ofCompletion(String... args) {
173

174
    return new CliArguments(CliArgument.ofCompletion(args));
6✔
175
  }
176

177
}
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