Example usage for org.apache.commons.cli Options getRequiredOptions

List of usage examples for org.apache.commons.cli Options getRequiredOptions

Introduction

In this page you can find the example usage for org.apache.commons.cli Options getRequiredOptions.

Prototype

public List getRequiredOptions() 

Source Link

Document

Returns the required options.

Usage

From source file:gpframework.RunExperiment.java

/**
 * Application's entry point./*from  w  w w. j a v a 2  s  . c  o m*/
 * 
 * @param args
 * @throws ParseException
 * @throws ParameterException 
 */
public static void main(String[] args) throws ParseException, ParameterException {
    // Failsafe parameters
    if (args.length == 0) {
        args = new String[] { "-f", "LasSortednessFunction", "-n", "5", "-ff", "JoinFactory", "-tf",
                "SortingElementFactory", "-pf", "SortingProgramFactory", "-s", "SMOGPSelection", "-a", "SMOGP",
                "-t", "50", "-e", "1000000000", "-mf", "SingleMutationFactory", "-d", "-bn", "other" };
    }

    // Create options
    Options options = new Options();
    setupOptions(options);

    // Read options from the command line
    CommandLineParser parser = new PosixParser();
    CommandLine cmd;

    // Print help if parameter requirements are not met
    try {
        cmd = parser.parse(options, args);
    }

    // If some parameters are missing, print help
    catch (MissingOptionException e) {
        HelpFormatter hf = new HelpFormatter();
        hf.printHelp("java -jar GPFramework \n", options);
        System.out.println();
        System.out.println("Missing parameters: " + e.getMissingOptions());
        return;
    }

    // Re-initialize PRNG
    long seed = System.currentTimeMillis();
    Utils.random = new Random(seed);

    // Set the problem size
    int problemSize = Integer.parseInt(cmd.getOptionValue("n"));

    // Set debug mode and cluster mode
    Utils.debug = cmd.hasOption("d");
    RunExperiment.cluster = cmd.hasOption("c");

    // Initialize fitness function and some factories
    FitnessFunction fitnessFunction = fromName(cmd.getOptionValue("f"), problemSize);
    MutationFactory mutationFactory = fromName(cmd.getOptionValue("mf"));
    Selection selectionCriterion = fromName(cmd.getOptionValue("s"));
    FunctionFactory functionFactory = fromName(cmd.getOptionValue("ff"));
    TerminalFactory terminalFactory = fromName(cmd.getOptionValue("tf"), problemSize);
    ProgramFactory programFactory = fromName(cmd.getOptionValue("pf"), functionFactory, terminalFactory);

    // Initialize algorithm
    Algorithm algorithm = fromName(cmd.getOptionValue("a"), mutationFactory, selectionCriterion);
    algorithm.setParameter("evaluationsBudget", cmd.getOptionValue("e"));
    algorithm.setParameter("timeBudget", cmd.getOptionValue("t"));

    // Initialize problem
    Problem problem = new Problem(programFactory, fitnessFunction);
    Program solution = algorithm.solve(problem);

    Utils.debug("Population results: ");
    Utils.debug(algorithm.getPopulation().toString());
    Utils.debug(algorithm.getPopulation().parse());

    Map<String, Object> entry = new HashMap<String, Object>();

    // Copy algorithm setup
    for (Object o : options.getRequiredOptions()) {
        Option option = options.getOption(o.toString());
        entry.put(option.getLongOpt(), cmd.getOptionValue(option.getOpt()));
    }
    entry.put("seed", seed);

    // Copy results
    entry.put("bestProgram", solution.toString());
    entry.put("bestSolution", fitnessFunction.normalize(solution));

    // Copy all statistics
    entry.putAll(algorithm.getStatistics());

    Utils.debug("Maximum encountered population size: "
            + algorithm.getStatistics().get("maxPopulationSizeToCompleteFront"));
    Utils.debug("Maximum encountered tree size: "
            + algorithm.getStatistics().get("maxProgramComplexityToCompleteFront"));
    Utils.debug("Solution complexity: " + solution.complexity() + "/" + (2 * problemSize - 1));
}

From source file:de.yaio.commons.cli.CmdLineHelper.java

/**
 * <h1>Bereich:</h1>/*from w  ww  . ja  v  a2s. c  o  m*/
 *     Tools - CLI-Config
 * <h1>Funktionalitaet:</h1>
 *     konfiguriert die verfuegbaren CLI-Optionen
 * <h1>Nebenwirkungen:</h1>
 *     Rueckgabe als Options
 * @param newAvailiableCmdLineOptions cmd-options to add to availiableCmdLineOptions 
 */
public void addAvailiableCmdLineOptions(final Options newAvailiableCmdLineOptions) {
    if (commandLine != null) {
        throw new IllegalStateException("addAvailiableCmdLineOptions: " + "cant add availiableCmdLineOptions "
                + "because commandLine already set");
    }

    if (newAvailiableCmdLineOptions != null) {
        // add new Options
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("addAvailiableCmdLineOptions: " + "add commandLineOptions:"
                    + newAvailiableCmdLineOptions.getOptions().size());
            LOGGER.debug("addAvailiableCmdLineOptions: " + "add commandLineOptions:"
                    + newAvailiableCmdLineOptions.getOptions());
            LOGGER.debug("addAvailiableCmdLineOptions: " + "add commandLineOptions:"
                    + newAvailiableCmdLineOptions.getRequiredOptions());
            LOGGER.debug("addAvailiableCmdLineOptions: " + " details:" + newAvailiableCmdLineOptions);
        }
        for (Object newOption : newAvailiableCmdLineOptions.getOptions()) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("addAvailiableCmdLineOptions: " + "add commandLineOption: " + (Option) newOption);
            }
            availiableCmdLineOptions.addOption((Option) newOption);
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("done addAvailiableCmdLineOptions: " + "with commandLineOptions:"
                    + newAvailiableCmdLineOptions);
        }
    }

}

From source file:de.bmw.yamaica.common.console.CommandExecuter.java

private List<ConsoleConfiguration> getParsedExtensionPointConfigurations() {
    List<ConsoleConfiguration> configurations = new ArrayList<ConsoleConfiguration>();

    // Parse extension point information
    IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
    IExtensionPoint commandsExtensionPoint = extensionRegistry.getExtensionPoint(COMMANDS_EXTENSION_POINT_ID);

    if (null != commandsExtensionPoint) {
        Map<String, IConfigurationElement> referenceableOptionConfigurations = parseExtensionPoint(
                extensionRegistry.getExtensionPoint(OPTIONS_EXTENSION_POINT_ID), OPTION_ID_ATTRIBUTE_NAME);
        Map<String, IConfigurationElement> referenceableOptionGroupConfigurations = parseExtensionPoint(
                extensionRegistry.getExtensionPoint(OPTION_GROUPS_EXTENSION_POINT_ID),
                OPTION_GROUP_ID_ATTRIBUTE_NAME);

        for (IConfigurationElement commandConfiguration : commandsExtensionPoint.getConfigurationElements()) {
            String id = commandConfiguration.getAttribute(COMMAND_ID_ATTRIBUTE_NAME);

            try {
                Options options = new Options();

                String name = commandConfiguration.getAttribute(COMMAND_NAME_ATTRIBUTE_NAME);
                String syntax = commandConfiguration.getAttribute(COMMAND_SYNTAX_ATTRIBUTE_NAME);
                String header = null;
                String footer = null;

                for (IConfigurationElement childConfiguration : commandConfiguration.getChildren()) {
                    switch (childConfiguration.getName()) {
                    case OPTIONS_CONFIGURATION_NAME:
                        for (IConfigurationElement optionConfiguration : childConfiguration.getChildren()) {
                            Option option = getOptionByConfiguration(optionConfiguration,
                                    referenceableOptionConfigurations);
                            OptionGroup optionGroup = getOptionGroupByConfiguration(optionConfiguration,
                                    referenceableOptionGroupConfigurations, referenceableOptionConfigurations);

                            if (null != option) {
                                options.addOption(option);
                            }//from   w  w w. ja v  a2  s  . co m

                            if (null != optionGroup) {
                                options.addOptionGroup(optionGroup);
                            }
                        }
                        break;

                    case HEADER_CONFIGURATION_NAME:
                        header = childConfiguration.getValue();
                        break;

                    case FOOTER_CONFIGURATION_NAME:
                        footer = childConfiguration.getValue();
                        break;
                    }
                }

                if (options.getRequiredOptions().size() < 1) {
                    throw new IllegalArgumentException(String.format(NO_REQUIRED_OPTION_MESSAGE));
                }

                configurations.add(new ConsoleConfiguration(commandConfiguration, options, id, name, syntax,
                        header, footer));
            } catch (Exception exception) {
                String message = String
                        .format(EXTENSION_POINT_PARSING_ERROR_MESSAGE + " " + exception.getMessage(), id);

                log(new IllegalArgumentException(message, exception));
            }
        }
    }

    return configurations;
}

From source file:org.apache.openmeetings.cli.OmHelpFormatter.java

@SuppressWarnings("unchecked")
private static List<OmOption> getReqOptions(Options opts) {
    //suppose we have only 1 group (for now)
    OptionGroup g = ((List<OptionGroup>) opts.getRequiredOptions()).get(0);
    List<OmOption> result = new ArrayList<OmOption>();
    for (Option o : g.getOptions()) {
        result.add((OmOption) o);/*from  w ww  .  j  a  v  a  2s . co  m*/
    }
    Collections.sort(result, new Comparator<OmOption>() {
        @Override
        public int compare(OmOption o1, OmOption o2) {
            return o1.getOrder() - o2.getOrder();
        }
    });
    return result;
}

From source file:org.nuunframework.cli.NuunCliPluginTest.java

@Test
public void testOptions() {
    Kernel kernel = nuunCliService.getKernel();
    Options globalOptions = kernel.getMainInjector().getInstance(Options.class);
    Map<Class<?>, Options> optionsMap = kernel.getMainInjector()
            .getInstance(Key.get(new TypeLiteral<Map<Class<?>, Options>>() {
            }));/*from   w w w  . j a v a  2  s  .  c o m*/
    Options holdersSpecificsOptions = optionsMap.get(Holder.class);

    assertThat(globalOptions).isNotNull();
    assertThat(holdersSpecificsOptions.getOptions().size()).isEqualTo(5);
    assertThat(globalOptions.getOptions().size()).isGreaterThan(5);

    assertThat(holdersSpecificsOptions.getOption("o1")).isNotNull();
    assertThat(holdersSpecificsOptions.getOption("o1").getDescription())
            .isEqualTo("the long description of opt number 1");
    assertThat(holdersSpecificsOptions.getOption("o1").getLongOpt()).isEqualTo("option1");
    assertThat(holdersSpecificsOptions.getOption("o1").isRequired()).isFalse();
    assertThat(holdersSpecificsOptions.getOption("o2")).isNotNull();

    assertThat(holdersSpecificsOptions.getOption("o2").getDescription())
            .isEqualTo("the long description of opt number 2");
    assertThat(holdersSpecificsOptions.getOption("o2").getLongOpt()).isEqualTo("option2");
    assertThat(holdersSpecificsOptions.getOption("o2").isRequired()).isTrue();

    assertThat(holdersSpecificsOptions.getRequiredOptions().size()).isEqualTo(4);
}

From source file:org.openmeetings.cli.OmHelpFormatter.java

@SuppressWarnings("unchecked")
private List<OmOption> getReqOptions(Options opts) {
    //suppose we have only 1 group (for now)
    OptionGroup g = ((List<OptionGroup>) opts.getRequiredOptions()).get(0);
    List<OmOption> result = new ArrayList<OmOption>(g.getOptions());
    Collections.sort(result, new Comparator<OmOption>() {
        public int compare(OmOption o1, OmOption o2) {
            return o1.getOrder() - o2.getOrder();
        }//from  w ww .j  a  v  a 2s  .c  o m
    });
    return result;
}