Search in sources :

Example 1 with CommandLineProgramProperties

use of org.broadinstitute.barclay.argparser.CommandLineProgramProperties in project gatk by broadinstitute.

the class Main method extractCommandLineProgram.

/**
     * Returns the command line program specified, or prints the usage and exits with exit code 1 *
     */
private static CommandLineProgram extractCommandLineProgram(final String[] args, final List<String> packageList, final List<Class<? extends CommandLineProgram>> classList, final String commandLineName) {
    /** Get the set of classes that are our command line programs **/
    final ClassFinder classFinder = new ClassFinder();
    for (final String pkg : packageList) {
        classFinder.find(pkg, CommandLineProgram.class);
    }
    String missingAnnotationClasses = "";
    final Set<Class<?>> toCheck = classFinder.getClasses();
    toCheck.addAll(classList);
    final Map<String, Class<?>> simpleNameToClass = new LinkedHashMap<>();
    for (final Class<?> clazz : toCheck) {
        // No interfaces, synthetic, primitive, local, or abstract classes.
        if (ClassUtils.canMakeInstances(clazz)) {
            final CommandLineProgramProperties property = getProgramProperty(clazz);
            // Check for missing annotations
            if (null == property) {
                if (missingAnnotationClasses.isEmpty())
                    missingAnnotationClasses += clazz.getSimpleName();
                else
                    missingAnnotationClasses += ", " + clazz.getSimpleName();
            } else if (!property.omitFromCommandLine()) {
                /** We should check for missing annotations later **/
                if (simpleNameToClass.containsKey(clazz.getSimpleName())) {
                    throw new RuntimeException("Simple class name collision: " + clazz.getSimpleName());
                }
                simpleNameToClass.put(clazz.getSimpleName(), clazz);
            }
        }
    }
    if (!missingAnnotationClasses.isEmpty()) {
        throw new RuntimeException("The following classes are missing the required CommandLineProgramProperties annotation: " + missingAnnotationClasses);
    }
    final Set<Class<?>> classes = new LinkedHashSet<>();
    classes.addAll(simpleNameToClass.values());
    if (args.length < 1) {
        printUsage(classes, commandLineName);
    } else {
        if (args[0].equals("-h") || args[0].equals("--help")) {
            printUsage(classes, commandLineName);
        } else {
            if (simpleNameToClass.containsKey(args[0])) {
                final Class<?> clazz = simpleNameToClass.get(args[0]);
                try {
                    return (CommandLineProgram) clazz.newInstance();
                } catch (final InstantiationException | IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
            printUsage(classes, commandLineName);
            throw new UserException(getUnknownCommandMessage(classes, args[0]));
        }
    }
    return null;
}
Also used : CommandLineProgram(org.broadinstitute.hellbender.cmdline.CommandLineProgram) CommandLineProgramProperties(org.broadinstitute.barclay.argparser.CommandLineProgramProperties) ClassFinder(org.broadinstitute.hellbender.cmdline.ClassFinder) UserException(org.broadinstitute.hellbender.exceptions.UserException)

Example 2 with CommandLineProgramProperties

use of org.broadinstitute.barclay.argparser.CommandLineProgramProperties 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());
}
Also used : CommandLineProgramProperties(org.broadinstitute.barclay.argparser.CommandLineProgramProperties) CommandLineProgramGroup(org.broadinstitute.barclay.argparser.CommandLineProgramGroup)

Aggregations

CommandLineProgramProperties (org.broadinstitute.barclay.argparser.CommandLineProgramProperties)2 CommandLineProgramGroup (org.broadinstitute.barclay.argparser.CommandLineProgramGroup)1 ClassFinder (org.broadinstitute.hellbender.cmdline.ClassFinder)1 CommandLineProgram (org.broadinstitute.hellbender.cmdline.CommandLineProgram)1 UserException (org.broadinstitute.hellbender.exceptions.UserException)1