From ffb928511c2b3fb2377eca6edf44e767f2c3bb41 Mon Sep 17 00:00:00 2001 From: David Pond Date: Fri, 19 May 2023 11:34:32 +0100 Subject: [PATCH 1/3] Failing tests --- src/test/java/picocli/UnmatchedArgumentExceptionTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/picocli/UnmatchedArgumentExceptionTest.java b/src/test/java/picocli/UnmatchedArgumentExceptionTest.java index 8804fdeda..a2a83c9c3 100644 --- a/src/test/java/picocli/UnmatchedArgumentExceptionTest.java +++ b/src/test/java/picocli/UnmatchedArgumentExceptionTest.java @@ -80,7 +80,7 @@ public void testUnmatchedArgumentSuggestsSubcommands() { Demo.mainCommand().parseWithHandler(((CommandLine.IParseResultHandler)null), new PrintStream(baos), new String[]{"chekcout"}); String expected = format("" + "Unmatched argument at index 0: 'chekcout'%n" + - "Did you mean: checkout or help or branch?%n"); + "Did you mean: git checkout or git help or git branch?%n"); assertEquals(expected, baos.toString()); } @Test @@ -90,7 +90,7 @@ public void testUnmatchedArgumentSuggestsSubcommands2() { Demo.mainCommand().parseWithHandler(((CommandLine.IParseResultHandler)null), new PrintStream(baos), new String[]{"me"}); String expected = format("" + "Unmatched argument at index 0: 'me'%n" + - "Did you mean: merge?%n"); + "Did you mean: git merge?%n"); assertEquals(expected, baos.toString()); } From 77e9941d93c5caae06d33b7564cfea6a754e7b9e Mon Sep 17 00:00:00 2001 From: David Pond Date: Fri, 19 May 2023 11:55:12 +0100 Subject: [PATCH 2/3] Fixed tests Still fails due to a deprecation warning related to System::setSecurityManager. Sorry - I'm not familiar enough with Java to know what should be done about it. --- src/main/java/picocli/CommandLine.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/picocli/CommandLine.java b/src/main/java/picocli/CommandLine.java index fb3caf2f0..37ce9b5a7 100644 --- a/src/main/java/picocli/CommandLine.java +++ b/src/main/java/picocli/CommandLine.java @@ -18487,7 +18487,7 @@ public static class TypeConversionException extends PicocliException { /** Exception indicating something went wrong while parsing command line options. */ public static class ParameterException extends PicocliException { private static final long serialVersionUID = 1477112829129763139L; - private final CommandLine commandLine; + protected final CommandLine commandLine; private ArgSpec argSpec = null; private String value = null; @@ -18659,7 +18659,7 @@ public boolean printSuggestions(PrintWriter writer) { if (!suggestions.isEmpty()) { writer.println(isUnknownOption() ? "Possible solutions: " + str(suggestions) - : "Did you mean: " + str(suggestions).replace(", ", " or ") + "?"); + : "Did you mean: " + this.commandLine.commandSpec.name + " " + str(suggestions).replace(", ", " or ") + "?"); writer.flush(); } return !suggestions.isEmpty(); From 8a54980680b0c8443d905621bedc1a599b184c97 Mon Sep 17 00:00:00 2001 From: David Pond Date: Sat, 20 May 2023 12:51:24 +0100 Subject: [PATCH 3/3] Ensure all subcommands prefixed with commandname This time I've actually fixed the tests. Previously I mistook the deprecation warning for the reason the tests failed. --- src/main/java/picocli/CommandLine.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/java/picocli/CommandLine.java b/src/main/java/picocli/CommandLine.java index 37ce9b5a7..81e4df731 100644 --- a/src/main/java/picocli/CommandLine.java +++ b/src/main/java/picocli/CommandLine.java @@ -18659,7 +18659,7 @@ public boolean printSuggestions(PrintWriter writer) { if (!suggestions.isEmpty()) { writer.println(isUnknownOption() ? "Possible solutions: " + str(suggestions) - : "Did you mean: " + this.commandLine.commandSpec.name + " " + str(suggestions).replace(", ", " or ") + "?"); + : "Did you mean: " + str(prefixCommandName(suggestions)).replace(", ", " or ") + "?"); writer.flush(); } return !suggestions.isEmpty(); @@ -18668,6 +18668,18 @@ private static String str(List list) { String s = list.toString(); return s.substring(0, s.length() - 1).substring(1); } + + private List prefixCommandName(List suggestions) + { + String commandName = this.commandLine.commandSpec.name; + if(commandName == null || commandName.trim().isEmpty()) { return suggestions; } + List prefixedSuggestions = new ArrayList(); + for (String s : suggestions) { + prefixedSuggestions.add(commandName + " " + s); + } + return prefixedSuggestions; + } + /** Returns suggested solutions if such solutions exist, otherwise returns an empty list. * @since 3.3.0 */ public List getSuggestions() {