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

Allow aliases of a CommandSpec that is already a subcommand to be properly & consistently modified. #1529

Merged
merged 4 commits into from
Feb 7, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6428,9 +6428,7 @@ public CommandSpec addSubcommand(String name, CommandLine subCommandLine) {
if (subSpec.name == null) { subSpec.name(actualName); }
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(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() + "'"); }
addAlias(alias, actualName, subCommandLine, t);
}
subSpec.initCommandHierarchyWithResourceBundle(resourceBundleBaseName(), resourceBundle());
if (scopeType() == ScopeType.INHERIT) {
Expand All @@ -6451,6 +6449,17 @@ public CommandSpec addSubcommand(String name, CommandLine subCommandLine) {
}
return this;
}
private void addAlias(String alias, String name, CommandLine subCommandLine, Tracer t) {
if (t.isDebug()) {t.debug("Adding alias '%s' for '%s'%n", (parent == null ? "" : parent.qualifiedName() + " ") + alias, qualifiedName());}
CommandLine previous = commands.put(interpolator.interpolate(alias), subCommandLine);
if (previous != null && previous != subCommandLine) {
throw new DuplicateNameException("Alias '" + alias + "' for subcommand '" + name + "' is already used by another subcommand of '" + name() + "'");
}
}
private void removeAlias(String alias, CommandLine subCommandLine, Tracer t) {
if (t.isDebug()) {t.debug("Removing alias '%s' for '%s'%n", (parent == null ? "" : parent.qualifiedName() + " ") + alias, qualifiedName());}
commands.remove(interpolator.interpolate(alias));
}
private void inheritAttributesFrom(CommandSpec root) {
inherited = true;
initFrom(root);
Expand Down Expand Up @@ -7086,7 +7095,21 @@ public String[] version() {
* @return this CommandSpec for method chaining
* @since 3.1 */
public CommandSpec aliases(String... aliases) {
rgoldberg marked this conversation as resolved.
Show resolved Hide resolved
Set<String> previousAliasSet = this.aliases;
this.aliases = new LinkedHashSet<String>(Arrays.asList(aliases == null ? new String[0] : aliases));
if (parent != null) {
//remove & add aliases
Set<String> addedAliasSet = new LinkedHashSet<String>(this.aliases);
addedAliasSet.removeAll(previousAliasSet);
Tracer t = new Tracer();
for (String alias : addedAliasSet) {
parent.addAlias(alias, name, commandLine, t);
}
previousAliasSet.removeAll(this.aliases);
for (String alias : previousAliasSet) {
parent.removeAlias(alias, commandLine, t);
}
}
return this;
}

Expand Down
74 changes: 74 additions & 0 deletions src/test/java/picocli/Issue1528.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package picocli;

import java.util.concurrent.Callable;
import org.junit.Test;
import picocli.CommandLine.Command;
import picocli.CommandLine.Model.CommandSpec;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

public class Issue1528 {

@Command(name = "main", subcommands = SubCommand.class)
static class MainCommand implements Callable<Integer> {
@Override
public Integer call() throws Exception {
return null;
}
}

@Command(name = "sub")
static class SubCommand implements Callable<Integer> {
@Override
public Integer call() throws Exception {
return null;
}
}

@Test
public void testSubCommandAliasModification() {
CommandLine cl = new CommandLine(new MainCommand());
CommandSpec cs = cl.getSubcommands().get("sub").getCommandSpec();

assertEquals(0, cs.aliases().length);
assertEquals(0, cl.execute("sub"));
assertNotEquals(0, cl.execute("alias1"));

cs.aliases("alias1");
assertEquals(1, cs.aliases().length);
assertEquals("alias1", cs.aliases()[0]);
assertEquals(0, cl.execute("sub"));
assertEquals(0, cl.execute("alias1"));

cs.aliases("alias1", "alias2");
assertEquals(2, cs.aliases().length);
assertEquals("alias1", cs.aliases()[0]);
assertEquals("alias2", cs.aliases()[1]);
assertEquals(0, cl.execute("sub"));
assertEquals(0, cl.execute("alias1"));
assertEquals(0, cl.execute("alias2"));

cs.aliases("alias2");
assertEquals(1, cs.aliases().length);
assertEquals("alias2", cs.aliases()[0]);
assertEquals(0, cl.execute("sub"));
assertNotEquals(0, cl.execute("alias1"));
assertEquals(0, cl.execute("alias2"));

cs.aliases("alias3");
assertEquals(1, cs.aliases().length);
assertEquals("alias3", cs.aliases()[0]);
assertEquals(0, cl.execute("sub"));
assertNotEquals(0, cl.execute("alias1"));
assertNotEquals(0, cl.execute("alias2"));
assertEquals(0, cl.execute("alias3"));

cs.aliases(new String[0]);
assertEquals(0, cs.aliases().length);
assertEquals(0, cl.execute("sub"));
assertNotEquals(0, cl.execute("alias1"));
assertNotEquals(0, cl.execute("alias2"));
assertNotEquals(0, cl.execute("alias3"));
}
}