Example usage for org.apache.commons.cli CommandLineParser parse

List of usage examples for org.apache.commons.cli CommandLineParser parse

Introduction

In this page you can find the example usage for org.apache.commons.cli CommandLineParser parse.

Prototype

CommandLine parse(Options options, String[] arguments) throws ParseException;

Source Link

Document

Parse the arguments according to the specified options.

Usage

From source file:edu.usc.pgroup.floe.client.commands.KillApp.java

/**
 * Entry point for Scale command./*w ww . j ava  2s . c o  m*/
 * @param args command line arguments sent by the floe.py script.
 */
public static void main(final String[] args) {

    Options options = new Options();

    Option appOption = OptionBuilder.withArgName("name").hasArg().isRequired()
            .withDescription("Application Name").create("app");

    options.addOption(appOption);

    CommandLineParser parser = new BasicParser();
    CommandLine line;

    try {
        line = parser.parse(options, args);

    } catch (ParseException e) {
        LOGGER.error("Invalid command: " + e.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("scale options", options);
        return;
    }

    String app = line.getOptionValue("app");

    LOGGER.info("Application: {}", app);

    try {
        FloeClient.getInstance().killApp(app);
    } catch (TException e) {
        LOGGER.error("Error while connecting to the coordinator: {}", e);
    }
}

From source file:com.epitech.oliver_f.astextexls.MainClass.java

public static void main(String[] args) {
    initalizeOption();//w  ww .jav a  2  s.c om
    CommandLineParser parser = new BasicParser();
    CommandLine cl = null;
    try {
        cl = parser.parse(options, args);
    } catch (ParseException ex) {
        Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (cl != null && cl.hasOption("help")) {
        HelpFormatter hf = new HelpFormatter();
        hf.printHelp("astexte script", options);
    }
    if (cl.hasOption("folder") && cl.hasOption("file")) {
        launchWriteAndRead(cl.getOptionValue("folder"), cl.getOptionValue("file"));
        System.out.println("OK");

    } else {
        HelpFormatter hf = new HelpFormatter();
        hf.printHelp("astexte script", options);
    }
    launch(args);
}

From source file:eu.itesla_project.commons.tools.Main.java

public static void main(String[] args) throws IOException {
    if (args.length < 1) {
        printUsage();//from   www  .  j  av a  2  s.  c o  m
    }

    Tool tool = findTool(args[0]);
    if (tool == null) {
        printUsage();
    }

    try {
        CommandLineParser parser = new PosixParser();
        CommandLine line = parser.parse(getOptionsWithHelp(tool.getCommand().getOptions()),
                Arrays.copyOfRange(args, 1, args.length));
        if (line.hasOption("help")) {
            printCommandUsage(tool.getCommand());
        } else {
            tool.run(line);
        }
    } catch (ParseException e) {
        System.err.println("error: " + e.getMessage());
        printCommandUsage(tool.getCommand());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:info.bitoo.utils.BiToorrentRemaker.java

public static void main(String[] args) throws IOException, TOTorrentException {
    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;//from   w w  w. java2s .c  om
    try {
        cmd = parser.parse(createCommandLineOptions(), args);
    } catch (ParseException e) {
        System.err.println("Parsing failed.  Reason: " + e.getMessage());
    }

    StringTokenizer stLocations = new StringTokenizer(cmd.getOptionValue("l"), "|");
    List locations = new ArrayList(stLocations.countTokens());

    while (stLocations.hasMoreTokens()) {
        URL locationURL = new URL((String) stLocations.nextToken());
        locations.add(locationURL.toString());
    }

    String torrentFileName = cmd.getOptionValue("t");

    File torrentFile = new File(torrentFileName);

    TOTorrentDeserialiseImpl torrent = new TOTorrentDeserialiseImpl(torrentFile);

    torrent.setAdditionalListProperty("alternative locations", locations);

    torrent.serialiseToBEncodedFile(torrentFile);

}

From source file:io.github.azige.mages.Cli.java

public static void main(String[] args) {

    Options options = new Options().addOption("h", "help", false, "print this message")
            .addOption(Option.builder("r").hasArg().argName("bundle").desc("set the ResourceBundle").build())
            .addOption(Option.builder("l").hasArg().argName("locale").desc("set the locale").build())
            .addOption(Option.builder("t").hasArg().argName("template").desc("set the template").build())
            .addOption(Option.builder("p").hasArg().argName("plugin dir")
                    .desc("set the directory to load plugins").build())
            .addOption(Option.builder("f").desc("force override existed file").build());
    try {/*from   ww w. j a  va2s.  c o  m*/
        CommandLineParser parser = new DefaultParser();
        CommandLine cl = parser.parse(options, args);

        if (cl.hasOption('h')) {
            printHelp(System.out, options);
            return;
        }

        MagesSiteGenerator msg = new MagesSiteGenerator(new File("."), new File("."));

        String[] fileArgs = cl.getArgs();
        if (fileArgs.length < 1) {
            msg.addTask(".");
        } else {
            for (String path : fileArgs) {
                msg.addTask(path);
            }
        }

        if (cl.hasOption("t")) {
            msg.setTemplate(new File(cl.getOptionValue("t")));
        }

        if (cl.hasOption("r")) {
            msg.setResource(new File(cl.getOptionValue("r")));
        } else {
            File resource = new File("Resource.properties");
            if (resource.exists()) {
                msg.setResource(resource);
            }
        }

        if (cl.hasOption("p")) {
            msg.setPluginDir(new File(cl.getOptionValue("p")));
        }

        if (cl.hasOption("f")) {
            msg.setForce(true);
        }

        msg.start();
    } catch (ParseException ex) {
        System.err.println(ex.getMessage());
        printHelp(System.err, options);
    }
}

From source file:com.amit.api.compiler.App.java

public static void main(String[] args) {
    Options options = createOptions();//from w  ww . j a va 2 s .c  o m

    CommandLineParser parser = new GnuParser();
    try {
        // parse the command line arguments
        CommandLine cmd = parser.parse(options, args);
        execute(cmd);
    } catch (ParseException exp) {
        System.out.println("Invalid arguments.  Reason: " + exp.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("args", options);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.google.flightmap.parsing.faa.nfd.NfdParser.java

public static void main(String args[]) {
    CommandLine line = null;//  w ww .  j  a v a 2s.  c o m
    try {
        final CommandLineParser parser = new PosixParser();
        line = parser.parse(OPTIONS, args);
    } catch (ParseException pEx) {
        System.err.println(pEx.getMessage());
        printHelp(line);
        System.exit(1);
    }

    if (line.hasOption(HELP_OPTION)) {
        printHelp(line);
        System.exit(0);
    }

    final String nfdPath = line.getOptionValue(NFD_OPTION);
    final File nfd = new File(nfdPath);
    final String iataToIcaoPath = line.getOptionValue(IATA_TO_ICAO_OPTION);
    final File iataToIcao = new File(iataToIcaoPath);

    (new NfdParser(nfd, iataToIcao)).execute();
}

From source file:de.akadvh.view.Main.java

/**
 * @param args/* w  ww . j av a  2  s  .c o  m*/
 */
public static void main(String[] args) {

    Options options = new Options();
    options.addOption("u", "user", true, "Benutzername");
    options.addOption("p", "pass", true, "Passwort");
    options.addOption("c", "console", false, "Consolenmodus");
    options.addOption("v", "verbose", false, "Mehr Ausgabe");
    options.addOption("m", "modul", true, "Modul");
    options.addOption("n", "noten", false, "Notenuebersicht erstellen");
    options.addOption("t", "termin", false, "Terminuebersicht (angemeldete Module) downloaden");
    options.addOption("version", false, "Version");
    options.addOption("h", "help", false, "Hilfe");

    CommandLineParser parser = new PosixParser();
    try {
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("help")) {

            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("java -jar akadvh.jar", options);
            System.exit(0);
        }

        if (cmd.hasOption("version")) {

            System.out.println("Akadvh Version: " + Akadvh.getVersion());
            System.exit(0);

        }

        if (cmd.hasOption("console")) {

            ConsoleView cv = new ConsoleView(cmd.getOptionValue("user"), cmd.getOptionValue("pass"),
                    cmd.getOptionValue("modul"), cmd.hasOption("noten"), cmd.hasOption("termin"),
                    cmd.hasOption("verbose"));

        } else {

            SwingView sv = new SwingView(cmd.getOptionValue("user"), cmd.getOptionValue("pass"));

        }

    } catch (UnrecognizedOptionException e1) {
        System.out.println(e1.getMessage());
        System.out.println("--help fuer Hilfe");
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

From source file:edu.washington.data.sentimentreebank.StanfordNLPDict.java

public static void main(String args[]) {
    Options options = new Options();
    options.addOption("d", "dict", true, "dictionary file.");
    options.addOption("s", "sentiment", true, "sentiment value file.");

    CommandLineParser parser = new GnuParser();
    try {//from www.j  a v a  2  s  .c  o  m
        CommandLine line = parser.parse(options, args);
        if (!line.hasOption("dict") && !line.hasOption("sentiment")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("StanfordNLPDict", options);
            return;
        }

        Path dictPath = Paths.get(line.getOptionValue("dict"));
        Path sentimentPath = Paths.get(line.getOptionValue("sentiment"));

        StanfordNLPDict snlp = new StanfordNLPDict(dictPath, sentimentPath);
        String sentence = "take off";
        System.out.printf("sentence [%1$s] %2$s\n", sentence,
                String.valueOf(snlp.getPhraseSentiment(sentence)));

    } catch (ParseException exp) {
        System.err.println("Parsing failed.  Reason: " + exp.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("StanfordNLPDict", options);
    } catch (IOException ex) {
        Logger.getLogger(StanfordNLPDict.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:cc.twittertools.index.ExtractTermStatisticsFromIndex.java

@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
    Options options = new Options();

    options.addOption(OptionBuilder.withArgName("dir").hasArg().withDescription("index").create(INDEX_OPTION));
    options.addOption(OptionBuilder.withArgName("num").hasArg().withDescription("min").create(MIN_OPTION));

    CommandLine cmdline = null;// w  w  w  .  j  a v a2 s  .c o m
    CommandLineParser parser = new GnuParser();
    try {
        cmdline = parser.parse(options, args);
    } catch (ParseException exp) {
        System.err.println("Error parsing command line: " + exp.getMessage());
        System.exit(-1);
    }

    if (!cmdline.hasOption(INDEX_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(ExtractTermStatisticsFromIndex.class.getName(), options);
        System.exit(-1);
    }

    String indexLocation = cmdline.getOptionValue(INDEX_OPTION);
    int min = cmdline.hasOption(MIN_OPTION) ? Integer.parseInt(cmdline.getOptionValue(MIN_OPTION)) : 1;

    PrintStream out = new PrintStream(System.out, true, "UTF-8");

    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexLocation)));
    Terms terms = SlowCompositeReaderWrapper.wrap(reader).terms(StatusField.TEXT.name);
    TermsEnum termsEnum = terms.iterator(TermsEnum.EMPTY);

    long missingCnt = 0;
    int skippedTerms = 0;
    BytesRef bytes = new BytesRef();
    while ((bytes = termsEnum.next()) != null) {
        byte[] buf = new byte[bytes.length];
        System.arraycopy(bytes.bytes, 0, buf, 0, bytes.length);
        String term = new String(buf, "UTF-8");
        int df = termsEnum.docFreq();
        long cf = termsEnum.totalTermFreq();

        if (df < min) {
            skippedTerms++;
            missingCnt += cf;
            continue;
        }

        out.println(term + "\t" + df + "\t" + cf);
    }

    reader.close();
    out.close();
    System.err.println("skipped terms: " + skippedTerms + ", cnt: " + missingCnt);
}