private static int doMain()

in src/main/java/org/apache/maven/doxia/cli/ConverterCli.java [73:190]


    private static int doMain(String[] args) throws PlexusContainerException, ComponentLookupException {
        // ----------------------------------------------------------------------
        // 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.");
        }
        if (debug) {
            System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "DEBUG");
        }

        PlexusContainer container = getPlexusContainer();
        Converter converter = container.lookup(Converter.class);

        InputFileWrapper input;
        OutputFileWrapper output;
        final PostProcess postProcess;
        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);
            if (commandLine.hasOption(CLIManager.REMOVE_IN)
                    && commandLine.hasOption(CLIManager.GIT_MV_INPUT_TO_OUTPUT)) {
                throw new IllegalArgumentException(
                        "Options 'removeIn' and 'gitMvInputToOutput' are mutually exclusive.");
            } else if (commandLine.hasOption(CLIManager.REMOVE_IN)) {
                postProcess = PostProcess.REMOVE_AFTER_CONVERSION;
            } else if (commandLine.hasOption(CLIManager.GIT_MV_INPUT_TO_OUTPUT)) {
                postProcess = PostProcess.GIT_MV_INPUT_TO_OUTPUT;
            } else {
                postProcess = PostProcess.NONE;
            }
            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);
        converter.setPostProcess(postProcess);

        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;
    }