picocli
picocli copied to clipboard
Allow overriding subcommand of superclass in subclass
An example is, say, a FileCommand
class, with a new
subcommand. Then there's a TextFileCommand
that extends FileCommand
. It would be nice for TextFileCommand
to be able to provide its own new
subcommand, as opposed to having to create a subcommand with a new name, like newtxt
.
I think that should already be possible if the new
subcommands are registered programmatically instead of with the annotations. For example:
@Command(subcommands = {FileCommand.class, TextFileCommand.class})
class App {}
@Command(name = "file")
class FileCommand {}
@Command(name = "text")
class TextFileCommand extends FileCommand {}
@Command(name = "new")
class NewFileCommand {}
@Command(name = "new")
class NewTextFileCommand {}
void init() {
// first register the top-level command
// with the file subcommand and the text subcommand
CommandLine app = new CommandLine(new App());
app.getSubcommands().get("file").addSubcommand("new", new NewFileCommand());
app.getSubcommands().get("text").addSubcommand("new", new NewTextFileCommand());
}
Oh thanks I'll try that. Still would be more convenient to be able to do it with annotations though.
Is it possible that the fix for #619 also fixed this issue or is this unrelated?