Skip to content

Commit

Permalink
[#1388] Fix subcommand aliases autocomplete regression
Browse files Browse the repository at this point in the history
  • Loading branch information
NewbieOrange authored and remkop committed Oct 31, 2021
1 parent 3421ba6 commit eb22ff1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/picocli/AutoComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ private static void generateFunctionCallsToArrContains(StringBuilder buff,
int count = functionCalls.size();
CommandSpec spec = descriptor.commandLine.getCommandSpec();
String full = spec.qualifiedName(" ");
String withoutTopLevelCommand = full.substring(spec.root().name().length() + 1);
String withoutTopLevelCommand = full.substring(spec.root().name().length() + 1).replace(spec.name(), descriptor.commandName);

functionCalls.add(format(" if CompWordsContainsArray \"${cmds%2$d[@]}\"; then %1$s; return $?; fi\n", descriptor.functionName, count));
buff.append( format(" local cmds%2$d=(%1$s)\n", withoutTopLevelCommand, count));
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/picocli/AutoCompleteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1896,4 +1896,35 @@ public void testIssue1352_SubcommandNameResourceBundle() throws FileNotFoundExce
}
}

@CommandLine.Command(name = "aliases", aliases = {"a"})
static class Issue1388AliasesCommand {}

@CommandLine.Command(name = "aliases-parent", subcommands = {Issue1388AliasesCommand.class})
static class Issue1388AliasesParentCommand {}

@Test
public void testIssue1388_AliasesCommand() throws FileNotFoundException {
File existingScript = new File("aliases-parent_completion");
if (existingScript.exists()) {
assertTrue(existingScript.delete());
}
try {
AutoComplete.main(Issue1388AliasesParentCommand.class.getName());

assertEquals("", systemErrRule.getLog());
assertEquals("", systemOutRule.getLog());

assertTrue("Expected file '" + existingScript.getAbsolutePath() + "' to exist",
existingScript.exists());

Scanner scanner = new Scanner(existingScript);
scanner.useDelimiter("\\Z"); // end of file
String script = scanner.next();
scanner.close();
assertThat(script, containsString("local cmds0=(aliases)"));
assertThat(script, containsString("local cmds1=(a)"));
} finally {
existingScript.delete();
}
}
}

0 comments on commit eb22ff1

Please sign in to comment.