Fix behavior when an unknown option is provided on the command line, fixes #354

This commit is contained in:
Guillaume Nodet
2021-02-15 09:34:09 +01:00
parent c85f2ea285
commit e15c4f7daa
2 changed files with 22 additions and 13 deletions

View File

@@ -240,8 +240,8 @@ public class DaemonMavenCli {
}
if (cliRequest.multiModuleProjectDirectory == null) {
System.err.format(
"-D%s system property is not set.", MULTIMODULE_PROJECT_DIRECTORY);
buildEventListener.log(String.format(
"-D%s system property is not set.", MULTIMODULE_PROJECT_DIRECTORY));
throw new ExitException(1);
}
System.setProperty("maven.multiModuleProjectDirectory", cliRequest.multiModuleProjectDirectory.toString());
@@ -281,9 +281,9 @@ public class DaemonMavenCli {
}
}
} catch (ParseException e) {
System.err.println("Unable to parse maven.config: " + e.getMessage());
buildEventListener.log(MvndHelpFormatter.displayHelp(cliManager));
throw e;
buildEventListener.log("Unable to parse maven.config: " + e.getMessage());
buildEventListener.log("Run 'mvnd --help' for available options.");
throw new ExitException(1);
}
try {
@@ -293,9 +293,9 @@ public class DaemonMavenCli {
cliRequest.commandLine = cliMerge(cliManager.parse(cliRequest.args), mavenConfig);
}
} catch (ParseException e) {
System.err.println("Unable to parse command line options: " + e.getMessage());
buildEventListener.log(MvndHelpFormatter.displayHelp(cliManager));
throw e;
buildEventListener.log("Unable to parse command line options: " + e.getMessage());
buildEventListener.log("Run 'mvnd --help' for available options.");
throw new ExitException(1);
}
}

View File

@@ -48,7 +48,7 @@ public class MvndHelpFormatter {
* @return the string containing the help message
*/
public static String displayHelp(CLIManager cliManager) {
int terminalWidth = Environment.MVND_TERMINAL_WIDTH.asInt();
int terminalWidth = getTerminalWidth();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (PrintStream out = new PrintStream(baos, false, StandardCharsets.UTF_8.name())) {
out.println();
@@ -116,8 +116,8 @@ public class MvndHelpFormatter {
spaces(help, indentPos - help.length());
wrap(help, toPlainText(entry.getJavaDoc()), terminalWidth, lineEnd, indent);
indentedLine(help, "Default", env.getDefault(), indent);
indentedLine(help, "Env. variable", env.getEnvironmentVariable(), indent);
indentedLine(help, terminalWidth, "Default", env.getDefault(), indent);
indentedLine(help, terminalWidth, "Env. variable", env.getEnvironmentVariable(), indent);
});
@@ -141,10 +141,19 @@ public class MvndHelpFormatter {
return help.toString();
}
private static void indentedLine(final StringBuilder stringBuilder, String key, final String value, final String indent) {
private static int getTerminalWidth() {
int terminalWidth;
try {
terminalWidth = Environment.MVND_TERMINAL_WIDTH.asInt();
} catch (Exception e) {
terminalWidth = 80;
}
return terminalWidth;
}
private static void indentedLine(StringBuilder stringBuilder, int terminalWidth, String key, String value, String indent) {
int lineEnd;
if (value != null) {
int terminalWidth = Environment.MVND_TERMINAL_WIDTH.asInt();
lineEnd = stringBuilder.length() + terminalWidth;
stringBuilder
.append(System.lineSeparator())