Skip to content

Commit

Permalink
[#1602] Fix incorrect debug output for add/removeAlias
Browse files Browse the repository at this point in the history
Closes #1602
  • Loading branch information
remkop committed Feb 27, 2022
1 parent e566fe3 commit a3c5fcc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Picocli 4.7.0 introduced a `sortSynopsis = false` attribute to let the synopsis
* [#964][#1080] Enhancement: ArgGroup synopsis should respect `order` (if specified). Thanks to [Enderaoe](https://github.com/Lyther) for the pull request with unit tests.
* [#899][#1578][#1579] Enhancement: improve built-in `Help` command description. Thanks to [Michael L Heuer](https://github.com/heuermh) for the pull request. Thanks to [Garret Wilson](https://github.com/garretwilson) for raising this.
* [#1572] Enhancement: Remove redundant braces in ArgGroup synopsis.
* [#1602] Enhancement: Fix incorrect debug output for add/removeAlias.
* [#1575] Bugfix: Synopsis should not cluster boolean options if `posixClusteredShortOptionsAllowed` is set to false.
* [#812] DOC: Document how to test a picocli spring-boot application.
* [#1596] DOC: fix javadoc typos and incorrect links.
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6558,14 +6558,16 @@ 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'", (parent == null ? "" : parent.qualifiedName() + " ") + alias, qualifiedName());}
CommandSpec subSpec = subCommandLine.getCommandSpec();
if (t.isDebug()) {t.debug("Adding alias '%s' for '%s'", (subSpec.parent() == null ? "" : subSpec.parent().qualifiedName() + " ") + alias, subSpec.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'", (parent == null ? "" : parent.qualifiedName() + " ") + alias, qualifiedName());}
CommandSpec subSpec = subCommandLine.getCommandSpec();
if (t.isDebug()) {t.debug("Removing alias '%s' for '%s'", (subSpec.parent() == null ? "" : subSpec.parent().qualifiedName() + " ") + alias, subSpec.qualifiedName());}
commands.remove(interpolator.interpolate(alias));
}
private void inheritAttributesFrom(CommandSpec root) {
Expand Down
24 changes: 22 additions & 2 deletions src/test/java/picocli/SubcommandTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class SubcommandTests {
@Rule
public final ProvideSystemProperty ansiOFF = new ProvideSystemProperty("picocli.ansi", "false");

@Command(name = "top")
static class MainCommand { @Option(names = "-a") boolean a; public boolean equals(Object o) { return getClass().equals(o.getClass()); }}
static class ChildCommand1 { @Option(names = "-b") boolean b; public boolean equals(Object o) { return getClass().equals(o.getClass()); }}
static class ChildCommand2 { @Option(names = "-c") boolean c; public boolean equals(Object o) { return getClass().equals(o.getClass()); }}
Expand All @@ -85,7 +86,7 @@ public void testAddSubcommandWithoutNameRequiresAnnotationName() {
cmd.addSubcommand(new ChildCommand1());
fail("Expected exception");
} catch (InitializationException ex) {
assertEquals("Cannot add subcommand with null name to <main class>", ex.getMessage());
assertEquals("Cannot add subcommand with null name to top", ex.getMessage());
}
}

Expand Down Expand Up @@ -302,10 +303,29 @@ public void testRemoveSubcommandByAliasAbbreviatedCaseInsensitive() {
assertEquals(0, cmd.getCommandSpec().subcommands().size());
assertSame(sub, removed);

String line = String.format("[picocli DEBUG] Removed 4 subcommand entries [alias1, alias2, bobobo, main] for key 'BO' from '<main class>'%n");
String line = String.format("[picocli DEBUG] Removed 4 subcommand entries [alias1, alias2, bobobo, main] for key 'BO' from 'top'%n");
assertEquals(line, systemErrRule.getLog());
}

@Test
public void testAddSubcommandAliasTrace() {
TestUtil.setTraceLevel("DEBUG");
MainCommand top = new MainCommand();
CommandLine cmd = new CommandLine(top);
SubcommandWithAliases sub = new SubcommandWithAliases();
cmd.addSubcommand("sub", sub);

String expected = String.format("" +
"[picocli DEBUG] Creating CommandSpec for picocli.SubcommandTests$MainCommand@%s with factory picocli.CommandLine$DefaultFactory%n" +
"[picocli DEBUG] Creating CommandSpec for picocli.SubcommandTests$SubcommandWithAliases@%s with factory picocli.CommandLine$DefaultFactory%n" +
"[picocli DEBUG] Adding subcommand 'sub' to 'top'%n" +
"[picocli DEBUG] Adding alias 'top alias1' for 'top sub'%n" +
"[picocli DEBUG] Adding alias 'top alias2' for 'top sub'%n" +
"[picocli DEBUG] Adding alias 'top bobobo' for 'top sub'%n",
Integer.toHexString(top.hashCode()), Integer.toHexString(sub.hashCode()));
assertEquals(expected, systemErrRule.getLog());
}

private static CommandLine createNestedCommand() {
CommandLine commandLine = new CommandLine(new MainCommand());
commandLine
Expand Down

0 comments on commit a3c5fcc

Please sign in to comment.