package picocli.codegen.util;

import picocli.CommandLine;
import picocli.CommandLine.IFactory;
import picocli.CommandLine.Model.CommandSpec;

import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Util {
    private Util() {}

    public static List<CommandSpec> getCommandSpecs(String factoryClass, Class<?>[] classes) throws Exception {
        IFactory factory = CommandLine.defaultFactory();
        if (factoryClass != null) {
            factory = (IFactory) factory.create(Class.forName(factoryClass));
        }
        List<CommandSpec> specs = new ArrayList<CommandSpec>();
        for (Class<?> cls : classes) {
            specs.add(new CommandLine(cls, factory).getCommandSpec());
        }
        return specs;
    }

    public static void closeSilently(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
            }
        }
    }
}

@@ -218,16 +218,50 @@
 ayList;%0A
+import java.util.IdentityHashMap;%0A
 import j
@@ -283,16 +283,22 @@
 %0Apublic 
+final 
 class Ut
@@ -831,16 +831,592 @@
 %0A    %7D%0A%0A
+    public static List%3CCommandSpec%3E flattenHierarchy(CommandSpec root) %7B%0A        IdentityHashMap%3CCommandSpec, CommandSpec%3E result = new IdentityHashMap%3CCommandSpec, CommandSpec%3E();%0A        addRecursively(root, result);%0A        return new ArrayList%3CCommandSpec%3E(result.keySet());%0A    %7D%0A%0A    private static void addRecursively(CommandSpec command, IdentityHashMap%3CCommandSpec, CommandSpec%3E result) %7B%0A        result.put(command, command);%0A        for (CommandLine sub : command.subcommands().values()) %7B%0A            addRecursively(sub.getCommandSpec(), result);%0A        %7D%0A    %7D%0A%0A
     publ

add `Util::flattenHierarchy` helper method for using `ManPageGenerator` as a subcommand