pmd: CLI - make sure we display the usage help text

Therefore make the -h option a help option.
Make the auxclasspath option description look nicer - newlines break the layout.
Always display the jcommander usage help text.
And exit with an error code in case there are parameter errors.
This commit is contained in:
Andreas Dangel 2012-12-05 21:58:39 +01:00
parent 063c4228e3
commit 63c1a8fe47
3 changed files with 22 additions and 13 deletions

View File

@ -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);

View File

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

View File

@ -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<Properties> {