Search in sources :

Example 76 with ParseException

use of org.apache.commons.cli.ParseException in project java-docs-samples by GoogleCloudPlatform.

the class BookstoreClient method main.

public static void main(String[] args) throws Exception {
    Options options = createOptions();
    CommandLineParser parser = new DefaultParser();
    CommandLine params;
    try {
        params = parser.parse(options, args);
    } catch (ParseException e) {
        System.err.println("Invalid command line: " + e.getMessage());
        printUsage(options);
        return;
    }
    String address = params.getOptionValue("bookstore", DEFAULT_ADDRESS);
    String apiKey = params.getOptionValue("api_key");
    String authToken = params.getOptionValue("auth_token");
    String operation = params.getOptionValue("operation", "list");
    // Create gRPC stub.
    BookstoreGrpc.BookstoreBlockingStub bookstore = createBookstoreStub(address, apiKey, authToken);
    if ("list".equals(operation)) {
        listShelves(bookstore);
    } else if ("create".equals(operation)) {
        createShelf(bookstore);
    } else if ("enumerate".equals(operation)) {
        enumerate(bookstore);
    }
}
Also used : CallOptions(io.grpc.CallOptions) Options(org.apache.commons.cli.Options) 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 77 with ParseException

use of org.apache.commons.cli.ParseException in project java-docs-samples by GoogleCloudPlatform.

the class BookstoreServer method main.

public static void main(String[] args) throws Exception {
    Options options = createOptions();
    CommandLineParser parser = new DefaultParser();
    CommandLine line;
    try {
        line = parser.parse(options, args);
    } catch (ParseException e) {
        System.err.println("Invalid command line: " + e.getMessage());
        printUsage(options);
        return;
    }
    int port = DEFAULT_PORT;
    if (line.hasOption("port")) {
        String portOption = line.getOptionValue("port");
        try {
            port = Integer.parseInt(portOption);
        } catch (java.lang.NumberFormatException e) {
            System.err.println("Invalid port number: " + portOption);
            printUsage(options);
            return;
        }
    }
    final BookstoreData data = initializeBookstoreData();
    final BookstoreServer server = new BookstoreServer();
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            try {
                System.out.println("Shutting down");
                server.stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    server.start(port, data);
    System.out.format("Bookstore service listening on %d\n", port);
    server.blockUntilShutdown();
}
Also used : Options(org.apache.commons.cli.Options) ParseException(org.apache.commons.cli.ParseException) StatusException(io.grpc.StatusException) IOException(java.io.IOException) 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 78 with ParseException

use of org.apache.commons.cli.ParseException in project bookkeeper by apache.

the class AutoRecoveryMain method parseArgs.

/*
     * Parse console args
     */
private static ServerConfiguration parseArgs(String[] args) throws IllegalArgumentException {
    try {
        BasicParser parser = new BasicParser();
        CommandLine cmdLine = parser.parse(opts, args);
        if (cmdLine.hasOption('h')) {
            throw new IllegalArgumentException();
        }
        ServerConfiguration conf = new ServerConfiguration();
        String[] leftArgs = cmdLine.getArgs();
        if (cmdLine.hasOption('c')) {
            if (null != leftArgs && leftArgs.length > 0) {
                throw new IllegalArgumentException();
            }
            String confFile = cmdLine.getOptionValue("c");
            loadConfFile(conf, confFile);
        }
        if (null != leftArgs && leftArgs.length > 0) {
            throw new IllegalArgumentException();
        }
        return conf;
    } catch (ParseException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) ParseException(org.apache.commons.cli.ParseException)

Example 79 with ParseException

use of org.apache.commons.cli.ParseException in project bookkeeper by apache.

the class Main method parseArgs.

private static ServerConfiguration parseArgs(String[] args) throws IllegalArgumentException {
    try {
        BasicParser parser = new BasicParser();
        CommandLine cmdLine = parser.parse(BK_OPTS, args);
        if (cmdLine.hasOption('h')) {
            throw new IllegalArgumentException();
        }
        ServerConfiguration conf = new ServerConfiguration();
        if (cmdLine.hasOption('c')) {
            String confFile = cmdLine.getOptionValue("c");
            loadConfFile(conf, confFile);
        }
        if (cmdLine.hasOption("withAutoRecovery")) {
            conf.setAutoRecoveryDaemonEnabled(true);
        }
        if (cmdLine.hasOption("r")) {
            conf.setForceReadOnlyBookie(true);
        }
        // command line arguments overwrite settings in configuration file
        if (cmdLine.hasOption('z')) {
            String sZK = cmdLine.getOptionValue('z');
            log.info("Get cmdline zookeeper instance: {}", sZK);
            conf.setZkServers(sZK);
        }
        if (cmdLine.hasOption('m')) {
            String sZkLedgersRootPath = cmdLine.getOptionValue('m');
            log.info("Get cmdline zookeeper ledger path: {}", sZkLedgersRootPath);
            conf.setZkLedgersRootPath(sZkLedgersRootPath);
        }
        if (cmdLine.hasOption('p')) {
            String sPort = cmdLine.getOptionValue('p');
            log.info("Get cmdline bookie port: {}", sPort);
            Integer iPort = Integer.parseInt(sPort);
            conf.setBookiePort(iPort.intValue());
        }
        if (cmdLine.hasOption('j')) {
            String sJournalDir = cmdLine.getOptionValue('j');
            log.info("Get cmdline journal dir: {}", sJournalDir);
            conf.setJournalDirName(sJournalDir);
        }
        if (cmdLine.hasOption('i')) {
            String[] sIndexDirs = cmdLine.getOptionValues('i');
            log.info("Get cmdline index dirs: ");
            for (String index : sIndexDirs) {
                log.info("indexDir : {}", index);
            }
            conf.setIndexDirName(sIndexDirs);
        }
        if (cmdLine.hasOption('l')) {
            String[] sLedgerDirs = cmdLine.getOptionValues('l');
            log.info("Get cmdline ledger dirs: ");
            for (String ledger : sLedgerDirs) {
                log.info("ledgerdir : {}", ledger);
            }
            conf.setLedgerDirNames(sLedgerDirs);
        }
        return conf;
    } catch (ParseException e) {
        log.error("Error parsing command line arguments : ", e);
        throw new IllegalArgumentException(e);
    }
}
Also used : BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) ParseException(org.apache.commons.cli.ParseException)

Example 80 with ParseException

use of org.apache.commons.cli.ParseException in project bookkeeper by apache.

the class BookkeeperVerifierMain method main.

public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("ledger_path", true, "Hostname or IP of bookie to benchmark");
    options.addOption("zookeeper", true, "Zookeeper ensemble, (default \"localhost:2181\")");
    options.addOption("ensemble_size", true, "Bookkeeper client ensemble size");
    options.addOption("write_quorum", true, "Bookkeeper client write quorum size");
    options.addOption("ack_quorum", true, "Bookkeeper client ack quorum size");
    options.addOption("duration", true, "Run duration in seconds");
    options.addOption("drainTimeout", true, "Seconds to wait for in progress ops to end");
    options.addOption("target_concurrent_ledgers", true, "target number ledgers to write to concurrently");
    options.addOption("target_concurrent_writes", true, "target number of concurrent writes per ledger");
    options.addOption("target_write_group", true, "target number of entries to write at a time");
    options.addOption("target_read_group", true, "target number of entries to read at a time");
    options.addOption("target_ledgers", true, "Target number of ledgers");
    options.addOption("target_ledger_size", true, "Target size per ledger");
    options.addOption("target_entry_size", true, "Target size per entry");
    options.addOption("target_concurrent_reads", true, "Number of reads to maintain");
    options.addOption("cold_to_hot_ratio", true, "Ratio of reads on open ledgers");
    options.addOption("help", false, "Print this help message");
    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        printHelpAndExit(options, "Unable to parse command line", 1);
    }
    if (cmd.hasOption("help")) {
        printHelpAndExit(options, "Help:", 0);
    }
    String ledgerPath = cmd.getOptionValue("ledger_path", "/ledgers");
    String zkString = cmd.getOptionValue("zookeeper", "localhost:2181");
    int ensembleSize = 0;
    int writeQuorum = 0;
    int ackQuorum = 0;
    int duration = 0;
    int drainTimeout = 0;
    int targetConcurrentLedgers = 0;
    int targetConcurrentWrites = 0;
    int targetWriteGroup = 0;
    int targetReadGroup = 0;
    int targetLedgers = 0;
    long targetLedgerSize = 0;
    int targetEntrySize = 0;
    int targetConcurrentReads = 0;
    double coldToHotRatio = 0;
    try {
        ensembleSize = Integer.parseInt(cmd.getOptionValue("ensemble_size", "3"));
        writeQuorum = Integer.parseInt(cmd.getOptionValue("write_quorum", "3"));
        ackQuorum = Integer.parseInt(cmd.getOptionValue("ack_quorum", "2"));
        duration = Integer.parseInt(cmd.getOptionValue("duration", "600"));
        drainTimeout = Integer.parseInt(cmd.getOptionValue("drain_timeout", "10"));
        targetConcurrentLedgers = Integer.parseInt(cmd.getOptionValue("target_concurrent_ledgers", "4"));
        targetConcurrentWrites = Integer.parseInt(cmd.getOptionValue("target_concurrent_writes", "12"));
        targetWriteGroup = Integer.parseInt(cmd.getOptionValue("target_write_group", "4"));
        targetReadGroup = Integer.parseInt(cmd.getOptionValue("target_read_group", "4"));
        targetLedgers = Integer.parseInt(cmd.getOptionValue("target_ledgers", "32"));
        targetLedgerSize = Long.parseLong(cmd.getOptionValue("target_ledger_size", "33554432"));
        targetEntrySize = Integer.parseInt(cmd.getOptionValue("target_entry_size", "16384"));
        targetConcurrentReads = Integer.parseInt(cmd.getOptionValue("target_concurrent_reads", "16"));
        coldToHotRatio = Double.parseDouble(cmd.getOptionValue("cold_to_hot_ratio", "0.5"));
    } catch (NumberFormatException e) {
        printHelpAndExit(options, "Invalid argument", 0);
    }
    ClientConfiguration conf = new ClientConfiguration();
    conf.setZkServers(zkString);
    conf.setZkLedgersRootPath(ledgerPath);
    BookKeeper bkclient = new BookKeeper(conf);
    BookkeeperVerifier verifier = new BookkeeperVerifier(new DirectBookkeeperDriver(bkclient), ensembleSize, writeQuorum, ackQuorum, duration, drainTimeout, targetConcurrentLedgers, targetConcurrentWrites, targetWriteGroup, targetReadGroup, targetLedgers, targetLedgerSize, targetEntrySize, targetConcurrentReads, coldToHotRatio);
    try {
        verifier.run();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    } finally {
        bkclient.close();
    }
}
Also used : Options(org.apache.commons.cli.Options) PosixParser(org.apache.commons.cli.PosixParser) BookKeeper(org.apache.bookkeeper.client.BookKeeper) ParseException(org.apache.commons.cli.ParseException) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration)

Aggregations

ParseException (org.apache.commons.cli.ParseException)586 CommandLine (org.apache.commons.cli.CommandLine)488 CommandLineParser (org.apache.commons.cli.CommandLineParser)380 Options (org.apache.commons.cli.Options)370 DefaultParser (org.apache.commons.cli.DefaultParser)220 HelpFormatter (org.apache.commons.cli.HelpFormatter)204 GnuParser (org.apache.commons.cli.GnuParser)173 IOException (java.io.IOException)124 Option (org.apache.commons.cli.Option)109 File (java.io.File)90 PosixParser (org.apache.commons.cli.PosixParser)65 Path (org.apache.hadoop.fs.Path)50 ArrayList (java.util.ArrayList)42 Properties (java.util.Properties)35 BasicParser (org.apache.commons.cli.BasicParser)31 FileInputStream (java.io.FileInputStream)29 Job (org.apache.hadoop.mapreduce.Job)27 Configuration (org.apache.hadoop.conf.Configuration)26 List (java.util.List)25 URI (java.net.URI)21