66import picocli .CommandLine ;
77
88import java .io .PrintWriter ;
9+ import java .util .Map ;
910import java .util .Objects ;
1011
1112/**
1415 */
1516class ShortErrorMessageHandler implements CommandLine .IParameterExceptionHandler {
1617
18+ private static final Map <String , String > SHORT_OPTIONS_REPLACED_IN_TWO_DOT_ZERO_RELEASE = Map .of (
19+ "-P" , "--spark-prop" ,
20+ "-R" , "--doc-prop" ,
21+ "-M" , "--doc-metadata" ,
22+ "-S" , "--splitter-prop" ,
23+ "-X" , "--xpath-namespace" ,
24+ "-L" , "--classifier-prop" ,
25+ "-E" , "--embedder-prop"
26+ );
27+
1728 public int handleParseException (CommandLine .ParameterException ex , String [] args ) {
1829 final CommandLine commandLine = ex .getCommandLine ();
1930 Objects .requireNonNull (commandLine );
@@ -29,7 +40,8 @@ public int handleParseException(CommandLine.ParameterException ex, String[] args
2940
3041 final String exceptionMessage = getErrorMessageToPrint (ex );
3142 if (exceptionMessage != null ) {
32- err .println (colorScheme .errorText (exceptionMessage )); // bold red
43+ err .println (colorScheme .errorText (exceptionMessage ));
44+ printHelpfulMessageForReplacedSingleLetterOption (exceptionMessage , err , colorScheme );
3345 }
3446
3547 CommandLine .UnmatchedArgumentException .printSuggestions (ex , err );
@@ -55,4 +67,24 @@ private String getErrorMessageToPrint(Exception ex) {
5567 }
5668 return message ;
5769 }
70+
71+ private void printHelpfulMessageForReplacedSingleLetterOption (String exceptionMessage , PrintWriter err , CommandLine .Help .ColorScheme colorScheme ) {
72+ SHORT_OPTIONS_REPLACED_IN_TWO_DOT_ZERO_RELEASE .keySet ().forEach (shortOption -> {
73+ final String indicatorOfReplacedShortOption = "Unknown option: '" + shortOption ;
74+ if (exceptionMessage .startsWith (indicatorOfReplacedShortOption )) {
75+ String longOption = SHORT_OPTIONS_REPLACED_IN_TWO_DOT_ZERO_RELEASE .get (shortOption );
76+ err .println ("" );
77+ err .println (colorScheme .errorText ("Did you mean to use %s instead of %s, as %s was replaced in the 2.0 release with %s?"
78+ .formatted (longOption , shortOption , shortOption , longOption )));
79+
80+ String optionValue = exceptionMessage .substring (indicatorOfReplacedShortOption .length ());
81+ if (optionValue .endsWith ("'" )) {
82+ optionValue = optionValue .substring (0 , optionValue .length () - 1 );
83+ }
84+
85+ err .println (colorScheme .errorText ("If so, use %s %s instead." .formatted (longOption , optionValue )));
86+ err .println ("" );
87+ }
88+ });
89+ }
5890}
0 commit comments