Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subcommand suggestions #2027

Merged
merged 3 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -18668,6 +18668,18 @@ private static String str(List<String> list) {
String s = list.toString();
return s.substring(0, s.length() - 1).substring(1);
}

private List<String> prefixCommandName(List<String> suggestions)
{
String commandName = this.commandLine.commandSpec.name;
if(commandName == null || commandName.trim().isEmpty()) { return suggestions; }
List<String> prefixedSuggestions = new ArrayList<String>();
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<String> getSuggestions() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/picocli/UnmatchedArgumentExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}

Expand Down