Search in sources :

Example 96 with DefaultParser

use of org.apache.commons.cli.DefaultParser 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 97 with DefaultParser

use of org.apache.commons.cli.DefaultParser 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 98 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project Krill by KorAP.

the class Indexer method main.

/**
 * Main method.
 *
 * @param argv
 *            Argument list,
 *            expecting the properties file
 *            and a list of directories
 * @throws IOException
 */
public static void main(String[] argv) throws IOException {
    Options options = new Options();
    options.addOption(Option.builder("c").longOpt("config").desc("configuration file (defaults to " + KrillProperties.defaultPropertiesLocation + ").").hasArg().argName("properties file").required().build());
    options.addOption(Option.builder("i").longOpt("inputDir").desc("input directories separated by semicolons. The input files " + "have to be in <filename>.json.gz format. ").hasArgs().argName("input directories").required().valueSeparator(new Character(';')).build());
    options.addOption(Option.builder("o").longOpt("outputDir").desc("index output directory (defaults to " + "krill.indexDir in the configuration.").hasArg().argName("output directory").build());
    CommandLineParser parser = new DefaultParser();
    String propFile = null;
    String[] inputDirectories = null;
    try {
        CommandLine cmd = parser.parse(options, argv);
        log.info("Configuration file: " + cmd.getOptionValue("c"));
        propFile = cmd.getOptionValue("c");
        log.info("Input directories: " + StringUtils.join(cmd.getOptionValues("i"), ";"));
        inputDirectories = cmd.getOptionValues("i");
        if (cmd.hasOption("o")) {
            log.info("Output directory: " + cmd.getOptionValue("o"));
            path = cmd.getOptionValue("o");
        }
    } catch (MissingOptionException e) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("Krill indexer\n java -jar -c <properties file> -i <input directories> " + "[-o <output directory>]", options);
        return;
    } catch (ParseException e) {
        log.error("Unexpected error: " + e);
        e.printStackTrace();
    }
    // Load properties
    Properties prop = KrillProperties.loadProperties(propFile);
    // Get indexer object
    Indexer indexer = new Indexer(prop);
    // Iterate over list of directories
    for (String arg : inputDirectories) {
        log.info("Indexing files in " + arg);
        File f = new File(arg);
        if (f.isDirectory())
            indexer.parse(f);
    }
    indexer.closeIndex();
    // Final commit
    log.info("Finished indexing.");
    // Finish indexing
    String message = "Indexed " + indexer.count + " file";
    if (indexer.count > 1) {
        message += "s";
    }
    System.out.print(message + ".");
}
Also used : Options(org.apache.commons.cli.Options) KrillProperties(de.ids_mannheim.korap.util.KrillProperties) HelpFormatter(org.apache.commons.cli.HelpFormatter) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) MissingOptionException(org.apache.commons.cli.MissingOptionException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 99 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project openmeetings by apache.

the class Admin method process.

// package private wrapper for testing
void process(String... args) throws Exception {
    CommandLineParser parser = new DefaultParser();
    try {
        cmdl = parser.parse(opts, args);
    } catch (ParseException e) {
        log(e.getMessage());
        usage();
        throw new ExitException();
    }
    verbose = cmdl.hasOption('v');
    Command cmd = Command.usage;
    if (cmdl.hasOption('i')) {
        cmd = Command.install;
    } else if (cmdl.hasOption('b')) {
        cmd = Command.backup;
    } else if (cmdl.hasOption('r')) {
        cmd = Command.restore;
    } else if (cmdl.hasOption('f')) {
        cmd = Command.files;
    } else if (cmdl.hasOption('l')) {
        cmd = Command.ldap;
    }
    String file = cmdl.getOptionValue("file", "");
    switch(cmd) {
        case install:
            step = "Install";
            processInstall(file);
            break;
        case backup:
            step = "Backup";
            processBackup(file);
            break;
        case restore:
            step = "Restore";
            processRestore(checkRestoreFile(file));
            break;
        case files:
            step = "Files";
            processFiles();
            break;
        case ldap:
            step = "LDAP import";
            processLdap();
            break;
        case usage:
        default:
            usage();
            break;
    }
}
Also used : CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 100 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project java-jotasync by trixon.

the class Main method main.

/**
 * @param args the command line arguments
 * @throws java.rmi.RemoteException
 */
public static void main(String[] args) throws RemoteException {
    SystemHelper.enableRmiServer();
    Options options = initOptions();
    CommandLineParser parser = new DefaultParser();
    try {
        CommandLine cmd = parser.parse(options, args);
        if (cmd.hasOption(Main.OPT_HELP)) {
            displayHelp(options);
        } else if (cmd.hasOption(OPT_VERSION)) {
            displayVersion();
        } else {
            Client client = new Client(cmd);
        }
    } catch (ParseException ex) {
        Xlog.timedErr(ex.getMessage());
        System.out.println(sBundle.getString("parse_help_client"));
    }
}
Also used : 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)

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