Search in sources :

Example 1 with DynamicOptions

use of com.google.gerrit.server.DynamicOptions in project gerrit by GerritCodeReview.

the class ParameterParser method parse.

<T> boolean parse(T param, ListMultimap<String, String> in, HttpServletRequest req, HttpServletResponse res) throws IOException {
    CmdLineParser clp = parserFactory.create(param);
    DynamicOptions pluginOptions = new DynamicOptions(param, injector, dynamicBeans);
    pluginOptions.parseDynamicBeans(clp);
    pluginOptions.setDynamicBeans();
    pluginOptions.onBeanParseStart();
    try {
        clp.parseOptionMap(in);
    } catch (CmdLineException | NumberFormatException e) {
        if (!clp.wasHelpRequestedByOption()) {
            replyError(req, res, SC_BAD_REQUEST, e.getMessage(), e);
            return false;
        }
    }
    if (clp.wasHelpRequestedByOption()) {
        StringWriter msg = new StringWriter();
        clp.printQueryStringUsage(req.getRequestURI(), msg);
        msg.write('\n');
        msg.write('\n');
        clp.printUsage(msg, null);
        msg.write('\n');
        CacheHeaders.setNotCacheable(res);
        replyBinaryResult(req, res, BinaryResult.create(msg.toString()).setContentType("text/plain"));
        return false;
    }
    pluginOptions.onBeanParseEnd();
    return true;
}
Also used : DynamicOptions(com.google.gerrit.server.DynamicOptions) CmdLineParser(com.google.gerrit.util.cli.CmdLineParser) StringWriter(java.io.StringWriter) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 2 with DynamicOptions

use of com.google.gerrit.server.DynamicOptions in project gerrit by GerritCodeReview.

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);
    DynamicOptions pluginOptions = new DynamicOptions(options, injector, dynamicBeans);
    pluginOptions.parseDynamicBeans(clp);
    pluginOptions.setDynamicBeans();
    pluginOptions.onBeanParseStart();
    try {
        clp.parseArgument(argv);
    } catch (IllegalArgumentException | CmdLineException err) {
        if (!clp.wasHelpRequestedByOption()) {
            throw new UnloggedFailure(1, "fatal: " + err.getMessage());
        }
    }
    if (clp.wasHelpRequestedByOption()) {
        StringWriter msg = new StringWriter();
        clp.printDetailedUsage(commandName, msg);
        msg.write(usage());
        throw new UnloggedFailure(1, msg.toString());
    }
    pluginOptions.onBeanParseEnd();
}
Also used : DynamicOptions(com.google.gerrit.server.DynamicOptions) CmdLineParser(com.google.gerrit.util.cli.CmdLineParser) StringWriter(java.io.StringWriter) CmdLineException(org.kohsuke.args4j.CmdLineException)

Aggregations

DynamicOptions (com.google.gerrit.server.DynamicOptions)2 CmdLineParser (com.google.gerrit.util.cli.CmdLineParser)2 StringWriter (java.io.StringWriter)2 CmdLineException (org.kohsuke.args4j.CmdLineException)2