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
Changes from 1 commit
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 @@ -7081,7 +7090,21 @@ public String[] version() {
* @return this CommandSpec for method chaining
* @since 3.1 */
public CommandSpec aliases(String... aliases) {
Set<String> existingAliasSet = this.aliases;
rgoldberg marked this conversation as resolved.
Show resolved Hide resolved
this.aliases = new LinkedHashSet<String>(Arrays.asList(aliases == null ? new String[0] : aliases));
if (parent != null) {
//remove & add aliases
Set<String> newAliasSet = new LinkedHashSet<String>(this.aliases);
rgoldberg marked this conversation as resolved.
Show resolved Hide resolved
newAliasSet.removeAll(existingAliasSet);
Tracer t = new Tracer();
for (String alias : newAliasSet) {
parent.addAlias(alias, name, commandLine, t);
}
existingAliasSet.removeAll(this.aliases);
for (String alias : existingAliasSet) {
parent.removeAlias(alias, commandLine, t);
}
}
return this;
}

Expand Down