Search in sources :

Example 81 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project toolkit by googleapis.

the class Pubsub method main.

public static void main(String[] args) throws Exception {
    Options options = new Options().addOption(Option.builder("c").longOpt("client").required().hasArg().desc("client to use: gapic/grpc").build()).addOption(Option.builder("cert").required().hasArg().desc("certificate file").build()).addOption("ep", "endpoint", true, "endpoint to connect to").addOption("n", "num_workers", true, "number of concurrent calls").addOption("wd", "warmup_duration", true, "warmup duration in seconds").addOption("d", "duration", true, "test duration in seconds");
    CommandLine helpCl = new DefaultParser().parse(new Options().addOption("h", "help", false, "print help message"), args, true);
    if (helpCl.hasOption('h')) {
        new HelpFormatter().printHelp("Pubsub -c <client> -cert <cert_file> -n <num_workers>", options);
        return;
    }
    CommandLine cl = new DefaultParser().parse(options, args);
    Settings settings = Settings.builder().warmDurNano(Long.parseLong(cl.getOptionValue("wd", "60")) * BILLION).targetDurNano(Long.parseLong(cl.getOptionValue("d", "60")) * BILLION).numWorkers(Integer.parseInt(cl.getOptionValue("n", "20"))).endpoint(cl.getOptionValue("ep", "localhost:8080")).cert(cl.getOptionValue("cert")).build();
    String client = cl.getOptionValue("c", "");
    switch(client) {
        case "grpc":
            grpc(settings);
            break;
        case "gapic":
            gapic(settings);
            break;
        default:
            throw new IllegalArgumentException("unknown client: " + client);
    }
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) CallOptions(io.grpc.CallOptions) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) TopicAdminSettings(com.google.cloud.pubsub.spi.v1.TopicAdminSettings) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 82 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project jPOS by jpos.

the class Q2 method parseCmdLine.

private void parseCmdLine(String[] args) {
    CommandLineParser parser = new DefaultParser();
    Options options = new Options();
    options.addOption("v", "version", false, "Q2's version");
    options.addOption("d", "deploydir", true, "Deployment directory");
    options.addOption("r", "recursive", false, "Deploy subdirectories recursively");
    options.addOption("h", "help", false, "Usage information");
    options.addOption("C", "config", true, "Configuration bundle");
    options.addOption("e", "encrypt", true, "Encrypt configuration bundle");
    options.addOption("i", "cli", false, "Command Line Interface");
    options.addOption("c", "command", true, "Command to execute");
    options.addOption("O", "osgi", false, "Start experimental OSGi framework server");
    options.addOption("p", "pid-file", true, "Store project's pid");
    options.addOption("n", "name", true, "Optional name (defaults to 'Q2')");
    options.addOption("s", "ssh", false, "Enable SSH server");
    options.addOption("sp", "ssh-port", true, "SSH port (defaults to 2222)");
    options.addOption("sa", "ssh-authorized-keys", true, "Path to authorized key file (defaults to 'cfg/authorized_keys')");
    options.addOption("su", "ssh-user", true, "SSH user (defaults to 'admin')");
    options.addOption("sh", "ssh-host-key-file", true, "SSH host key file, defaults to 'cfg/hostkeys.ser'");
    options.addOption("Ns", "no-scan", false, "Disables deploy directory scan");
    options.addOption("Nd", "no-dynamic", false, "Disables dynamic classloader");
    options.addOption("E", "environment", true, "Environment name.\nCan be given multiple times (applied in order, and values may override previous ones)");
    options.addOption("Ed", "envdir", true, "Environment file directory, defaults to cfg");
    try {
        // log4shell prevention
        System.setProperty("log4j2.formatMsgNoLookups", "true");
        CommandLine line = parser.parse(options, args);
        if (line.hasOption("v")) {
            displayVersion();
            System.exit(0);
        }
        if (line.hasOption("h")) {
            HelpFormatter helpFormatter = new HelpFormatter();
            helpFormatter.printHelp("Q2", options);
            System.exit(0);
        }
        // force reload if any of the env options was changed
        if (line.hasOption("Ed")) {
            System.setProperty("jpos.envdir", line.getOptionValue("Ed"));
        }
        if (line.hasOption("E")) {
            System.setProperty("jpos.env", ISOUtil.commaEncode(line.getOptionValues("E")));
        }
        if (line.hasOption("c")) {
            cli = new CLI(this, line.getOptionValue("c"), line.hasOption("i"));
        } else if (line.hasOption("i"))
            cli = new CLI(this, null, true);
        String dir = DEFAULT_DEPLOY_DIR;
        if (line.hasOption("d")) {
            dir = line.getOptionValue("d");
        } else if (cli != null)
            dir = dir + "-" + "cli";
        recursive = line.hasOption("r");
        this.deployDir = new File(dir);
        if (line.hasOption("C"))
            deployBundle(new File(line.getOptionValue("C")), false);
        if (line.hasOption("e"))
            deployBundle(new File(line.getOptionValue("e")), true);
        if (line.hasOption("O"))
            startOSGI = true;
        if (line.hasOption("p"))
            pidFile = line.getOptionValue("p");
        if (line.hasOption("n"))
            name = line.getOptionValue("n");
        disableDeployScan = line.hasOption("Ns");
        disableDynamicClassloader = line.hasOption("Nd");
        enableSsh = line.hasOption("s");
        sshPort = Integer.parseInt(line.getOptionValue("sp", "2222"));
        sshAuthorizedKeys = line.getOptionValue("sa", "cfg/authorized_keys");
        sshUser = line.getOptionValue("su", "admin");
        sshHostKeyFile = line.getOptionValue("sh", "cfg/hostkeys.ser");
    } catch (MissingArgumentException e) {
        System.out.println("ERROR: " + e.getMessage());
        System.exit(1);
    } catch (IllegalAccessError | UnrecognizedOptionException e) {
        System.out.println(e.getMessage());
        System.exit(1);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : Options(org.apache.commons.cli.Options) MissingArgumentException(org.apache.commons.cli.MissingArgumentException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) JDOMException(org.jdom2.JDOMException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) GeneralSecurityException(java.security.GeneralSecurityException) BundleException(org.osgi.framework.BundleException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) SAXException(org.xml.sax.SAXException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ISOException(org.jpos.iso.ISOException) MissingArgumentException(org.apache.commons.cli.MissingArgumentException) HelpFormatter(org.apache.commons.cli.HelpFormatter) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 83 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project incubator-gobblin by apache.

the class CliOptions method parseArgs.

/**
 * Parse command line arguments and return a {@link java.util.Properties} object for the gobblin job found.
 * @param caller Class of the calling main method. Used for error logs.
 * @param args Command line arguments.
 * @return Instance of {@link Properties} for the Gobblin job to run.
 * @throws IOException
 */
public static Properties parseArgs(Class<?> caller, String[] args) throws IOException {
    try {
        // Parse command-line options
        CommandLine cmd = new DefaultParser().parse(options(), args);
        if (cmd.hasOption(HELP_OPTION.getOpt())) {
            printUsage(caller);
            System.exit(0);
        }
        if (!cmd.hasOption(SYS_CONFIG_OPTION.getLongOpt()) || !cmd.hasOption(JOB_CONFIG_OPTION.getLongOpt())) {
            printUsage(caller);
            System.exit(1);
        }
        // Load system and job configuration properties
        Properties sysConfig = JobConfigurationUtils.fileToProperties(cmd.getOptionValue(SYS_CONFIG_OPTION.getLongOpt()));
        Properties jobConfig = JobConfigurationUtils.fileToProperties(cmd.getOptionValue(JOB_CONFIG_OPTION.getLongOpt()));
        return JobConfigurationUtils.combineSysAndJobProperties(sysConfig, jobConfig);
    } catch (ParseException | ConfigurationException e) {
        throw new IOException(e);
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ConfigurationException(org.apache.commons.configuration.ConfigurationException) ParseException(org.apache.commons.cli.ParseException) IOException(java.io.IOException) Properties(java.util.Properties) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 84 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project incubator-gobblin by apache.

the class StateStoreBasedWatermarkStorageCli method run.

@Override
public void run(String[] args) {
    Options options = new Options();
    options.addOption(HELP);
    options.addOption(ZK);
    options.addOption(JOB_NAME);
    options.addOption(ROOT_DIR);
    options.addOption(WATCH);
    CommandLine cli;
    try {
        CommandLineParser parser = new DefaultParser();
        cli = parser.parse(options, Arrays.copyOfRange(args, 1, args.length));
    } catch (ParseException pe) {
        System.out.println("Command line parse exception: " + pe.getMessage());
        return;
    }
    if (cli.hasOption(HELP.getOpt())) {
        printUsage(options);
        return;
    }
    TaskState taskState = new TaskState();
    String jobName;
    if (!cli.hasOption(JOB_NAME.getOpt())) {
        log.error("Need Job Name to be specified --", JOB_NAME.getLongOpt());
        throw new RuntimeException("Need Job Name to be specified");
    } else {
        jobName = cli.getOptionValue(JOB_NAME.getOpt());
        log.info("Using job name: {}", jobName);
    }
    taskState.setProp(ConfigurationKeys.JOB_NAME_KEY, jobName);
    String zkAddress = "locahost:2181";
    if (cli.hasOption(ZK.getOpt())) {
        zkAddress = cli.getOptionValue(ZK.getOpt());
    }
    log.info("Using zk address : {}", zkAddress);
    taskState.setProp(StateStoreBasedWatermarkStorage.WATERMARK_STORAGE_TYPE_KEY, "zk");
    taskState.setProp("state.store.zk.connectString", zkAddress);
    if (cli.hasOption(ROOT_DIR.getOpt())) {
        String rootDir = cli.getOptionValue(ROOT_DIR.getOpt());
        taskState.setProp(StateStoreBasedWatermarkStorage.WATERMARK_STORAGE_CONFIG_PREFIX + ConfigurationKeys.STATE_STORE_ROOT_DIR_KEY, rootDir);
        log.info("Setting root dir to {}", rootDir);
    } else {
        log.error("Need root directory specified");
        printUsage(options);
        return;
    }
    StateStoreBasedWatermarkStorage stateStoreBasedWatermarkStorage = new StateStoreBasedWatermarkStorage(taskState);
    final AtomicBoolean stop = new AtomicBoolean(true);
    if (cli.hasOption(WATCH.getOpt())) {
        stop.set(false);
    }
    try {
        if (!stop.get()) {
            Runtime.getRuntime().addShutdownHook(new Thread() {

                public void run() {
                    stop.set(true);
                }
            });
        }
        do {
            boolean foundWatermark = false;
            try {
                for (CheckpointableWatermarkState wmState : stateStoreBasedWatermarkStorage.getAllCommittedWatermarks()) {
                    foundWatermark = true;
                    System.out.println(wmState.getProperties());
                }
            } catch (IOException ie) {
                Throwables.propagate(ie);
            }
            if (!foundWatermark) {
                System.out.println("No watermarks found.");
            }
            if (!stop.get()) {
                Thread.sleep(1000);
            }
        } while (!stop.get());
    } catch (Exception e) {
        Throwables.propagate(e);
    }
}
Also used : Options(org.apache.commons.cli.Options) IOException(java.io.IOException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 85 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project incubator-gobblin by apache.

the class StressTestUtils method parseCommandLine.

/**
 * Parse command line.
 */
public static CommandLine parseCommandLine(Options options, String[] args) throws ParseException {
    CommandLineParser parser = new DefaultParser();
    CommandLine cli = parser.parse(options, args);
    if (cli.hasOption(StressTestUtils.HELP_OPT.getOpt())) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(MRStressTest.class.getSimpleName(), OPTIONS);
        System.exit(0);
    }
    return cli;
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) DefaultParser(org.apache.commons.cli.DefaultParser)

Aggregations

DefaultParser (org.apache.commons.cli.DefaultParser)344 CommandLine (org.apache.commons.cli.CommandLine)290 Options (org.apache.commons.cli.Options)241 CommandLineParser (org.apache.commons.cli.CommandLineParser)234 ParseException (org.apache.commons.cli.ParseException)224 HelpFormatter (org.apache.commons.cli.HelpFormatter)116 Option (org.apache.commons.cli.Option)67 File (java.io.File)34 IOException (java.io.IOException)32 HashMap (java.util.HashMap)30 JobConfig (edu.iu.dsc.tws.api.JobConfig)26 ArrayList (java.util.ArrayList)26 Config (edu.iu.dsc.tws.api.config.Config)25 Twister2Job (edu.iu.dsc.tws.api.Twister2Job)21 Properties (java.util.Properties)19 Test (org.junit.jupiter.api.Test)19 Field (core.field.Field)17 Level (java.util.logging.Level)13 ToolOptions (com.google.api.tools.framework.tools.ToolOptions)12 OptionGroup (org.apache.commons.cli.OptionGroup)11