[#1066] add Util::flattenHierarchy helper method for using ManPageGenerator as a subcommand

This commit is contained in:
Remko Popma
2020-06-06 08:06:53 +09:00
parent 79b74ffb1a
commit c51ddf924b

View File

@@ -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<CommandSpec> getCommandSpecs(String factoryClass, Class<?>[] classes) throws Exception {
@@ -24,6 +25,19 @@ public class Util {
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 {