diff --git a/src/main/java/picocli/CommandLine.java b/src/main/java/picocli/CommandLine.java index fb3caf2f0..81e4df731 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: " + 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() { 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()); }