Search in sources :

Example 1 with CmdLineParser

use of com.gitblit.utils.cli.CmdLineParser in project gitblit by gitblit.

the class BaseCommand method parseCommandLine.

/**
	 * Parses the command line argument, injecting parsed values into fields.
	 * <p>
	 * This method must be explicitly invoked to cause a parse.
	 *
	 * @param options
	 *            object whose fields declare Option and Argument annotations to
	 *            describe the parameters of the command. Usually {@code this}.
	 * @throws UnloggedFailure
	 *             if the command line arguments were invalid.
	 * @see Option
	 * @see Argument
	 */
protected void parseCommandLine(Object options) throws UnloggedFailure {
    final CmdLineParser clp = newCmdLineParser(options);
    try {
        clp.parseArgument(argv);
    } catch (IllegalArgumentException err) {
        if (!clp.wasHelpRequestedByOption()) {
            throw new UnloggedFailure(1, "fatal: " + err.getMessage());
        }
    } catch (CmdLineException err) {
        if (!clp.wasHelpRequestedByOption()) {
            throw new UnloggedFailure(1, "fatal: " + err.getMessage());
        }
    }
    if (clp.wasHelpRequestedByOption()) {
        CommandMetaData meta = getClass().getAnnotation(CommandMetaData.class);
        String title = meta.name().toUpperCase() + ": " + meta.description();
        String b = com.gitblit.utils.StringUtils.leftPad("", title.length() + 2, '═');
        StringWriter msg = new StringWriter();
        msg.write('\n');
        msg.write(b);
        msg.write('\n');
        msg.write(' ');
        msg.write(title);
        msg.write('\n');
        msg.write(b);
        msg.write("\n\n");
        msg.write("USAGE\n");
        msg.write("─────\n");
        msg.write(' ');
        msg.write(commandName);
        msg.write('\n');
        msg.write("  ");
        clp.printSingleLineUsage(msg, null);
        msg.write("\n\n");
        String txt = getUsageText();
        if (!StringUtils.isEmpty(txt)) {
            msg.write(txt);
            msg.write("\n\n");
        }
        msg.write("ARGUMENTS & OPTIONS\n");
        msg.write("───────────────────\n");
        clp.printUsage(msg, null);
        msg.write('\n');
        String examples = usage().trim();
        if (!StringUtils.isEmpty(examples)) {
            msg.write('\n');
            msg.write("EXAMPLES\n");
            msg.write("────────\n");
            msg.write(examples);
            msg.write('\n');
        }
        throw new UnloggedFailure(1, msg.toString());
    }
}
Also used : CmdLineParser(com.gitblit.utils.cli.CmdLineParser) StringWriter(java.io.StringWriter) CmdLineException(org.kohsuke.args4j.CmdLineException)

Aggregations

CmdLineParser (com.gitblit.utils.cli.CmdLineParser)1 StringWriter (java.io.StringWriter)1 CmdLineException (org.kohsuke.args4j.CmdLineException)1