use of org.broadinstitute.barclay.argparser.CommandLineProgramGroup in project gatk by broadinstitute.
the class Main method printUsage.
private static void printUsage(final Set<Class<?>> classes, final String commandLineName) {
final StringBuilder builder = new StringBuilder();
builder.append(BOLDRED + "USAGE: " + commandLineName + " " + GREEN + "<program name>" + BOLDRED + " [-h]\n\n" + KNRM).append(BOLDRED + "Available Programs:\n" + KNRM);
/** Group CommandLinePrograms by CommandLineProgramGroup **/
final Map<Class<? extends CommandLineProgramGroup>, CommandLineProgramGroup> programGroupClassToProgramGroupInstance = new LinkedHashMap<>();
final Map<CommandLineProgramGroup, List<Class<?>>> programsByGroup = new TreeMap<>(CommandLineProgramGroup.comparator);
final Map<Class<?>, CommandLineProgramProperties> programsToProperty = new LinkedHashMap<>();
for (final Class<?> clazz : classes) {
// Get the command line property for this command line program
final CommandLineProgramProperties property = getProgramProperty(clazz);
if (null == property) {
throw new RuntimeException(String.format("The class '%s' is missing the required CommandLineProgramProperties annotation.", clazz.getSimpleName()));
}
programsToProperty.put(clazz, property);
// Get the command line program group for the command line property
// NB: we want to minimize the number of times we make a new instance, hence programGroupClassToProgramGroupInstance
CommandLineProgramGroup programGroup = programGroupClassToProgramGroupInstance.get(property.programGroup());
if (null == programGroup) {
try {
programGroup = property.programGroup().newInstance();
} catch (final InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
programGroupClassToProgramGroupInstance.put(property.programGroup(), programGroup);
}
List<Class<?>> programs = programsByGroup.get(programGroup);
if (null == programs) {
programsByGroup.put(programGroup, programs = new ArrayList<>());
}
programs.add(clazz);
}
/** Print out the programs in each group **/
for (final Map.Entry<CommandLineProgramGroup, List<Class<?>>> entry : programsByGroup.entrySet()) {
final CommandLineProgramGroup programGroup = entry.getKey();
builder.append(WHITE + "--------------------------------------------------------------------------------------\n" + KNRM);
builder.append(String.format("%s%-48s %-45s%s\n", RED, programGroup.getName() + ":", programGroup.getDescription(), KNRM));
final List<Class<?>> sortedClasses = new ArrayList<>();
sortedClasses.addAll(entry.getValue());
Collections.sort(sortedClasses, new SimpleNameComparator());
for (final Class<?> clazz : sortedClasses) {
final CommandLineProgramProperties property = programsToProperty.get(clazz);
if (null == property) {
throw new RuntimeException(String.format("Unexpected error: did not find the CommandLineProgramProperties annotation for '%s'", clazz.getSimpleName()));
}
if (clazz.getSimpleName().length() >= 45) {
builder.append(String.format("%s %s %s%s%s\n", GREEN, clazz.getSimpleName(), CYAN, property.oneLineSummary(), KNRM));
} else {
builder.append(String.format("%s %-45s%s%s%s\n", GREEN, clazz.getSimpleName(), CYAN, property.oneLineSummary(), KNRM));
}
}
builder.append(String.format("\n"));
}
builder.append(WHITE + "--------------------------------------------------------------------------------------\n" + KNRM);
System.err.println(builder.toString());
}
Aggregations