Search in sources :

Example 56 with PosixParser

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

the class GridRandomCommandLineLoader method main.

/**
 * Main entry point.
 *
 * @param args Command line arguments.
 */
@SuppressWarnings({ "BusyWait" })
public static void main(String[] args) {
    System.setProperty(IgniteSystemProperties.IGNITE_UPDATE_NOTIFIER, "false");
    logo();
    Options options = createOptions();
    // Create the command line parser.
    CommandLineParser parser = new PosixParser();
    String cfgPath = null;
    long minTtl = DFLT_MIN_TIMEOUT;
    long maxTtl = DFLT_MAX_TIMEOUT;
    long duration = DFLT_RUN_TIMEOUT;
    String logCfgPath = null;
    try {
        CommandLine cmd = parser.parse(options, args);
        if (cmd.hasOption(OPTION_HELP))
            exit(null, options, 0);
        if (!cmd.hasOption(OPTION_LOG_CFG))
            exit("-log should be set", options, -1);
        else
            logCfgPath = cmd.getOptionValue(OPTION_LOG_CFG);
        if (cmd.hasOption(OPTION_CFG))
            cfgPath = cmd.getOptionValue(OPTION_CFG);
        try {
            if (cmd.hasOption(OPTION_DURATION))
                duration = Long.parseLong(cmd.getOptionValue(OPTION_DURATION));
        } catch (NumberFormatException ignored) {
            exit("Invalid argument for option: " + OPTION_DURATION, options, -1);
        }
        try {
            if (cmd.hasOption(OPTION_MIN_TTL))
                minTtl = Long.parseLong(cmd.getOptionValue(OPTION_MIN_TTL));
        } catch (NumberFormatException ignored) {
            exit("Invalid argument for option: " + OPTION_MIN_TTL, options, -1);
        }
        try {
            if (cmd.hasOption(OPTION_MAX_TTL))
                maxTtl = Long.parseLong(cmd.getOptionValue(OPTION_MAX_TTL));
        } catch (NumberFormatException ignored) {
            exit("Invalid argument for option: " + OPTION_MAX_TTL, options, -1);
        }
        if (minTtl >= maxTtl)
            exit("Invalid arguments for options: " + OPTION_MAX_TTL + ", " + OPTION_MIN_TTL, options, -1);
    } catch (ParseException e) {
        exit(e.getMessage(), options, -1);
    }
    System.out.println("Configuration path: " + cfgPath);
    System.out.println("Log4j configuration path: " + logCfgPath);
    System.out.println("Duration: " + duration);
    System.out.println("Minimum TTL: " + minTtl);
    System.out.println("Maximum TTL: " + maxTtl);
    G.addListener(new IgnitionListener() {

        @Override
        public void onStateChange(String name, IgniteState state) {
            if (state == STOPPED && latch != null)
                latch.countDown();
        }
    });
    Random rand = new Random();
    long now = System.currentTimeMillis();
    long end = duration + System.currentTimeMillis();
    try {
        while (now < end) {
            G.start(getConfiguration(cfgPath, logCfgPath));
            long delay = rand.nextInt((int) (maxTtl - minTtl)) + minTtl;
            delay = (now + delay > end) ? (end - now) : delay;
            now = System.currentTimeMillis();
            echo("Time left (ms): " + (end - now));
            echo("Going to sleep for (ms): " + delay);
            Thread.sleep(delay);
            G.stopAll(false);
            now = System.currentTimeMillis();
        }
    } catch (IgniteCheckedException e) {
        echo(e);
        exit("Failed to start grid: " + e.getMessage(), null, -1);
    } catch (InterruptedException e) {
        echo("Loader was interrupted (exiting): " + e.getMessage());
    }
    latch = new CountDownLatch(G.allGrids().size());
    try {
        while (latch.getCount() > 0) latch.await();
    } catch (InterruptedException e) {
        echo("Loader was interrupted (exiting): " + e.getMessage());
    }
    System.exit(0);
}
Also used : Options(org.apache.commons.cli.Options) PosixParser(org.apache.commons.cli.PosixParser) IgnitionListener(org.apache.ignite.IgnitionListener) CountDownLatch(java.util.concurrent.CountDownLatch) CommandLine(org.apache.commons.cli.CommandLine) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Random(java.util.Random) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) IgniteState(org.apache.ignite.IgniteState)

Example 57 with PosixParser

use of org.apache.commons.cli.PosixParser in project incubator-systemml by apache.

the class DMLScript method parseCLArguments.

/**
 * Parses command line arguments to create a {@link DMLOptions} instance with the correct options
 * @param args	arguments from the command line
 * @param options	an {@link Options} instance containing the options that need to be parsed
 * @return an instance of {@link Options} that contain the correct {@link Option}s.
 * @throws org.apache.commons.cli.ParseException if there is an incorrect option specified in the CLI
 */
public static DMLOptions parseCLArguments(String[] args, Options options) throws org.apache.commons.cli.ParseException {
    CommandLineParser clParser = new PosixParser();
    CommandLine line = clParser.parse(options, args);
    DMLOptions dmlOptions = new DMLOptions();
    dmlOptions.help = line.hasOption("help");
    dmlOptions.scriptType = line.hasOption("python") ? ScriptType.PYDML : ScriptType.DML;
    dmlOptions.debug = line.hasOption("debug");
    dmlOptions.gpu = line.hasOption("gpu");
    if (dmlOptions.gpu) {
        String force = line.getOptionValue("gpu");
        if (force != null) {
            if (force.equalsIgnoreCase("force")) {
                dmlOptions.forceGPU = true;
            } else {
                throw new org.apache.commons.cli.ParseException("Invalid argument specified for -gpu option");
            }
        }
    }
    if (line.hasOption("exec")) {
        String execMode = line.getOptionValue("exec");
        if (execMode != null) {
            if (execMode.equalsIgnoreCase("hadoop"))
                dmlOptions.execMode = RUNTIME_PLATFORM.HADOOP;
            else if (execMode.equalsIgnoreCase("singlenode"))
                dmlOptions.execMode = RUNTIME_PLATFORM.SINGLE_NODE;
            else if (execMode.equalsIgnoreCase("hybrid"))
                dmlOptions.execMode = RUNTIME_PLATFORM.HYBRID;
            else if (execMode.equalsIgnoreCase("hybrid_spark"))
                dmlOptions.execMode = RUNTIME_PLATFORM.HYBRID_SPARK;
            else if (execMode.equalsIgnoreCase("spark"))
                dmlOptions.execMode = RUNTIME_PLATFORM.SPARK;
            else
                throw new org.apache.commons.cli.ParseException("Invalid argument specified for -exec option, must be one of [hadoop, singlenode, hybrid, hybrid_spark, spark]");
        }
    }
    if (line.hasOption("explain")) {
        dmlOptions.explainType = ExplainType.RUNTIME;
        String explainType = line.getOptionValue("explain");
        if (explainType != null) {
            if (explainType.equalsIgnoreCase("hops"))
                dmlOptions.explainType = ExplainType.HOPS;
            else if (explainType.equalsIgnoreCase("runtime"))
                dmlOptions.explainType = ExplainType.RUNTIME;
            else if (explainType.equalsIgnoreCase("recompile_hops"))
                dmlOptions.explainType = ExplainType.RECOMPILE_HOPS;
            else if (explainType.equalsIgnoreCase("recompile_runtime"))
                dmlOptions.explainType = ExplainType.RECOMPILE_RUNTIME;
            else
                throw new org.apache.commons.cli.ParseException("Invalid argument specified for -hops option, must be one of [hops, runtime, recompile_hops, recompile_runtime]");
        }
    }
    dmlOptions.stats = line.hasOption("stats");
    if (dmlOptions.stats) {
        String statsCount = line.getOptionValue("stats");
        if (statsCount != null) {
            try {
                dmlOptions.statsCount = Integer.parseInt(statsCount);
            } catch (NumberFormatException e) {
                throw new org.apache.commons.cli.ParseException("Invalid argument specified for -stats option, must be a valid integer");
            }
        }
    }
    dmlOptions.clean = line.hasOption("clean");
    if (line.hasOption("config")) {
        dmlOptions.configFile = line.getOptionValue("config");
    }
    if (line.hasOption("f")) {
        dmlOptions.filePath = line.getOptionValue("f");
    }
    if (line.hasOption("s")) {
        dmlOptions.script = line.getOptionValue("s");
    }
    // Positional arguments map is created as ("$1", "a"), ("$2", 123), ....
    if (line.hasOption("args")) {
        String[] argValues = line.getOptionValues("args");
        for (int k = 0; k < argValues.length; k++) {
            String str = argValues[k];
            if (!str.isEmpty()) {
                dmlOptions.argVals.put("$" + (k + 1), str);
            }
        }
    }
    // Named arguments map is created as ("$K, 123), ("$X", "X.csv"), ....
    if (line.hasOption("nvargs")) {
        String varNameRegex = "^[a-zA-Z]([a-zA-Z0-9_])*$";
        String[] nvargValues = line.getOptionValues("nvargs");
        for (String str : nvargValues) {
            if (!str.isEmpty()) {
                String[] kv = str.split("=");
                if (kv.length != 2) {
                    throw new org.apache.commons.cli.ParseException("Invalid argument specified for -nvargs option, must be a list of space separated K=V pairs, where K is a valid name of a variable in the DML/PyDML program");
                }
                if (!kv[0].matches(varNameRegex)) {
                    throw new org.apache.commons.cli.ParseException("Invalid argument specified for -nvargs option, " + kv[0] + " does not seem like a valid variable name in DML. Valid variable names in DML start with upper-case or lower-case letter, and contain only letters, digits, or underscores");
                }
                dmlOptions.argVals.put("$" + kv[0], kv[1]);
            }
        }
    }
    return dmlOptions;
}
Also used : PosixParser(org.apache.commons.cli.PosixParser) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.sysml.parser.ParseException)

Example 58 with PosixParser

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

the class EffectiveTomEEXml method parseCommand.

private static CommandLine parseCommand(final String[] args) throws SystemExitException {
    final Options options = new Options();
    options.addOption(OptionBuilder.hasArg(true).withLongOpt("path").withDescription("[openejb|tomee].xml path").create("p"));
    final CommandLine line;
    try {
        line = new PosixParser().parse(options, args);
    } catch (final ParseException exp) {
        help(options);
        throw new SystemExitException(-1);
    }
    if (line.hasOption("help")) {
        help(options);
        return null;
    }
    return line;
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) SystemExitException(org.apache.openejb.cli.SystemExitException) ParseException(org.apache.commons.cli.ParseException)

Example 59 with PosixParser

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

the class AppValidator method main.

public static void main(final String[] args) throws SystemExitException {
    final CommandLineParser parser = new PosixParser();
    // create the Options
    final Options options = new Options();
    options.addOption(AppValidator.option("v", "version", "cmd.validate.opt.version"));
    options.addOption(AppValidator.option("h", "help", "cmd.validate.opt.help"));
    CommandLine line = null;
    try {
        line = parser.parse(options, args);
    } catch (final ParseException exp) {
        AppValidator.help(options);
        throw new SystemExitException(-1);
    }
    if (line.hasOption("help")) {
        AppValidator.help(options);
        return;
    } else if (line.hasOption("version")) {
        OpenEjbVersion.get().print(System.out);
        return;
    }
    if (line.getArgList().size() == 0) {
        System.out.println("Must specify an module id.");
        AppValidator.help(options);
    }
    final DeploymentLoader deploymentLoader = new DeploymentLoader();
    try {
        final AppValidator validator = new AppValidator();
        for (final Object obj : line.getArgList()) {
            final String module = (String) obj;
            final File file = new File(module);
            final AppModule appModule = deploymentLoader.load(file, null);
            validator.validate(appModule);
        }
    } catch (final Exception e) {
        e.printStackTrace();
    }
}
Also used : Options(org.apache.commons.cli.Options) PosixParser(org.apache.commons.cli.PosixParser) SystemExitException(org.apache.openejb.cli.SystemExitException) SystemExitException(org.apache.openejb.cli.SystemExitException) OpenEJBException(org.apache.openejb.OpenEJBException) DefinitionException(javax.enterprise.inject.spi.DefinitionException) ParseException(org.apache.commons.cli.ParseException) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File)

Example 60 with PosixParser

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

the class Cipher method main.

public static void main(final String[] args) throws SystemExitException {
    final CommandLineParser parser = new PosixParser();
    // create the Options
    final Options options = new Options();
    options.addOption(option("h", "help", "cmd.cipher.opt.help"));
    options.addOption(option("c", "cipher", "c", "cmd.cipher.opt.impl"));
    options.addOption(option("d", "decrypt", "cmd.cipher.opt.decrypt"));
    options.addOption(option("e", "encrypt", "cmd.cipher.opt.encrypt"));
    final CommandLine line;
    try {
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (final ParseException exp) {
        help(options);
        throw new SystemExitException(-1);
    }
    if (line.hasOption("help")) {
        help(options);
        return;
    }
    String cipherName = "Static3DES";
    if (line.hasOption("cipher")) {
        cipherName = line.getOptionValue("cipher");
    }
    if (line.getArgList().size() != 1) {
        System.out.println("Must specify either a plain text to encrypt or a ciphered value to decrypt.");
        help(options);
        return;
    }
    final PasswordCipher cipher;
    try {
        cipher = PasswordCipherFactory.getPasswordCipher(cipherName);
    } catch (final PasswordCipherException e) {
        System.out.println("Could not load password cipher implementation class. Check your classpath.");
        availableCiphers();
        throw new SystemExitException(-1);
    }
    if (line.hasOption("decrypt")) {
        final String pwdArg = (String) line.getArgList().get(0);
        final char[] encryptdPassword = pwdArg.toCharArray();
        System.out.println(cipher.decrypt(encryptdPassword));
    } else {
        // if option neither encrypt/decrypt is specified, we assume
        // it is encrypt.
        final String plainPassword = (String) line.getArgList().get(0);
        System.out.println(new String(cipher.encrypt(plainPassword)));
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PasswordCipher(org.apache.openejb.cipher.PasswordCipher) PasswordCipherException(org.apache.openejb.cipher.PasswordCipherException) PosixParser(org.apache.commons.cli.PosixParser) SystemExitException(org.apache.openejb.cli.SystemExitException) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

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