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.IdentityHashMap;
import java.util.List;

public final 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 List<CommandSpec> flattenHierarchy(CommandSpec root) {
        IdentityHashMap<CommandSpec, CommandSpec> result = new IdentityHashMap<CommandSpec, CommandSpec>();
        addRecursively(root, result);
        return new ArrayList<CommandSpec>(result.keySet());
    }

    private static void addRecursively(CommandSpec command, IdentityHashMap<CommandSpec, CommandSpec> 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 {
                closeable.close();
            } catch (IOException e) {
            }
        }
    }
}

@@ -1225,24 +1225,76 @@
 %3E result) %7B%0A
+        if (!command.usageMessage().hidden()) %7B%0A    
         resu
@@ -1319,16 +1319,20 @@
 mmand);%0A
+    
         
@@ -1392,32 +1392,36 @@
 ) %7B%0A            
+    
 addRecursively(s
@@ -1442,32 +1442,46 @@
 pec(), result);%0A
+            %7D%0A
         %7D%0A    %7D%0A

exclude hidden commands