diff --git a/pmd/src/main/java/net/sourceforge/pmd/PMD.java b/pmd/src/main/java/net/sourceforge/pmd/PMD.java index 7b478287ac..377221b9d4 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/PMD.java +++ b/pmd/src/main/java/net/sourceforge/pmd/PMD.java @@ -335,8 +335,6 @@ public class PMD { return languages; } - private static final int ERROR_STATUS = 1; - /** * Entry to invoke PMD as command line tool * @@ -367,7 +365,7 @@ public class PMD { } catch (Exception e) { PMDCommandLineInterface.buildUsageText(); System.out.println(e.getMessage()); - status = ERROR_STATUS; + status = PMDCommandLineInterface.ERROR_STATUS; } finally { logHandlerManager.close(); LOG.setLevel(oldLogLevel); diff --git a/pmd/src/main/java/net/sourceforge/pmd/cli/PMDCommandLineInterface.java b/pmd/src/main/java/net/sourceforge/pmd/cli/PMDCommandLineInterface.java index eb0289bd9c..9c1adcb57a 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/cli/PMDCommandLineInterface.java +++ b/pmd/src/main/java/net/sourceforge/pmd/cli/PMDCommandLineInterface.java @@ -27,17 +27,24 @@ public class PMDCommandLineInterface { public static final String NO_EXIT_AFTER_RUN = "net.sourceforge.pmd.cli.noExit"; public static final String STATUS_CODE_PROPERTY = "net.sourceforge.pmd.cli.status"; + public static final int ERROR_STATUS = 1; + public static PMDParameters extractParameters(PMDParameters arguments, String[] args, String progName) { + jcommander = new JCommander(arguments); + jcommander.setProgramName(progName); + try { - jcommander = new JCommander(arguments, args); - jcommander.setProgramName(progName); + jcommander.parse(args); if (arguments.isHelp()) { jcommander.usage(); - System.exit(0); + System.out.println(buildUsageText()); + setStatusCodeOrExit(0); } } catch (ParameterException e) { + jcommander.usage(); System.out.println(buildUsageText()); System.out.println(e.getMessage()); + setStatusCodeOrExit(ERROR_STATUS); } return arguments; } @@ -61,7 +68,7 @@ public class PMDCommandLineInterface { + "3) A ruleset filename or a comma-delimited string of ruleset filenames" + PMD.EOL + PMD.EOL + "For example: " + PMD.EOL - + "c:\\> " + launchCmd + "-d c:\\my\\source\\code -f html -R java-unusedcode" + PMD.EOL + + "c:\\> " + launchCmd + " -d c:\\my\\source\\code -f html -R java-unusedcode" + PMD.EOL + PMD.EOL; fullText += supportedVersions() + PMD.EOL; @@ -148,12 +155,16 @@ public class PMDCommandLineInterface { } public static void run(String[] args) { - if ( isExitAfterRunSet() ) - System.exit(PMD.run(args)); - else - setStatusCode(PMD.run(args)); + setStatusCodeOrExit(PMD.run(args)); } + public static void setStatusCodeOrExit(int status) { + if ( isExitAfterRunSet() ) + System.exit(status); + else + setStatusCode(status); + } + private static boolean isExitAfterRunSet() { return (System.getenv(NO_EXIT_AFTER_RUN) == null ? false : true); } diff --git a/pmd/src/main/java/net/sourceforge/pmd/cli/PMDParameters.java b/pmd/src/main/java/net/sourceforge/pmd/cli/PMDParameters.java index b20d4876cd..e747221b60 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/cli/PMDParameters.java +++ b/pmd/src/main/java/net/sourceforge/pmd/cli/PMDParameters.java @@ -28,7 +28,7 @@ public class PMDParameters { @Parameter(names = {"-debug", "-verbose", "-D", "-V"}, description = "Debug mode") private boolean debug = false; - @Parameter(names = {"-help","-h","-H"}, description = "Display help on usage") + @Parameter(names = {"-help","-h","-H"}, description = "Display help on usage", help = true) private boolean help = false; @Parameter(names= {"-encoding", "-e"} , description = "specifies the character set encoding of the source code files PMD is reading (i.e., UTF-8)") @@ -67,7 +67,7 @@ public class PMDParameters { @Parameter(names = {"-language", "-l"}, description = "specify version of a language PMD should use") private String language = Language.getDefaultLanguage().getTerseName(); - @Parameter(names = "-auxclasspath", description = "specifies the classpath for libraries used by the source code (used by type resolution)\n(alternatively, a 'file://' URL to a text file containing path elements on consecutive lines") + @Parameter(names = "-auxclasspath", description = "specifies the classpath for libraries used by the source code. This is used by the type resolution. Alternatively, a 'file://' URL to a text file containing path elements on consecutive lines can be specified.") private String auxclasspath; class PropertyConverter implements IStringConverter {