Skip to content

Commit 6d8ec83

Browse files
tineff96hohwille
andauthored
Fix ide -v and --version handling (#2049)
Co-authored-by: Jörg Hohwiller <hohwille@users.noreply.github.com>
1 parent 9cccda1 commit 6d8ec83

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

cli/src/main/java/com/devonfw/tools/ide/context/AbstractIdeContext.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import com.devonfw.tools.ide.process.ProcessContext;
6767
import com.devonfw.tools.ide.process.ProcessContextImpl;
6868
import com.devonfw.tools.ide.process.ProcessResult;
69+
import com.devonfw.tools.ide.property.KeywordProperty;
6970
import com.devonfw.tools.ide.property.Property;
7071
import com.devonfw.tools.ide.step.Step;
7172
import com.devonfw.tools.ide.step.StepImpl;
@@ -1628,7 +1629,8 @@ public ValidationResult apply(CliArguments arguments, Commandlet cmd) {
16281629
currentProperty = option;
16291630
} else {
16301631
boolean allowDashedValue = (property != null && property.isValue() && property.isMultiValued());
1631-
if (!allowDashedValue && currentArgument.isOption()) {
1632+
boolean allowKeywordOption = (currentProperty instanceof KeywordProperty keywordProperty) && keywordProperty.matches(currentArgument.getKey());
1633+
if (!allowDashedValue && !allowKeywordOption && currentArgument.isOption()) {
16321634
ValidationState state = new ValidationState(null);
16331635
state.addInvalidOption(currentArgument.getKey());
16341636
state.addErrorMessage("Invalid option \"" + currentArgument.getKey() + "\"");

cli/src/main/java/com/devonfw/tools/ide/property/KeywordProperty.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ public boolean matches(String nameOrAlias) {
6767
public boolean apply(CliArguments args, IdeContext context, Commandlet commandlet, CompletionCandidateCollector collector) {
6868

6969
String normalizedName = this.name;
70-
if (args.current().isOption()) {
70+
if (args.current().getKey().equals(this.alias)) {
71+
normalizedName = this.alias;
72+
} else if (args.current().isOption()) {
7173
normalizedName = this.optionName;
7274
}
7375
return apply(normalizedName, args, context, commandlet, collector);

cli/src/test/java/com/devonfw/tools/ide/cli/IdeasyTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import com.devonfw.tools.ide.context.AbstractIdeContextTest;
1111
import com.devonfw.tools.ide.context.IdeTestContext;
12+
import com.devonfw.tools.ide.version.IdeVersion;
1213

1314
/**
1415
* Test of {@link Ideasy}.
@@ -33,6 +34,44 @@ void testEnvOutsideProjectDoesNotLogCliExitException() {
3334
assertThat(context).log().hasNoEntryWithException();
3435
}
3536

37+
/**
38+
* Test of {@code ide --version}.
39+
*/
40+
@Test
41+
void testVersionLongOption() {
42+
43+
// arrange
44+
IdeTestContext context = newContext(Path.of("/"));
45+
Ideasy ideasy = new Ideasy(context);
46+
47+
// act
48+
int exitCode = ideasy.run("--version");
49+
50+
// assert
51+
assertThat(exitCode).isEqualTo(0);
52+
assertThat(context).logAtProcessable().hasMessage(IdeVersion.getVersionString());
53+
assertThat(context).logAtError().hasNoMessageContaining("Unknown command");
54+
}
55+
56+
/**
57+
* Test of {@code ide -v}.
58+
*/
59+
@Test
60+
void testVersionShortOption() {
61+
62+
// arrange
63+
IdeTestContext context = newContext(Path.of("/"));
64+
Ideasy ideasy = new Ideasy(context);
65+
66+
// act
67+
int exitCode = ideasy.run("-v");
68+
69+
// assert
70+
assertThat(exitCode).isEqualTo(0);
71+
assertThat(context).logAtProcessable().hasMessage(IdeVersion.getVersionString());
72+
assertThat(context).logAtError().hasNoMessageContaining("Unknown command");
73+
}
74+
3675
/**
3776
* Test that running 'ide' without arguments does not trigger any tool installation.
3877
* Verifies fix for issue #1667.

0 commit comments

Comments
 (0)