private static int doMain()

in src/main/java/org/apache/maven/doxia/cli/ConverterCli.java [63:166]


    private static int doMain(String[] args) {
        // ----------------------------------------------------------------------
        // Setup the command line parser
        // ----------------------------------------------------------------------

        CLIManager cliManager = new CLIManager();

        CommandLine commandLine;
        try {
            commandLine = cliManager.parse(args);
        } catch (ParseException e) {
            System.err.println("Unable to parse command line options: " + e.getMessage());
            CLIManager.displayHelp();

            return 1;
        }

        if (commandLine.hasOption(CLIManager.HELP)) {
            CLIManager.displayHelp();

            return 0;
        }

        if (commandLine.hasOption(CLIManager.VERSION)) {
            showVersion();

            return 0;
        }

        boolean debug = commandLine.hasOption(CLIManager.DEBUG);

        boolean showErrors = debug || commandLine.hasOption(CLIManager.ERRORS);

        if (showErrors) {
            System.out.println("+ Error stacktraces are turned on.");
        }

        Converter converter = new DefaultConverter();
        if (debug) {
            System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "DEBUG");
        }

        InputFileWrapper input;
        OutputFileWrapper output;
        try {
            String sourceFormat = commandLine.getOptionValue(CLIManager.FROM, CLIManager.AUTO_FORMAT);
            final DefaultConverter.DoxiaFormat parserFormat;
            if (CLIManager.AUTO_FORMAT.equalsIgnoreCase(sourceFormat)) {
                File inputFile = new File(commandLine.getOptionValue(CLIManager.IN));
                parserFormat = DefaultConverter.DoxiaFormat.autoDetectFormat(inputFile);
                if (debug) {
                    System.out.println("Auto detected input format: " + parserFormat);
                }
            } else {
                parserFormat = DefaultConverter.DoxiaFormat.valueOf(sourceFormat.toUpperCase());
            }
            String targetFormat = commandLine.getOptionValue(CLIManager.TO);
            final DefaultConverter.DoxiaFormat sinkFormat =
                    DefaultConverter.DoxiaFormat.valueOf(targetFormat.toUpperCase());
            input = InputFileWrapper.valueOf(
                    commandLine.getOptionValue(CLIManager.IN),
                    parserFormat,
                    commandLine.getOptionValue(CLIManager.INENCODING));
            output = OutputFileWrapper.valueOf(
                    commandLine.getOptionValue(CLIManager.OUT),
                    sinkFormat,
                    commandLine.getOptionValue(CLIManager.OUTENCODING));
        } catch (IllegalArgumentException e) {
            showFatalError("Illegal argument: " + e.getMessage(), e, showErrors);

            CLIManager.displayHelp();

            return 1;
        } catch (UnsupportedEncodingException | FileNotFoundException e) {
            showFatalError(e.getMessage(), e, showErrors);

            return 1;
        }

        boolean format = commandLine.hasOption(CLIManager.FORMAT);
        converter.setFormatOutput(format);

        try {
            converter.convert(input, output);
        } catch (UnsupportedFormatException e) {
            showFatalError(e.getMessage(), e, showErrors);

            return 1;
        } catch (ConverterException e) {
            showFatalError("Converter exception: " + e.getMessage(), e, showErrors);

            return 1;
        } catch (IllegalArgumentException e) {
            showFatalError("Illegal argument: " + e.getMessage(), e, showErrors);

            return 1;
        } catch (RuntimeException e) {
            showFatalError("Runtime exception: " + e.getMessage(), e, showErrors);

            return 1;
        }

        return 0;
    }