From 3d00695c14b6e179e443b2b8b48d05279d564a0f Mon Sep 17 00:00:00 2001 From: Remko Popma Date: Tue, 14 Aug 2018 12:16:48 +0900 Subject: [PATCH] [#443] Bugfix: Subcommand aliases were not recognized in some cases. Closes #443 --- RELEASE-NOTES.md | 34 ++++++++++++++++++++++ src/main/java/picocli/CommandLine.java | 4 ++- src/test/java/picocli/CommandLineTest.java | 27 +++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index c54589337..cc2e166ae 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -1,5 +1,39 @@ # picocli Release Notes +# Picocli 3.5.2 +The picocli community is pleased to announce picocli 3.5.2. + +This is a bugfix release that fixes an issue where subcommand aliases were not recognized in some cases. + +This is the thirty-eighth public release. +Picocli follows [semantic versioning](http://semver.org/). + +## Table of Contents +* [New and noteworthy](#3.5.2-new) +* [Promoted features](#3.5.2-promoted) +* [Fixed issues](#3.5.2-fixes) +* [Deprecations](#3.5.2-deprecated) +* [Potential breaking changes](#3.5.2-breaking-changes) + +## New and Noteworthy + + +## Promoted Features +Promoted features are features that were incubating in previous versions of picocli but are now supported and subject to backwards compatibility. + +No features have been promoted in this picocli release. + +## Fixed issues +- [#443] Bugfix: Subcommand aliases were not recognized in some cases. Thanks to [K. Alex Mills](https://github.com/kalexmills) for the bug report. + +## Deprecations +No features were deprecated in this release. + +## Potential breaking changes +This release has no breaking changes. + + + # Picocli 3.5.1 The picocli community is pleased to announce picocli 3.5.1. diff --git a/src/main/java/picocli/CommandLine.java b/src/main/java/picocli/CommandLine.java index 010de4b0d..295654372 100644 --- a/src/main/java/picocli/CommandLine.java +++ b/src/main/java/picocli/CommandLine.java @@ -241,7 +241,9 @@ public CommandLine addSubcommand(String name, Object command) { */ public CommandLine addSubcommand(String name, Object command, String... aliases) { CommandLine subcommandLine = toCommandLine(command, factory); - subcommandLine.getCommandSpec().aliases(aliases); + List update = new ArrayList(Arrays.asList(subcommandLine.getCommandSpec().aliases())); + update.addAll(Arrays.asList(aliases)); + subcommandLine.getCommandSpec().aliases(update.toArray(new String[0])); getCommandSpec().addSubcommand(name, subcommandLine); CommandLine.Model.CommandReflection.initParentCommand(subcommandLine.getCommandSpec().userObject(), getCommandSpec().userObject()); return this; diff --git a/src/test/java/picocli/CommandLineTest.java b/src/test/java/picocli/CommandLineTest.java index f2872407b..4b1f38bcb 100644 --- a/src/test/java/picocli/CommandLineTest.java +++ b/src/test/java/picocli/CommandLineTest.java @@ -2256,6 +2256,33 @@ public void testCommandListReturnsAliases() { assertSame(subMap.get("sub21"), subMap.get("sub21alias2")); } + @Command(name = "cb") + static class Issue443TopLevelCommand implements Runnable { + boolean topWasExecuted; + public void run() { + topWasExecuted = true; + } + } + + @Command(name = "task", aliases = {"t"}) + static class SubCommandWithAlias implements Runnable { + boolean subWasExecuted; + public void run() { + subWasExecuted = true; + } + } + + @Test + public void testIssue443SubcommandWithAliasAnnotation() { + Issue443TopLevelCommand top = new Issue443TopLevelCommand(); + SubCommandWithAlias sub = new SubCommandWithAlias(); + CommandLine cmd = new CommandLine(top).addSubcommand("task", sub); + String[] args = {"t"}; + List result = cmd.parseWithHandler(new RunAll(), args); + assertTrue("top was executed", top.topWasExecuted); + assertTrue("sub was executed", sub.subWasExecuted); + } + public static Set setOf(T... elements) { Set result = new HashSet(); for (T t : elements) { result.add(t); }