From 732518893a3981fedc7414fb1f7bcea501210b1c Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Tue, 27 Aug 2024 13:49:45 +0200 Subject: [PATCH] Avoid using a dynamic regexp --- .../cli/src/main/java/io/quarkus/cli/QuarkusCli.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/devtools/cli/src/main/java/io/quarkus/cli/QuarkusCli.java b/devtools/cli/src/main/java/io/quarkus/cli/QuarkusCli.java index c39ea351ecbcc..ef9ad75138415 100644 --- a/devtools/cli/src/main/java/io/quarkus/cli/QuarkusCli.java +++ b/devtools/cli/src/main/java/io/quarkus/cli/QuarkusCli.java @@ -12,7 +12,6 @@ import java.util.Optional; import java.util.concurrent.Callable; import java.util.function.Supplier; -import java.util.regex.Pattern; import java.util.stream.Collectors; import jakarta.inject.Inject; @@ -178,7 +177,7 @@ public Optional checkMissingCommand(CommandLine root, String[] args) { if (!unmatchedSubcommands.isEmpty()) { missingCommand.append("-").append(unmatchedSubcommands.get(0)); // We don't want the root itself to be added to the result - return Optional.of(missingCommand.toString().replaceFirst(Pattern.quote(root.getCommandName() + "-"), "")); + return Optional.of(stripRootPrefix(missingCommand.toString(), root.getCommandName() + "-")); } currentParseResult = currentParseResult.subcommand(); @@ -195,6 +194,14 @@ public Optional checkMissingCommand(CommandLine root, String[] args) { } } + private static String stripRootPrefix(String command, String rootPrefix) { + if (!command.startsWith(rootPrefix)) { + return command; + } + + return command.substring(rootPrefix.length()); + } + @Override public Integer call() throws Exception { output.info("%n@|bold Quarkus CLI|@ version %s", Version.clientVersion());