remkop / picocli

Picocli is a modern framework for building powerful, user-friendly, GraalVM-enabled command line apps with ease. It supports colors, autocompletion, subcommands, and more. In 1 source file so apps can include as source & avoid adding a dependency. Written in Java, usable from Groovy, Kotlin, Scala, etc.
https://picocli.info
Apache License 2.0
4.89k stars 422 forks source link

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

Open vrdhn opened 5 months ago

vrdhn commented 5 months ago

Very specific use case, perhaps:

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 commented 5 months ago

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 commented 5 months ago

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.