-
Notifications
You must be signed in to change notification settings - Fork 418
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
Inherited annotation for commands? #660
Comments
For some reason I completely missed However, picocli already walks the class hierarchy looking for annotations (including To expand on your example: @Command(name = "mycommand", footer = "Top-level footer")
abstract class MyCommand {
abstract String getContext();
}
@Command(name = "foo") // if name is omitted the name of this command becomes "mycommand"
class MyCommandFoo extends MyCommand {
String getContext() { return "foo"; }
public static void main(String[] args) {
new CommandLine(new MyCommandFoo()).usage(System.out);
}
}
@Command(name = "bar", footer = "Sub-sub footer in bar")
class MyCommandBar extends MyCommand {
String getContext() { return "bar"; }
public static void main(String[] args) {
new CommandLine(new MyCommandBar()).usage(System.out);
}
} Now, if I run
While if I run
This proves that not only is the That said, I will check if adding the |
When I remove all the
|
Here's a working example: import picocli.CommandLine;
import picocli.CommandLine.Command;
class Example {
@Command(name = "mycommand", footer = "Top-level footer")
abstract static class CommonCommand {
abstract String getContext();
}
static class FooSubcommand extends CommonCommand {
@Override
String getContext() {
return "foo";
}
}
static class BarSubcommand extends CommonCommand {
@Override
String getContext() {
return "bar";
}
}
@Command(name = "foo", subcommands = {FooSubcommand.class})
static class Foo { }
@Command(name = "bar", subcommands = {BarSubcommand.class})
static class Bar { }
@Command(
name = "top",
subcommands = {
Foo.class,
Bar.class
}
)
static class TopLevel { }
public static void main(String[] args) {
new CommandLine(new TopLevel()).usage(System.out);
}
} |
Thanks for the example, I can see that the InitializationException no longer happens after adding the The existing unit tests also still pass with Thanks for raising this! |
Pushed to master. Thanks for raising this! |
@devinrsmith, I am afraid I will have to revert this change, since it is causing failures in other scenarios. Specifically, since this change, users can no longer have multiple levels of inheritance. See #716 For example: @Command(subcommands = CommandLine.HelpCommand.class)
static abstract class AbstractCommand {
}
static abstract class AbstractCommandSubclass extends AbstractCommand {
}
static class ConcreteCommand extends AbstractCommandSubclass {
}
@Test
public void testNoInitializationException() {
// if Command is @Inherited, the below line throws
// picocli.CommandLine$InitializationException:
// Another subcommand named 'help' already exists for command '<main class>'
new CommandLine(new ConcreteCommand());
} The above test fails when Command is For now, I will simply revert this change. I have not investigated if it is possible to address #716 in a way that preserves the For now, the impact on your application is that subcommands need a |
This was reverted, and the See this test that shows how to make your example work after this change. |
I haven't heard any feedback, so closing this ticket. |
I've got a couple commands that share a lot of the same structure, but differ in defaults only. I'd like to be able to specify an abstract command, with specific implementations inhering the abstract classes
Command
annotation. I think this can be accomplished by addingjava.lang.annotation.Inherited
toCommand
.For example:
Would the Inherited Command annotation be possible?
The text was updated successfully, but these errors were encountered: