Example usage for org.apache.commons.cli HelpFormatter printHelp

List of usage examples for org.apache.commons.cli HelpFormatter printHelp

Introduction

In this page you can find the example usage for org.apache.commons.cli HelpFormatter printHelp.

Prototype

public void printHelp(String cmdLineSyntax, Options options) 

Source Link

Document

Print the help for options with the specified command line syntax.

Usage

From source file:com.sourcethought.simpledaemon.EchoTask.java

public static void main(String[] args) throws Exception {
    // setup command line options
    Options options = new Options();
    options.addOption("h", "help", false, "print this help screen");

    // read command line options
    CommandLineParser parser = new PosixParser();
    try {/* w  w w.  j  a v  a  2s.  c om*/
        CommandLine cmdline = parser.parse(options, args);

        if (cmdline.hasOption("help") || cmdline.hasOption("h")) {
            System.out.println("SimpleDaemon Version " + Main.version);

            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(
                    "java -cp lib/*:target/SimpleDaemon-1.0-SNAPSHOT.jar com.sourcethought.simpledaemon.Main",
                    options);
            return;
        }

        final SimpleDaemon daemon = new SimpleDaemon();
        daemon.start();

        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                if (daemon.isRunning()) {
                    try {
                        System.out.println("Shutting down daemon...");
                        daemon.stop();
                    } catch (Exception ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }));

    } catch (ParseException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.twitter.bazel.checkstyle.JavaCheckstyle.java

public static void main(String[] args) throws IOException {
    CommandLineParser parser = new DefaultParser();

    // create the Options
    Options options = new Options();
    options.addOption(Option.builder("f").required(true).hasArg().longOpt("extra_action_file")
            .desc("bazel extra action protobuf file").build());
    options.addOption(Option.builder("c").required(true).hasArg().longOpt("checkstyle_config_file")
            .desc("checkstyle config file").build());

    try {// ww w.  j a v a2s . c o  m
        // parse the command line arguments
        CommandLine line = parser.parse(options, args);

        String extraActionFile = line.getOptionValue("f");
        String configFile = line.getOptionValue("c");

        String[] sourceFiles = getSourceFiles(extraActionFile);
        if (sourceFiles.length == 0) {
            LOG.fine("No java files found by checkstyle");
            return;
        }

        LOG.fine(sourceFiles.length + " java files found by checkstyle");

        String[] checkstyleArgs = (String[]) ArrayUtils.addAll(new String[] { "-c", configFile }, sourceFiles);

        LOG.fine("checkstyle args: " + Joiner.on(" ").join(checkstyleArgs));
        com.puppycrawl.tools.checkstyle.Main.main(checkstyleArgs);
    } catch (ParseException exp) {
        LOG.severe(String.format("Invalid input to %s: %s", CLASSNAME, exp.getMessage()));
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java " + CLASSNAME, options);
    }
}

From source file:edu.mit.csail.sdg.alloy4.VizGUI.java

/**
 * @param args//from  ww  w  . j ava2  s  .com
 */
public static void main(String[] args) {
    // create the command line parser
    CommandLineParser parser = new PosixParser();

    // create the Options
    Options optionsArg = new Options();
    Option helpOpt = new Option("?", "help", false, "print this message");
    Option inputFileOpt = new Option("f", "file", true, "the name of the xml file to show");
    inputFileOpt.setRequired(true);

    optionsArg.addOption(helpOpt);
    optionsArg.addOption(inputFileOpt);

    CommandLine line = null;
    try {
        // parse the command line arguments
        line = parser.parse(optionsArg, args);

        if (line.hasOption(helpOpt.getOpt())) {
            // automatically generate the help statement
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("vizgui", optionsArg);
            return;
        }

    } catch (ParseException exp) {
        System.err.println("Unexpected exception:" + exp.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("vizgui", optionsArg);
        return;
    }

    String inputFile = null;
    if (line.hasOption(inputFileOpt.getOpt())) {
        inputFile = line.getOptionValue(inputFileOpt.getOpt());
        if (inputFile == null) {
            System.err.println("Invalid input file");
        }
    }

    new edu.mit.csail.sdg.alloy4viz.VizGUI(true, inputFile, null);

}

From source file:com.ctriposs.rest4j.tools.snapshot.gen.Rest4JSnapshotExporterCmdLineApp.java

/**
 * @param args rest4jexporter -sourcepath sourcepath -resourcepackages packagenames [-name api_name] [-outdir outdir]
 *//*from   w  w  w  . jav  a2  s . co  m*/
public static void main(String[] args) {
    CommandLine cl = null;
    try {
        final CommandLineParser parser = new GnuParser();
        cl = parser.parse(OPTIONS, args);
    } catch (ParseException e) {
        System.err.println("Invalid arguments: " + e.getMessage());
        final HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(
                "rest4jexporter -sourcepath sourcepath [-resourcepackages packagenames] [-resourceclasses classnames]"
                        + "[-name api_name] [-outdir outdir]",
                OPTIONS);
        System.exit(0);
    }

    final String resolverPath = System.getProperty(AbstractGenerator.GENERATOR_RESOLVER_PATH);

    try {
        final Rest4JSnapshotExporter exporter = new Rest4JSnapshotExporter();
        exporter.setResolverPath(resolverPath);
        exporter.export(cl.getOptionValue("name"), null, cl.getOptionValues("sourcepath"),
                cl.getOptionValues("resourcepackages"), cl.getOptionValues("resourceClasses"),
                cl.getOptionValue("outdir", "."));
    } catch (Throwable e) {
        log.error("Error writing Snapshot files", e);
        System.out.println("Error writing Snapshot files:\n" + e);
        System.exit(1);
    }
}

From source file:com.trovit.hdfstree.HdfsTree.java

public static void main(String... args) {
    Options options = new Options();
    options.addOption("l", false, "Use local filesystem.");
    options.addOption("p", true, "Path used as root for the tree.");
    options.addOption("s", false, "Display the size of the directory");
    options.addOption("d", true, "Maximum depth of the tree (when displaying)");

    CommandLineParser parser = new PosixParser();

    TreeBuilder treeBuilder;/*from   w ww. j a va2  s  .co  m*/
    FSInspector fsInspector = null;
    String rootPath = null;

    Displayer displayer = new ConsoleDisplayer();

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

        // local or hdfs.
        if (cmd.hasOption("l")) {
            fsInspector = new LocalFSInspector();
        } else {
            fsInspector = new HDFSInspector();
        }

        // check that it has the root path.
        if (cmd.hasOption("p")) {
            rootPath = cmd.getOptionValue("p");
        } else {
            throw new ParseException("Mandatory option (-p) is not specified.");
        }

        if (cmd.hasOption("d")) {
            displayer.setMaxDepth(Integer.parseInt(cmd.getOptionValue("d")));
        }

        if (cmd.hasOption("s")) {
            displayer.setDisplaySize();
        }
    } catch (ParseException e) {
        System.out.println(e.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("hdfstree", options);
        System.exit(1);
    }

    treeBuilder = new TreeBuilder(rootPath, fsInspector);
    TreeNode tree = treeBuilder.buildTree();
    displayer.display(tree);

}

From source file:ezbake.thrift.VisibilityBase64Converter.java

public static void main(String[] args) {
    try {/*from w w w .  j  a  v a2 s  .co  m*/
        Options options = new Options();
        options.addOption("d", "deserialize", false,
                "deserialize base64 visibility, if not present tool will serialize");
        options.addOption("v", "visibility", true,
                "formalVisibility to serialize, or base64 encoded string to deserialize");
        options.addOption("h", "help", false, "display usage info");

        CommandLineParser parser = new BasicParser();
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("h")) {
            HelpFormatter helpFormatter = new HelpFormatter();
            helpFormatter.printHelp("thrift-visibility-converter", options);
            return;
        }

        String input = cmd.getOptionValue("v");
        boolean deserialize = cmd.hasOption("d");

        String output;
        if (deserialize) {
            if (input == null) {
                System.err.println("Cannot deserialize empty string");
                System.exit(1);
            }
            output = ThriftUtils.deserializeFromBase64(Visibility.class, input).toString();
        } else {
            Visibility visibility = new Visibility();
            if (input == null) {
                input = "(empty visibility)";
            } else {
                visibility.setFormalVisibility(input);
            }
            output = ThriftUtils.serializeToBase64(visibility);
        }
        String operation = deserialize ? "deserialize" : "serialize";
        System.out.println("operation: " + operation);
        System.out.println("input: " + input);
        System.out.println("output: " + output);
    } catch (TException | ParseException e) {
        System.out.println("An error occurred: " + e.getMessage());
        System.exit(1);
    }
}

From source file:com.ovea.tajin.server.ContainerRunner.java

public static void main(String... args) throws Exception {
    CommandLineParser parser = new GnuParser();
    CommandLine line = parser.parse(options, args);

    if (line.hasOption("help") || !line.hasOption(Properties.CONTAINER.getName())) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(ContainerRunner.class.getSimpleName(), options);
        return;//from www .j a  v  a  2  s .  c o m
    }

    // Set default values
    // And override by system properties. This way, we can start ContainerRunner with specific options such as -DjettyConf=.. -DjettyEnv=...
    ContainerConfiguration settings = ContainerConfiguration.from(System.getProperties());
    // Then parse command line
    for (Option option : line.getOptions()) {
        settings.set(Properties.from(option.getOpt()), line.getOptionValue(option.getOpt()));
    }
    Container container = settings.buildContainer(line.getOptionValue(Properties.CONTAINER.getName()));
    container.start();
}

From source file:cc.wikitools.lucene.ScoreWikipediaArticle.java

@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption(//  w  w w .  j av a  2 s  . c o m
            OptionBuilder.withArgName("path").hasArg().withDescription("index location").create(INDEX_OPTION));
    options.addOption(
            OptionBuilder.withArgName("num").hasArg().withDescription("article id").create(ID_OPTION));
    options.addOption(
            OptionBuilder.withArgName("string").hasArg().withDescription("article title").create(TITLE_OPTION));
    options.addOption(
            OptionBuilder.withArgName("string").hasArg().withDescription("query text").create(QUERY_OPTION));

    CommandLine cmdline = null;
    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(ID_OPTION) || cmdline.hasOption(TITLE_OPTION)) || !cmdline.hasOption(INDEX_OPTION)
            || !cmdline.hasOption(QUERY_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(ScoreWikipediaArticle.class.getName(), options);
        System.exit(-1);
    }

    File indexLocation = new File(cmdline.getOptionValue(INDEX_OPTION));
    if (!indexLocation.exists()) {
        System.err.println("Error: " + indexLocation + " does not exist!");
        System.exit(-1);
    }

    String queryText = cmdline.getOptionValue(QUERY_OPTION);

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

    if (cmdline.hasOption(ID_OPTION)) {
        out.println("score: "
                + searcher.scoreArticle(queryText, Integer.parseInt(cmdline.getOptionValue(ID_OPTION))));
    } else {
        out.println("score: " + searcher.scoreArticle(queryText, cmdline.getOptionValue(TITLE_OPTION)));
    }

    searcher.close();
    out.close();
}

From source file:com.github.jjYBdx4IL.utils.lang.ConstantClassCreator.java

public static void main(String[] args) {
    try {//from  w  w  w . j av  a  2  s . com
        CommandLineParser parser = new GnuParser();
        Options options = new Options();
        options.addOption(OPTNAME_H, OPTNAME_HELP, false, "show help (this page)");
        options.addOption(null, OPTNAME_SYSPROP, true, "system property name(s)");
        options.addOption(null, OPTNAME_OUTPUT_CLASSNAME, true, "output classname");
        options.addOption(null, OPTNAME_OUTPUT_DIRECTORY, true, "output directory");
        CommandLine line = parser.parse(options, args);
        if (line.hasOption(OPTNAME_H)) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(ConstantClassCreator.class.getName(), options);
        } else if (line.hasOption(OPTNAME_SYSPROP)) {
            new ConstantClassCreator().run(line);
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.svds.tailer2kafka.main.Main.java

/**
 * Given command-line arguments, create GenericConsumerGroup
 * //from   w w  w .j av a  2s .  co m
 * @param args  command-line arguments to parse
 * @throws IOException 
 */
public static void main(String[] args) throws IOException, InterruptedException {

    Options options = new Options();

    options.addOption(OptionBuilder.isRequired().withLongOpt(TOPICNAME).withDescription("Kafka topic name")
            .hasArg().create());

    options.addOption(OptionBuilder.isRequired().withLongOpt(METADATABROKERLIST)
            .withDescription("Kafka metadata.broker.list").hasArg().create());

    options.addOption(
            OptionBuilder.isRequired().withLongOpt(FILENAME).withDescription("Log filename").hasArg().create());

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

    } catch (ParseException e) {
        LOG.error(e.getMessage(), e);
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("com.svds.tailer2kafka.main.Main", options);

        throw new IOException("Missing Args");
    }

    Main main = new Main();
    main.doWork(cmd);
}