picocli icon indicating copy to clipboard operation
picocli copied to clipboard

Command methods: picking from interfaces in addition to superclasses.

Open vrdhn opened this issue 10 months ago • 2 comments

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

vrdhn avatar Apr 14 '24 15:04 vrdhn

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 avatar Apr 15 '24 02:04 remkop

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.

remkop avatar May 01 '24 05:05 remkop