picocli icon indicating copy to clipboard operation
picocli copied to clipboard

Allow overriding subcommand of superclass in subclass

Open BeeeWall opened this issue 6 years ago • 3 comments

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.

BeeeWall avatar Feb 01 '19 15:02 BeeeWall

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());
}

remkop avatar Feb 01 '19 16:02 remkop

Oh thanks I'll try that. Still would be more convenient to be able to do it with annotations though.

BeeeWall avatar Feb 01 '19 16:02 BeeeWall

Is it possible that the fix for #619 also fixed this issue or is this unrelated?

remkop avatar Feb 02 '19 11:02 remkop