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

[#1388] Fix subcommand aliases autocomplete regression #1430

Merged
merged 3 commits into from
Oct 31, 2021
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
3 changes: 2 additions & 1 deletion src/main/java/picocli/AutoComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,8 @@ 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,
full.length() - spec.name().length()) + 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
4 changes: 2 additions & 2 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6412,7 +6412,7 @@ public CommandSpec addSubcommand(String name, CommandSpec subcommand) {
*/
public CommandSpec addSubcommand(String name, CommandLine subCommandLine) {
CommandSpec subSpec = subCommandLine.getCommandSpec();
String actualName = validateSubcommandName(name, subSpec);
String actualName = validateSubcommandName(interpolator.interpolateCommandName(name), subSpec);
Tracer t = new Tracer();
if (t.isDebug()) {t.debug("Adding subcommand '%s' to '%s'%n", actualName, this.qualifiedName());}
String previousName = commands.getCaseSensitiveKey(actualName);
Expand All @@ -6422,7 +6422,7 @@ public CommandSpec addSubcommand(String name, CommandLine subCommandLine) {
subSpec.parent(this);
for (String alias : subSpec.aliases()) {
if (t.isDebug()) {t.debug("Adding alias '%s' for '%s'%n", (parent == null ? "" : parent.qualifiedName() + " ") + alias, this.qualifiedName());}
previous = commands.put(alias, subCommandLine);
previous = commands.put(interpolator.interpolate(alias), subCommandLine);
if (previous != null && previous != subCommandLine) { throw new DuplicateNameException("Alias '" + alias + "' for subcommand '" + actualName + "' is already used by another subcommand of '" + this.name() + "'"); }
}
subSpec.initCommandHierarchyWithResourceBundle(resourceBundleBaseName(), resourceBundle());
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();
}
}
}