Search in sources :

Example 31 with PosixParser

use of org.apache.commons.cli.PosixParser in project compiler by boalang.

the class BoaMain method main.

public static void main(final String[] args) throws IOException {
    final Options options = new Options();
    options.addOption("p", "parse", false, "parse and semantic check a Boa program (don't generate code)");
    options.addOption("c", "compile", false, "compile a Boa program");
    options.addOption("e", "execute", false, "execute a Boa program locally");
    options.addOption("g", "generate", false, "generate a Boa dataset");
    try {
        if (args.length == 0) {
            printHelp(options, null);
            return;
        } else {
            final CommandLine cl = new PosixParser().parse(options, new String[] { args[0] });
            final String[] tempargs = new String[args.length - 1];
            System.arraycopy(args, 1, tempargs, 0, args.length - 1);
            if (cl.hasOption("c")) {
                boa.compiler.BoaCompiler.main(tempargs);
            } else if (cl.hasOption("p")) {
                boa.compiler.BoaCompiler.parseOnly(tempargs);
            } else if (cl.hasOption("e")) {
                boa.evaluator.BoaEvaluator.main(tempargs);
            } else if (cl.hasOption("g")) {
                boa.datagen.BoaGenerator.main(tempargs);
            }
        }
    } catch (final org.apache.commons.cli.ParseException e) {
        printHelp(options, e.getMessage());
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser)

Example 32 with PosixParser

use of org.apache.commons.cli.PosixParser in project opennms by OpenNMS.

the class SystemReport method main.

/**
 * @param args
 */
public static void main(final String[] args) throws Exception {
    final String tempdir = System.getProperty("java.io.tmpdir");
    // pull out -D defines first
    for (final String arg : args) {
        if (arg.startsWith("-D") && arg.contains("=")) {
            final Matcher m = m_pattern.matcher(arg);
            if (m.matches()) {
                System.setProperty(m.group(1), m.group(2));
            }
        }
    }
    if (System.getProperty("opennms.home") == null) {
        System.setProperty("opennms.home", tempdir);
    }
    if (System.getProperty("rrd.base.dir") == null) {
        System.setProperty("rrd.base.dir", tempdir);
    }
    if (System.getProperty("rrd.binary") == null) {
        System.setProperty("rrd.binary", "/usr/bin/rrdtool");
    }
    final CommandLineParser parser = new PosixParser();
    final Options options = new Options();
    options.addOption("h", "help", false, "this help");
    options.addOption("D", "define", true, "define a java property");
    options.addOption("p", "list-plugins", false, "list the available system report plugins");
    options.addOption("u", "use-plugins", true, "select the plugins to output");
    options.addOption("l", "list-formats", false, "list the available output formats");
    options.addOption("f", "format", true, "the format to output");
    options.addOption("o", "output", true, "the file to write output to");
    final CommandLine line = parser.parse(options, args, false);
    final Set<String> plugins = new LinkedHashSet<>();
    final SystemReport report = new SystemReport();
    // help
    if (line.hasOption("h")) {
        final HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("system-report.sh [options]", options);
        System.exit(0);
    }
    // format and output file
    if (line.hasOption("f")) {
        report.setFormat(line.getOptionValue("f"));
    }
    if (line.hasOption("o")) {
        report.setOutput(line.getOptionValue("o"));
    }
    if (line.hasOption("u")) {
        final String value = line.getOptionValue("u");
        if (value != null) {
            for (final String s : value.split(",+")) {
                plugins.add(s);
            }
        }
    }
    // final command
    if (line.hasOption("p")) {
        report.listPlugins();
    } else if (line.hasOption("l")) {
        report.listFormats();
    } else {
        report.writePluginData(plugins);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) Matcher(java.util.regex.Matcher) PosixParser(org.apache.commons.cli.PosixParser) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Example 33 with PosixParser

use of org.apache.commons.cli.PosixParser in project opennms by OpenNMS.

the class SpikeHunter method parseCmdLine.

public static void parseCmdLine(String[] argv) throws Exception {
    m_options.addOption("h", "help", false, "This help text");
    m_options.addOption("f", "file", true, "JRobin disk file on which to operate");
    m_options.addOption("d", "ds-name", true, "Data source names on which to operate, comma-separated. If unspecified, operate on all DSes.");
    m_options.addOption("a", "analysis-strategy", true, "Data analysis strategy. Defaults to percentile.");
    m_options.addOption("o", "operands", true, "Operands (numeric, comma-separated) for the selected analysis strategy. Defaults to 95,5.");
    m_options.addOption("r", "replacement-strategy", true, "Strategy for replacing spike samples, one of nan|previous|next, defaults to nan");
    m_options.addOption("n", "dry-run", false, "Just report spikes, do not make any changes to the JRobin disk file.");
    m_options.addOption("p", "dump-contents", false, "Just dump the DSes and RRAs in the JRobin disk file.");
    m_options.addOption("q", "quiet", false, "Do not print any informational output");
    m_options.addOption("v", "verbose", false, "Print plenty of informational output");
    CommandLineParser parser = new PosixParser();
    m_commandLine = parser.parse(m_options, argv);
    if (m_commandLine.hasOption("h")) {
        usage(m_options, m_commandLine);
        System.exit(0);
    }
    Map<String, Integer> analysisStrategies = new HashMap<String, Integer>();
    analysisStrategies.put("percentile", ANALYSIS_STRATEGIES.PERCENTILE_STRATEGY.ordinal());
    Map<String, Integer> replacementStrategies = new HashMap<String, Integer>();
    replacementStrategies.put("nan", REPLACEMENT_STRATEGIES.NAN_STRATEGY.ordinal());
    replacementStrategies.put("previous", REPLACEMENT_STRATEGIES.PREVIOUS_STRATEGY.ordinal());
    replacementStrategies.put("next", REPLACEMENT_STRATEGIES.NEXT_STRATEGY.ordinal());
    m_rrdFileName = m_commandLine.getOptionValue("f");
    m_dsNames = m_commandLine.getOptionValue("d", null);
    m_operands = new ArrayList<>();
    for (String operandStr : m_commandLine.getOptionValue("o", "95,5").split(",")) {
        m_operands.add(Double.parseDouble(operandStr));
    }
    m_analysisStrategy = analysisStrategies.get(m_commandLine.getOptionValue("l", "percentile").toLowerCase());
    m_replacementStrategy = replacementStrategies.get(m_commandLine.getOptionValue("r", "nan").toLowerCase());
    m_dryRun = m_commandLine.hasOption("n");
    m_dumpContents = m_commandLine.hasOption("p");
    m_quiet = m_commandLine.hasOption("q");
    m_verbose = m_commandLine.hasOption("v");
}
Also used : HashMap(java.util.HashMap) PosixParser(org.apache.commons.cli.PosixParser) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Example 34 with PosixParser

use of org.apache.commons.cli.PosixParser in project opennms by OpenNMS.

the class CheckWmi method main.

/**
 * <p>main</p>
 *
 * @param args an array of {@link java.lang.String} objects.
 * @throws org.apache.commons.cli.ParseException if any.
 */
public static void main(final String[] args) throws ParseException {
    final Options options = new Options();
    options.addOption("domain", true, "the NT/AD domain the credentials belong to");
    options.addOption("wmiClass", true, "the object class in WMI to query");
    options.addOption("wmiNamespace", true, "the namespace in WMI to use (default: " + WmiParams.WMI_DEFAULT_NAMESPACE + ")");
    options.addOption("wmiObject", true, "the object to query in WMI");
    options.addOption("wmiWql", true, "the query string to execute in WMI");
    options.addOption("op", true, "compare operation: NOOP, EQ, NEQ, GT, LT");
    options.addOption("value", true, "the value to compare to");
    options.addOption("matchType", true, "type of matching for multiple results: all, none, some, one");
    final CommandLineParser parser = new PosixParser();
    final CommandLine cmd = parser.parse(options, args);
    @SuppressWarnings("unchecked") List<String> arguments = (List<String>) cmd.getArgList();
    if (arguments.size() < 3) {
        usage(options, cmd);
        System.exit(1);
    }
    final String host = arguments.remove(0);
    final String user = arguments.remove(0);
    final String pass = arguments.remove(0);
    String wmiClass = "";
    if (cmd.hasOption("wmiClass")) {
        wmiClass = cmd.getOptionValue("wmiClass");
    }
    /* else {
			usage(options, cmd);
			System.exit(1);
		}*/
    String wmiObject = "";
    if (cmd.hasOption("wmiObject")) {
        wmiObject = cmd.getOptionValue("wmiObject");
    } else {
        usage(options, cmd);
        System.exit(1);
    }
    String wmiNamespace = WmiParams.WMI_DEFAULT_NAMESPACE;
    if (cmd.hasOption("wmiNamespace")) {
        wmiNamespace = cmd.getOptionValue("wmiNamespace");
    }
    String wmiWql = "";
    if (cmd.hasOption("wmiWql")) {
        wmiWql = cmd.getOptionValue("wmiWql");
    }
    /*else {
            usage(options, cmd);
            System.exit(1);
        } */
    String compVal = "";
    if (cmd.hasOption("value")) {
        compVal = cmd.getOptionValue("value");
    } else {
        usage(options, cmd);
        System.exit(1);
    }
    String compOp = "";
    if (cmd.hasOption("op")) {
        compOp = cmd.getOptionValue("op");
    } else {
        usage(options, cmd);
        System.exit(1);
    }
    String domain = "";
    if (cmd.hasOption("domain")) {
        domain = cmd.getOptionValue("domain");
    }
    String matchType = "all";
    if (cmd.hasOption("matchType")) {
        matchType = cmd.getOptionValue("matchType");
    }
    try {
        // Hold the WMI objects from the results.
        List<Object> wmiObjects;
        // Create the check parameters holder.
        WmiParams clientParams;
        if (wmiWql == null || wmiWql.length() == 0)
            clientParams = new WmiParams(WmiParams.WMI_OPERATION_INSTANCEOF, compVal, compOp, wmiClass, wmiObject);
        else
            clientParams = new WmiParams(WmiParams.WMI_OPERATION_WQL, compVal, compOp, wmiWql, wmiObject);
        // Create the WMI Manager
        final WmiManager mgr = new WmiManager(host, user, pass, domain, matchType);
        mgr.setNamespace(wmiNamespace);
        // Connect to the WMI server.
        mgr.init();
        // Perform the operation specified in the parameters.
        final WmiResult result = mgr.performOp(clientParams);
        // And retrieve the WMI objects from the results.
        wmiObjects = result.getResponse();
        // Now output a brief report of the check results.
        System.out.println("Checking: " + wmiWql + " for " + wmiObject + " Op: " + compOp + " Val: " + compVal);
        System.out.println("Check results: " + WmiResult.convertStateToString(result.getResultCode()) + " (" + wmiObjects.size() + ")");
        for (int i = 0; i < wmiObjects.size(); i++) {
            System.out.println("Result for (" + (i + 1) + ") " + wmiClass + "\\" + wmiObject + ": " + wmiObjects.get(i));
        }
        // Disconnect when we're done.
        mgr.close();
    } catch (final Exception e) {
        e.printStackTrace();
    }
}
Also used : Options(org.apache.commons.cli.Options) PosixParser(org.apache.commons.cli.PosixParser) ParseException(org.apache.commons.cli.ParseException) CommandLine(org.apache.commons.cli.CommandLine) List(java.util.List) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Example 35 with PosixParser

use of org.apache.commons.cli.PosixParser in project tomee by apache.

the class DontStartServerException method main.

public static void main(String[] args) throws SystemExitException {
    CommandLineParser parser = new PosixParser();
    // create the Options
    Options options = new Options();
    options.addOption(option("v", "version", "cmd.start.opt.version"));
    options.addOption(option("h", "help", "cmd.start.opt.help"));
    options.addOption(option(null, "conf", "file", "cmd.start.opt.conf"));
    options.addOption(option(null, "local-copy", "boolean", "cmd.start.opt.localCopy"));
    options.addOption(option(null, "examples", "cmd.start.opt.examples"));
    ResourceFinder finder = new ResourceFinder("META-INF/");
    Set<String> services = null;
    try {
        Map<String, Properties> serviceEntries = finder.mapAvailableProperties(ServerService.class.getName());
        services = serviceEntries.keySet();
        for (String service : services) {
            options.addOption(option(null, service + "-port", "int", "cmd.start.opt.port", service));
            options.addOption(option(null, service + "-bind", "host", "cmd.start.opt.bind", service));
        }
    } catch (Exception e) {
        services = Collections.EMPTY_SET;
    }
    CommandLine line = null;
    try {
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (ParseException exp) {
        help(options);
        throw new SystemExitException(-1);
    }
    if (line.hasOption("help")) {
        help(options);
        return;
    } else if (line.hasOption("version")) {
        OpenEjbVersion.get().print(System.out);
        return;
    } else if (line.hasOption("examples")) {
        try {
            String text = finder.findString("org.apache.openejb.cli/start.examples");
            System.out.println(text);
            return;
        } catch (Exception e) {
            System.err.println("Unable to print examples:");
            e.printStackTrace(System.err);
            throw new SystemExitException(-2);
        }
    }
    Properties props = SystemInstance.get().getProperties();
    if (line.hasOption("conf")) {
        props.setProperty("openejb.configuration", line.getOptionValue("conf"));
    } else if (line.hasOption("local-copy")) {
        String value = line.getOptionValue("local-copy");
        if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no") || value.equalsIgnoreCase("off")) {
            props.setProperty("openejb.localcopy", "false");
        } else {
            props.setProperty("openejb.localcopy", "true");
        }
    }
    for (String service : services) {
        String[] opts = { "port", "bind" };
        for (String opt : opts) {
            String option = service + "-" + opt;
            if (line.hasOption(option)) {
                props.setProperty(service + "." + opt, line.getOptionValue(option));
            }
        }
    }
    try {
        final SystemInstance system = SystemInstance.get();
        File libs = system.getHome().getDirectory("lib");
        system.getClassPath().addJarsToPath(libs);
        initServer(system);
    } catch (DontStartServerException ignored) {
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Options(org.apache.commons.cli.Options) ResourceFinder(org.apache.xbean.finder.ResourceFinder) PosixParser(org.apache.commons.cli.PosixParser) SystemExitException(org.apache.openejb.cli.SystemExitException) Properties(java.util.Properties) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) SystemExitException(org.apache.openejb.cli.SystemExitException) CommandLine(org.apache.commons.cli.CommandLine) SystemInstance(org.apache.openejb.loader.SystemInstance) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File)

Aggregations

PosixParser (org.apache.commons.cli.PosixParser)103 CommandLine (org.apache.commons.cli.CommandLine)97 CommandLineParser (org.apache.commons.cli.CommandLineParser)67 Options (org.apache.commons.cli.Options)56 ParseException (org.apache.commons.cli.ParseException)51 File (java.io.File)24 IOException (java.io.IOException)20 HelpFormatter (org.apache.commons.cli.HelpFormatter)16 Optional (java.util.Optional)10 Test (org.junit.Test)9 List (java.util.List)8 Configuration (org.apache.hadoop.conf.Configuration)8 GenericOptionsParser (org.apache.hadoop.util.GenericOptionsParser)7 SystemExitException (org.apache.openejb.cli.SystemExitException)7 Option (org.apache.commons.cli.Option)6 Parser (org.apache.commons.cli.Parser)6 Properties (java.util.Properties)5 Path (org.apache.hadoop.fs.Path)5 ArrayList (java.util.ArrayList)4 HBaseConfiguration (org.apache.hadoop.hbase.HBaseConfiguration)4