Search in sources :

Example 1 with ParameterException

use of picocli.CommandLine.ParameterException in project keycloak by keycloak.

the class PropertyMapperParameterConsumer method validateOption.

private void validateOption(Stack<String> args, ArgSpec argSpec, CommandSpec commandSpec) {
    OptionSpec option = (OptionSpec) argSpec;
    String name = String.join(", ", option.names());
    CommandLine commandLine = commandSpec.commandLine();
    if (args.isEmpty() || !isOptionValue(args.peek())) {
        throw new ParameterException(commandLine, "Missing required value for option '" + name + "' (" + argSpec.paramLabel() + ")." + getExpectedValuesMessage(argSpec, option));
    }
    // consumes the value
    String value = args.pop();
    if (!args.isEmpty() && isOptionValue(args.peek())) {
        throw new ParameterException(commandLine, "Option '" + name + "' expects a single value (" + argSpec.paramLabel() + ")" + getExpectedValuesMessage(argSpec, option));
    }
    if (isExpectedValue(option, value)) {
        return;
    }
    throw new ParameterException(commandLine, "Invalid value for option '" + name + "': " + value + "." + getExpectedValuesMessage(argSpec, option));
}
Also used : OptionSpec(picocli.CommandLine.Model.OptionSpec) CommandLine(picocli.CommandLine) ParameterException(picocli.CommandLine.ParameterException)

Example 2 with ParameterException

use of picocli.CommandLine.ParameterException in project groovy by apache.

the class GroovyMain method processArgs.

// package-level visibility for testing purposes (just usage/errors at this stage)
static void processArgs(String[] args, final PrintStream out, final PrintStream err) {
    GroovyCommand groovyCommand = new GroovyCommand();
    CommandLine parser = new CommandLine(groovyCommand).setOut(new PrintWriter(out)).setErr(new PrintWriter(err)).setUnmatchedArgumentsAllowed(true).setStopAtUnmatched(true);
    try {
        ParseResult result = parser.parseArgs(args);
        if (CommandLine.printHelpIfRequested(result)) {
            return;
        }
        // TODO: pass printstream(s) down through process
        if (!groovyCommand.process(parser)) {
            // If we fail, then exit with an error so scripting frameworks can catch it.
            System.exit(1);
        }
    } catch (ParameterException ex) {
        // command line arguments could not be parsed
        err.println(ex.getMessage());
        ex.getCommandLine().usage(err);
    } catch (IOException ioe) {
        err.println("error: " + ioe.getMessage());
    }
}
Also used : CommandLine(picocli.CommandLine) ParseResult(picocli.CommandLine.ParseResult) ParameterException(picocli.CommandLine.ParameterException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 3 with ParameterException

use of picocli.CommandLine.ParameterException in project checkstyle by checkstyle.

the class Main method main.

/**
 * Loops over the files specified checking them for errors. The exit code
 * is the number of errors found in all the files.
 *
 * @param args the command line arguments.
 * @throws IOException if there is a problem with files access
 * @noinspection UseOfSystemOutOrSystemErr, CallToPrintStackTrace, CallToSystemExit
 */
public static void main(String... args) throws IOException {
    final CliOptions cliOptions = new CliOptions();
    final CommandLine commandLine = new CommandLine(cliOptions);
    commandLine.setUsageHelpWidth(CliOptions.HELP_WIDTH);
    commandLine.setCaseInsensitiveEnumValuesAllowed(true);
    // provide proper exit code based on results.
    int exitStatus = 0;
    int errorCounter = 0;
    try {
        final ParseResult parseResult = commandLine.parseArgs(args);
        if (parseResult.isVersionHelpRequested()) {
            System.out.println(getVersionString());
        } else if (parseResult.isUsageHelpRequested()) {
            commandLine.usage(System.out);
        } else {
            exitStatus = execute(parseResult, cliOptions);
            errorCounter = exitStatus;
        }
    } catch (ParameterException ex) {
        exitStatus = EXIT_WITH_INVALID_USER_INPUT_CODE;
        System.err.println(ex.getMessage());
        System.err.println("Usage: checkstyle [OPTIONS]... FILES...");
        System.err.println("Try 'checkstyle --help' for more information.");
    } catch (CheckstyleException ex) {
        exitStatus = EXIT_WITH_CHECKSTYLE_EXCEPTION_CODE;
        errorCounter = 1;
        ex.printStackTrace();
    } finally {
        // return exit code base on validation of Checker
        if (errorCounter > 0) {
            final Violation errorCounterViolation = new Violation(1, Definitions.CHECKSTYLE_BUNDLE, ERROR_COUNTER, new String[] { String.valueOf(errorCounter) }, null, Main.class, null);
            // print error count statistic to error output stream,
            // output stream might be used by validation report content
            System.err.println(errorCounterViolation.getViolation());
        }
    }
    Runtime.getRuntime().exit(exitStatus);
}
Also used : Violation(com.puppycrawl.tools.checkstyle.api.Violation) CommandLine(picocli.CommandLine) ParseResult(picocli.CommandLine.ParseResult) ParameterException(picocli.CommandLine.ParameterException) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException)

Example 4 with ParameterException

use of picocli.CommandLine.ParameterException in project checkstyle by checkstyle.

the class JavadocPropertiesGenerator method main.

/**
 * TokenTypes.properties generator entry point.
 *
 * @param args the command line arguments
 * @throws CheckstyleException if parser or lexer failed or if there is an IO problem
 */
public static void main(String... args) throws CheckstyleException {
    final CliOptions cliOptions = new CliOptions();
    final CommandLine cmd = new CommandLine(cliOptions).setUsageHelpWidth(USAGE_HELP_WIDTH);
    try {
        final ParseResult parseResult = cmd.parseArgs(args);
        if (parseResult.isUsageHelpRequested()) {
            cmd.usage(System.out);
        } else {
            writePropertiesFile(cliOptions);
        }
    } catch (ParameterException ex) {
        System.err.println(ex.getMessage());
        ex.getCommandLine().usage(System.err);
    }
}
Also used : CommandLine(picocli.CommandLine) ParseResult(picocli.CommandLine.ParseResult) ParameterException(picocli.CommandLine.ParameterException)

Aggregations

CommandLine (picocli.CommandLine)4 ParameterException (picocli.CommandLine.ParameterException)4 ParseResult (picocli.CommandLine.ParseResult)3 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)1 Violation (com.puppycrawl.tools.checkstyle.api.Violation)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 OptionSpec (picocli.CommandLine.Model.OptionSpec)1