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

Command methods: picking from interfaces in addition to superclasses. #2245

Open
vrdhn opened this issue Apr 14, 2024 · 2 comments
Open

Command methods: picking from interfaces in addition to superclasses. #2245

vrdhn opened this issue Apr 14, 2024 · 2 comments

Comments

@vrdhn
Copy link

vrdhn commented Apr 14, 2024

Very specific use case, perhaps:

  • lots of top level sub-commands
  • implemented as command methods.
  • not wanting to put all of them in one java file.

Right now, the only way is chain of inheritance, and give root
of chain to CommandLine.

Another way is to create default methods in interfaces, and make
top Command implement all these interfaces.

I tried simple change, will this work ?

--- a/src/main/java/picocli/CommandLine.java
+++ b/src/main/java/picocli/CommandLine.java
@@ -11828,6 +11828,9 @@ public class CommandLine {
                     Stack<Class<?>> hierarchy = new Stack<Class<?>>();
                     while (cls != null) {
                         hierarchy.add(cls);
+                        for ( Class<?> c : cls.getInterfaces()) {
+                            hierarchy.add(c);
+                        }
                         cls = cls.getSuperclass();
                     }
                     Set<Class<?>> fullHierarchySet = new HashSet<Class<?>>(hierarchy);
@remkop
Copy link
Owner

remkop commented Apr 15, 2024

Hi @vrdhn, my time to work on picocli is extremely limited these days. Mostly just bugfixes.
However, I can discuss ideas for enhancements and review pull requests.

I suggest that you clone the picocli repository, and experiment until it becomes clear which changes are necessary to fulfil your use case. When you are ready, you can raise a pull request to include those changes into the picocli library. A good pull request passes all existing tests (we cannot break any existing applications), and also should add a test for your use case - a test that fails with the current version of picocli and passes with your changes. Of course, make sure that you are happy with your solution, so that it solves your issue completely.

Note that this may be quite a lot of work. If you already have a workaround, I am not sure if it is worth your time working on a pull request for also supporting interfaces.

@remkop
Copy link
Owner

remkop commented May 1, 2024

One more thing: we cannot use default methods on interfaces, since picocli requires only Java 5 (see reasons why), and default methods were introduced in java 8.

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