From 367865d17e6a15732c70efecbfb417a07c178dcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Hohwiller?= Date: Thu, 5 Dec 2024 19:28:13 +0100 Subject: [PATCH] #846: fix version shortopt (-v) (#847) --- .../tools/ide/commandlet/Commandlet.java | 24 +++++++------------ .../ide/commandlet/CommandletManagerImpl.java | 12 ++++++++-- .../tools/ide/commandlet/HelpCommandlet.java | 9 ++++--- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/cli/src/main/java/com/devonfw/tools/ide/commandlet/Commandlet.java b/cli/src/main/java/com/devonfw/tools/ide/commandlet/Commandlet.java index 158243593..8ad317634 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/commandlet/Commandlet.java +++ b/cli/src/main/java/com/devonfw/tools/ide/commandlet/Commandlet.java @@ -36,7 +36,7 @@ public abstract class Commandlet { private Property multiValued; - private String firstKeyword; + private KeywordProperty firstKeyword; /** * The constructor. @@ -103,21 +103,13 @@ protected void addKeyword(String keyword) { */ protected void addKeyword(String keyword, String alias) { - if (this.properties.isEmpty()) { - this.firstKeyword = keyword; + KeywordProperty property = new KeywordProperty(keyword, true, alias); + if (this.firstKeyword == null) { + if (!this.properties.isEmpty()) { + throw new IllegalStateException(property + " must be first property in " + getClass().getSimpleName()); + } + this.firstKeyword = property; } - add(new KeywordProperty(keyword, true, alias)); - } - - /** - * @param property the keyword {@link Property} to {@link #add(Property) add}. - */ - protected void addKeyword(Property property) { - - if (!this.properties.isEmpty()) { - throw new IllegalStateException(); - } - this.firstKeyword = property.getNameOrAlias(); add(property); } @@ -169,7 +161,7 @@ private void add(String name, Property property, boolean alias) { /** * @return the first keyword of this {@link Commandlet}. Typically the same as {@link #getName() name} but may also differ (e.g. "set" vs. "set-version"). */ - public String getKeyword() { + public KeywordProperty getFirstKeyword() { return this.firstKeyword; } diff --git a/cli/src/main/java/com/devonfw/tools/ide/commandlet/CommandletManagerImpl.java b/cli/src/main/java/com/devonfw/tools/ide/commandlet/CommandletManagerImpl.java index e4c200fbb..7c61d3684 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/commandlet/CommandletManagerImpl.java +++ b/cli/src/main/java/com/devonfw/tools/ide/commandlet/CommandletManagerImpl.java @@ -131,9 +131,17 @@ protected void add(Commandlet commandlet) { boolean hasRequiredProperty = false; List> properties = commandlet.getProperties(); int propertyCount = properties.size(); - String keyword = commandlet.getKeyword(); + KeywordProperty keyword = commandlet.getFirstKeyword(); if (keyword != null) { - this.firstKeywordMap.putIfAbsent(keyword, commandlet); + String name = keyword.getName(); + this.firstKeywordMap.putIfAbsent(name, commandlet); + if (name.startsWith("--")) { + this.firstKeywordMap.putIfAbsent(name.substring(2), commandlet); + } + String alias = keyword.getAlias(); + if (alias != null) { + this.firstKeywordMap.putIfAbsent(alias, commandlet); + } } for (int i = 0; i < propertyCount; i++) { Property property = properties.get(i); diff --git a/cli/src/main/java/com/devonfw/tools/ide/commandlet/HelpCommandlet.java b/cli/src/main/java/com/devonfw/tools/ide/commandlet/HelpCommandlet.java index 835c2a3c8..426b03f16 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/commandlet/HelpCommandlet.java +++ b/cli/src/main/java/com/devonfw/tools/ide/commandlet/HelpCommandlet.java @@ -132,9 +132,12 @@ private void printCommandlets(NlsBundle bundle) { Args toolcommandlets = new Args(); for (Commandlet cmd : this.context.getCommandletManager().getCommandlets()) { String key = cmd.getName(); - String keyword = cmd.getKeyword(); - if ((keyword != null) && !keyword.equals(key)) { - key = key + "(" + keyword + ")"; + KeywordProperty keyword = cmd.getFirstKeyword(); + if (keyword != null) { + String name = keyword.getName(); + if (!name.equals(key)) { + key = key + "(" + keyword + ")"; + } } if (cmd instanceof ToolCommandlet) { toolcommandlets.add(key, bundle.get(cmd));