diff --git a/picocli-codegen/src/main/java/picocli/codegen/util/Util.java b/picocli-codegen/src/main/java/picocli/codegen/util/Util.java index 6a38716a..fc60f9ef 100644 --- a/picocli-codegen/src/main/java/picocli/codegen/util/Util.java +++ b/picocli-codegen/src/main/java/picocli/codegen/util/Util.java @@ -7,9 +7,10 @@ import picocli.CommandLine.Model.CommandSpec; import java.io.Closeable; import java.io.IOException; import java.util.ArrayList; +import java.util.IdentityHashMap; import java.util.List; -public class Util { +public final class Util { private Util() {} public static List getCommandSpecs(String factoryClass, Class[] classes) throws Exception { @@ -24,6 +25,19 @@ public class Util { return specs; } + public static List flattenHierarchy(CommandSpec root) { + IdentityHashMap result = new IdentityHashMap(); + addRecursively(root, result); + return new ArrayList(result.keySet()); + } + + private static void addRecursively(CommandSpec command, IdentityHashMap result) { + result.put(command, command); + for (CommandLine sub : command.subcommands().values()) { + addRecursively(sub.getCommandSpec(), result); + } + } + public static void closeSilently(Closeable closeable) { if (closeable != null) { try {