Skip to content
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

Allow overriding subcommand of superclass in subclass #618

Open
BeeeWall opened this issue Feb 1, 2019 · 3 comments
Open

Allow overriding subcommand of superclass in subclass #618

BeeeWall opened this issue Feb 1, 2019 · 3 comments

Comments

@BeeeWall
Copy link

BeeeWall commented Feb 1, 2019

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.

@remkop
Copy link
Owner

remkop commented Feb 1, 2019

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

@BeeeWall
Copy link
Author

BeeeWall commented Feb 1, 2019

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

@remkop
Copy link
Owner

remkop commented Feb 2, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants