Search in sources :

Example 1 with ParseResult

use of picocli.CommandLine.ParseResult 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 2 with ParseResult

use of picocli.CommandLine.ParseResult 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 3 with ParseResult

use of picocli.CommandLine.ParseResult 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)

Example 4 with ParseResult

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

the class FileSystemCompiler method commandLineCompile.

/**
 * Same as main(args) except that exceptions are thrown out instead of causing
 * the VM to exit and the lookup for .groovy files can be controlled
 */
public static void commandLineCompile(String[] args, boolean lookupUnnamedFiles) throws Exception {
    CompilationOptions options = new CompilationOptions();
    CommandLine parser = configureParser(options);
    ParseResult parseResult = parser.parseArgs(args);
    if (CommandLine.printHelpIfRequested(parseResult)) {
        return;
    }
    displayStackTraceOnError = options.printStack;
    CompilerConfiguration configuration = options.toCompilerConfiguration();
    // Load the file name list
    String[] filenames = options.generateFileNames();
    boolean fileNameErrors = filenames == null;
    if (!fileNameErrors && (filenames.length == 0)) {
        parser.usage(System.err);
        return;
    }
    fileNameErrors = fileNameErrors && !validateFiles(filenames);
    if (!fileNameErrors) {
        doCompilation(configuration, null, filenames, lookupUnnamedFiles);
    }
}
Also used : CommandLine(picocli.CommandLine) ParseResult(picocli.CommandLine.ParseResult) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration)

Example 5 with ParseResult

use of picocli.CommandLine.ParseResult in project jgnash by ccavanaugh.

the class jGnashFx method main.

public static void main(final String[] args) {
    if (OS.getJavaVersion() < 11f) {
        System.err.println(ResourceUtils.getString("Message.JVM11"));
        System.err.println(ResourceUtils.getString("Message.Version") + " " + System.getProperty("java.version") + "\n");
        // show a swing based dialog
        JOptionPane.showMessageDialog(null, ResourceUtils.getString("Message.JVM11"), ResourceUtils.getString("Title.Error"), JOptionPane.ERROR_MESSAGE);
        return;
    }
    // Register the default exception handler
    Thread.setDefaultUncaughtExceptionHandler(new StaticUIMethods.ExceptionHandler());
    final CommandLine commandLine = new CommandLine(new CommandLineOptions());
    commandLine.setToggleBooleanFlags(false);
    commandLine.setUsageHelpWidth(80);
    try {
        final ParseResult pr = commandLine.parseArgs(args);
        final CommandLineOptions options = commandLine.getCommand();
        if (CommandLine.printHelpIfRequested(pr)) {
            System.exit(0);
        }
        // check for bad server file and hostName combination... can't do both
        if (serverFile != null && options.hostName != null) {
            commandLine.usage(System.err, Help.Ansi.AUTO);
            System.exit(1);
        }
        configureLogging();
        if (options.uninstall) {
            PortablePreferences.deleteUserPreferences();
            System.exit(0);
        }
        // System.getProperties().put(EncryptionManager.ENCRYPTION_FLAG, Boolean.toString(options.ssl));
        // System.getProperties().put("ssl", Boolean.toString(options.ssl));
        port = options.port;
        hostName = options.hostName;
        password = options.password;
        if (options.shutdown) {
            if (hostName == null) {
                hostName = EngineFactory.LOCALHOST;
            }
            MessageBus.getInstance().shutDownRemoteServer(hostName, port + 1, password);
            System.exit(0);
        }
        if (options.verbose) {
            System.setProperty("javafx.verbose", "true");
        }
        if (options.portableFile != null) {
            PortablePreferences.initPortablePreferences(options.portableFile.getAbsolutePath());
        } else if (options.portable) {
            PortablePreferences.initPortablePreferences(null);
        }
        if (options.zeroArgFile != null && options.zeroArgFile.exists()) {
            jGnashFx.dataFile = options.zeroArgFile;
        }
        if (options.dataFile != null && options.dataFile.exists()) {
            jGnashFx.dataFile = options.dataFile;
        }
        if (options.serverFile != null && options.serverFile.exists()) {
            jGnashFx.serverFile = options.serverFile;
            startServer();
        } else {
            setupNetworking();
            launch(args);
        }
    } catch (final Exception e) {
        logSevere(jGnashFx.class, e);
        commandLine.usage(System.err, Help.Ansi.AUTO);
        System.exit(1);
    }
}
Also used : CommandLine(picocli.CommandLine) ParseResult(picocli.CommandLine.ParseResult) StaticUIMethods(jgnash.uifx.StaticUIMethods) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

CommandLine (picocli.CommandLine)5 ParseResult (picocli.CommandLine.ParseResult)5 ParameterException (picocli.CommandLine.ParameterException)3 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)1 Violation (com.puppycrawl.tools.checkstyle.api.Violation)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StaticUIMethods (jgnash.uifx.StaticUIMethods)1 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)1