Example usage for org.apache.commons.cli2.util HelpFormatter HelpFormatter

List of usage examples for org.apache.commons.cli2.util HelpFormatter HelpFormatter

Introduction

In this page you can find the example usage for org.apache.commons.cli2.util HelpFormatter HelpFormatter.

Prototype

public HelpFormatter() 

Source Link

Document

Creates a new HelpFormatter using the defaults

Usage

From source file:com.gsinnovations.howdah.CommandLineUtil.java

public static void printHelp(Group group) {
    HelpFormatter formatter = new HelpFormatter();
    formatter.setGroup(group);
    formatter.print();
}

From source file:it.jnrpe.plugin.jmx.CCheckJMX.java

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

    CCheckJMX checkJMX = new CCheckJMX();
    Status status;
    try {
        // query.parse(args);
        // configure a parser
        PluginDefinition pluginDef = PluginRepositoryUtil.parseXmlPluginDefinition(
                JMXQuery.class.getClassLoader(), CCheckJMX.class.getResourceAsStream("/check_jmx_plugin.xml"));
        GroupBuilder gBuilder = new GroupBuilder();
        for (PluginOption po : pluginDef.getOptions()) {
            gBuilder = gBuilder.withOption(po.toOption());
        }
        HelpFormatter hf = new HelpFormatter();
        Parser cliParser = new Parser();
        cliParser.setGroup(gBuilder.create());
        cliParser.setHelpFormatter(hf);
        CommandLine cl = cliParser.parse(args);
        ReturnValue retValue = checkJMX.execute(new PluginCommandLine(cl));
        status = retValue.getStatus();
        System.out.println(retValue.getMessage());
    } catch (Exception ex) {
        status = checkJMX.report(ex, System.out);
    } finally {
        try {
            checkJMX.disconnect();
        } catch (IOException e) {
            status = checkJMX.report(e, System.out);
        }
    }
    System.exit(status.intValue());
}

From source file:com.gsinnovations.howdah.CommandLineUtil.java

/**
 * Print the options supported by <code>GenericOptionsParser</code>.
 * In addition to the options supported by the job, passed in as the
 * group parameter.//from   ww  w. j  av a  2s.  co  m
 *
 * @param group job-specific command-line options.
 */
public static void printHelpWithGenericOptions(Group group) {
    org.apache.commons.cli.Options ops = new org.apache.commons.cli.Options();
    new GenericOptionsParser(new Configuration(), ops, new String[0]);
    org.apache.commons.cli.HelpFormatter fmt = new org.apache.commons.cli.HelpFormatter();
    fmt.printHelp("<command> [Generic Options] [Job-Specific Options]", "Generic Options:", ops, "");

    PrintWriter pw = new PrintWriter(System.out);
    HelpFormatter formatter = new HelpFormatter();
    formatter.setGroup(group);
    formatter.setPrintWriter(pw);
    formatter.printHelp();
    pw.flush();
}

From source file:br.edu.ifpb.pos.command.CommandExecute.java

@Override
public void run() throws ResourceException {
    System.out.println("");

    if (line.hasOption("--help")) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.setGroup(options);
        helpFormatter.setDivider(" ");
        helpFormatter.getFullUsageSettings().remove(DisplaySetting.DISPLAY_GROUP_OUTER);
        helpFormatter.print();/*from   www.j  a v  a  2  s. co m*/
    }

    if (line.hasOption("--insert")) {
        Representation representation = new StringRepresentation((CharSequence) line.getValue("--insert"),
                MediaType.APPLICATION_JSON);
        ClientResource clientResource;
        if (line.getValue("--type").equals(TYPE_PERSON)) {
            clientResource = new ClientResource(URL + "/person");
        } else {
            clientResource = new ClientResource(URL + "/user");
        }
        try {
            clientResource.post(representation).write(System.out);
            System.out.println("");
        } catch (IOException ex) {
            Logger.getLogger(CommandExecute.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    if (line.hasOption("--update")) {
        String entity = (String) line.getValue("--update");
        Representation representation = new StringRepresentation(entity, MediaType.APPLICATION_JSON);
        ClientResource clientResource;
        Gson gson = new Gson();
        if (line.getValue("--type").equals(TYPE_PERSON)) {
            Key key = gson.fromJson(entity, Key.class);
            clientResource = new ClientResource(URL + "/person/" + key.getKey());
        } else {
            Key key = gson.fromJson(entity, Key.class);
            clientResource = new ClientResource(URL + "/user/" + key.getKey());
        }
        try {
            clientResource.put(representation).write(System.out);
            System.out.println("");
        } catch (IOException ex) {
            Logger.getLogger(CommandExecute.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    if (line.hasOption("--delete")) {
        ClientResource clientResource;
        String key = getKey(line.getValue("--delete").toString());

        if (line.getValue("--type").equals(TYPE_PERSON)) {
            clientResource = new ClientResource(URL + "/person/" + key);
        } else {
            clientResource = new ClientResource(URL + "/user/" + key);

        }
        try {
            clientResource.delete().write(System.out);
            System.out.println("");
        } catch (IOException ex) {
            Logger.getLogger(CommandExecute.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    if (line.hasOption("--select")) {
        ClientResource clientResource;
        String key = getKey(line.getValue("--select").toString());
        if (line.getValue("--type").equals(TYPE_PERSON)) {
            clientResource = new ClientResource(URL + "/person/" + key);
        } else {
            clientResource = new ClientResource(URL + "/user/" + key);
        }
        Representation representation = clientResource.get();
        try {
            System.out.println(representation.getText());
        } catch (IOException ex) {
            Logger.getLogger(CommandExecute.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:de.tu_chemnitz.mi.barcd.app.CommandLineArgumentsParser.java

/**
 * Create the command line parser./*from  w w  w .  java  2  s  .  c o m*/
 *
 * @return a command line parser
 */
private Parser createParser() {
    GroupBuilder gb = new GroupBuilder();

    Parser parser = new Parser();

    DefaultOption helpOption = createHelpOption();
    parser.setHelpOption(helpOption);

    Group group = gb.withOption(createJobFileOption()).withOption(createXmlSchemaOption())
            .withOption(createDisplaySwitch()).withOption(createPersistSwitch()).withOption(helpOption)
            .create();
    parser.setGroup(group);

    HelpFormatter hf = new HelpFormatter();
    parser.setHelpFormatter(hf);

    return parser;
}

From source file:egat.cli.Main.java

protected static void printHelp(Group options) {
    HelpFormatter hf = new HelpFormatter();
    hf.setShellCommand("egat");
    hf.setGroup(options);//from   w w  w  .j  a  v a  2  s .  c  o  m

    hf.getFullUsageSettings().add(DisplaySetting.DISPLAY_GROUP_NAME);
    hf.getFullUsageSettings().remove(DisplaySetting.DISPLAY_GROUP_ARGUMENT);
    hf.getFullUsageSettings().remove(DisplaySetting.DISPLAY_GROUP_EXPANDED);
    hf.getFullUsageSettings().remove(DisplaySetting.DISPLAY_OPTIONAL_CHILD_GROUP);
    hf.setFooter("egat is developed by Patrick R. Jordan (prjordan@umich.edu).");
    hf.print();
}

From source file:it.jnrpe.plugins.PluginProxy.java

/**
 * Executes the proxied plugin passing the received arguments as parameters.
 * /*w  ww . j ava  2s .  c o  m*/
 * @param argsAry
 *            The parameters to be passed to the plugin
        
        
 * @return The return value of the plugin. * @throws BadThresholdException
 *             - */
public ReturnValue execute(final String[] argsAry) throws BadThresholdException {
    // CommandLineParser clp = new PosixParser();
    try {
        HelpFormatter hf = new HelpFormatter();
        // configure a parser
        Parser cliParser = new Parser();
        cliParser.setGroup(mainOptionsGroup);
        cliParser.setHelpFormatter(hf);
        CommandLine cl = cliParser.parse(argsAry);

        // Inject the context...
        InjectionUtils.inject(proxiedPlugin, getContext());

        Thread.currentThread().setContextClassLoader(proxiedPlugin.getClass().getClassLoader());

        ReturnValue retValue = proxiedPlugin.execute(new PluginCommandLine(cl));

        if (retValue == null) {
            String msg = "Plugin [" + getPluginName() + "] with args [" + StringUtils.join(argsAry)
                    + "] returned null";

            retValue = new ReturnValue(Status.UNKNOWN, msg);
        }

        return retValue;
    } catch (BadThresholdException bte) {
        throw bte;
    } catch (OptionException e) {

        String msg = e.getMessage();
        LOG.error(getContext(), "Error parsing plugin '" + getPluginName() + "' command line : " + msg, e);
        return new ReturnValue(Status.UNKNOWN, msg);
    }
}

From source file:egat.cli.AbstractGameCommandHandler.java

protected void printHelp() {
    HelpFormatter hf = new HelpFormatter();
    hf.setShellCommand("egat " + getCommandName());
    hf.setGroup(getCommand().getChildren());

    hf.getFullUsageSettings().add(DisplaySetting.DISPLAY_GROUP_NAME);
    hf.getFullUsageSettings().remove(DisplaySetting.DISPLAY_GROUP_ARGUMENT);
    hf.getFullUsageSettings().remove(DisplaySetting.DISPLAY_GROUP_EXPANDED);
    hf.getFullUsageSettings().remove(DisplaySetting.DISPLAY_OPTIONAL_CHILD_GROUP);
    hf.setFooter("egat is developed by Patrick R. Jordan (prjordan@umich.edu).");
    hf.print();/*  ww  w  .  j a v  a 2  s.co m*/
}

From source file:it.jnrpe.server.JNRPEServer.java

/**
 * Parses the command line./*from w ww. j ava  2  s .co  m*/
 * 
 * @param vsArgs
 *            The command line
 * @return The parsed command line
 */
private static CommandLine parseCommandLine(final String[] vsArgs) {
    try {
        Group opts = configureCommandLine();
        // configure a HelpFormatter
        HelpFormatter hf = new HelpFormatter();

        // configure a parser
        Parser p = new Parser();
        p.setGroup(opts);
        p.setHelpFormatter(hf);
        // p.setHelpTrigger("--help");
        return p.parse(vsArgs);
    } catch (OptionException oe) {
        printUsage(oe);
    } catch (Exception e) {
        e.printStackTrace();
        // Should never happen...
    }
    return null;
}

From source file:it.jnrpe.plugins.PluginProxy.java

/**
 * Prints the help related to the plugin to a specified output.
 * // w  w w  .ja va 2 s  . com
 * @param out
 *            the writer where the help should be written
 */
public void printHelp(final PrintWriter out) {
    HelpFormatter hf = new HelpFormatter();
    StringBuilder sbDivider = new StringBuilder("=");
    while (sbDivider.length() < hf.getPageWidth()) {
        sbDivider.append('=');
    }
    out.println(sbDivider.toString());
    out.println("PLUGIN NAME : " + proxyedPluginDefinition.getName());
    if (description != null && description.trim().length() != 0) {
        out.println(sbDivider.toString());
        out.println("Description : ");
        out.println();
        out.println(description);
    }

    hf.setGroup(mainOptionsGroup);
    // hf.setHeader(m_pluginDef.getName());
    hf.setDivider(sbDivider.toString());
    hf.setPrintWriter(out);
    hf.print();
    // hf.printHelp(m_pluginDef.getName(), m_Options);
}