Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom ShortErrorMessageHandler manual example should use bold red for error message #1127

Closed
remkop opened this issue Jul 15, 2020 · 1 comment

Comments

@remkop
Copy link
Owner

remkop commented Jul 15, 2020

Currently, the error message generated by this handler is rendered in the default console style.
The default handler uses bold red, which is nice.
Show users how to do this in the custom handler.

Index: docs/index.adoc
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- docs/index.adoc	(date 1594795931082)
+++ docs/index.adoc	(date 1594795931082)
@@ -2251,13 +2251,17 @@
         CommandLine cmd = ex.getCommandLine();
         PrintWriter writer = cmd.getErr();
 
-        writer.println(ex.getMessage());
+        writer.println(cmd.getColorScheme().errorText(ex.getMessage())); // error message in bold red
         UnmatchedArgumentException.printSuggestions(ex, writer);
         writer.print(cmd.getHelp().fullSynopsis()); // since 4.1
 
         CommandSpec spec = cmd.getCommandSpec();
         writer.printf("Try '%s --help' for more information.%n", spec.qualifiedName());
 
+        if ("DEBUG".equalsIgnoreCase(System.getProperty("picocli.trace"))) {
+            ex.printStackTrace(System.err); // provide more details if requested
+        }
+
         return cmd.getExitCodeExceptionMapper() != null
                     ? cmd.getExitCodeExceptionMapper().getExitCode(ex)
                     : spec.exitCodeOnInvalidInput();
@remkop
Copy link
Owner Author

remkop commented Jul 16, 2020

More changes:

  • rename writer to err
  • print stack trace to cmd.getErr instead of to System.err
  • print stack trace after DEBUG trace messages and before the red bold error message/usage message
  • apply stacktrace style to stack trace
        PrintWriter err = cmd.getErr();

        // if we are tracing at DEBUG level, print a stacktrace
        if ("DEBUG".equalsIgnoreCase(System.getProperty("picocli.trace"))) {
            StringWriter sw = new StringWriter();
            ex.printStackTrace(new PrintWriter(sw, true));
            err.print(cmd.getColorScheme().stackTraceText(sw.toString()));
        }

        err.println(cmd.getColorScheme().errorText(ex.getMessage())); // bold red
        UnmatchedArgumentException.printSuggestions(ex, err);
        err.print(cmd.getHelp().fullSynopsis());

        CommandSpec spec = cmd.getCommandSpec();
        err.printf("Try '%s --help' for more information.%n", spec.qualifiedName());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant