From 25b711d8958ff795c8798f8cb81421dd83cc66aa Mon Sep 17 00:00:00 2001 From: Remko Popma Date: Sun, 1 Dec 2019 21:21:16 +0900 Subject: [PATCH] [#889][#885] (DOC) Bugfix in example: help was not showing --- docs/picocli-programmatic-api.adoc | 32 ++++++++++++++++++++-------- docs/picocli-programmatic-api.html | 34 +++++++++++++++++++++--------- 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/docs/picocli-programmatic-api.adoc b/docs/picocli-programmatic-api.adoc index db4e290a5..76c3db2b2 100644 --- a/docs/picocli-programmatic-api.adoc +++ b/docs/picocli-programmatic-api.adoc @@ -25,10 +25,12 @@ public class ProgrammaticCommand { public static void main(String[] args) { CommandSpec spec = CommandSpec.create(); spec.mixinStandardHelpOptions(true); // usageHelp and versionHelp options - spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT") + spec.addOption(OptionSpec.builder("-c", "--count") + .paramLabel("COUNT") .type(int.class) .description("number of times to execute").build()); - spec.addPositional(PositionalParamSpec.builder().paramLabel("FILES") + spec.addPositional(PositionalParamSpec.builder() + .paramLabel("FILES") .type(List.class).auxiliaryTypes(File.class) // List .description("The files to process").build()); CommandLine commandLine = new CommandLine(spec); @@ -40,6 +42,11 @@ public class ProgrammaticCommand { } static int run(ParseResult pr) { + // handle requests for help or version information + Integer helpExitCode = CommandLine.executeHelpRequest(pr); + if (helpExitCode != null) { return helpExitCode; } + + // implement the business logic int count = pr.matchedOptionValue('c', 1); List files = pr.matchedPositionalValue(0, Collections.emptyList()); for (File f : files) { @@ -333,35 +340,42 @@ For example: [source,java] ---- -public class ProgrammaticCommand { +public class MyApp { public static void main(String[] args) { CommandSpec spec = CommandSpec.create(); spec.mixinStandardHelpOptions(true); // usageHelp and versionHelp options - spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT") + spec.addOption(OptionSpec.builder("-c", "--count") + .paramLabel("COUNT") .type(int.class) .description("number of times to execute").build()); - spec.addPositional(PositionalParamSpec.builder().paramLabel("FILES") + spec.addPositional(PositionalParamSpec.builder() + .paramLabel("FILES") .type(List.class).auxiliaryTypes(File.class) // List .description("The files to process").build()); CommandLine commandLine = new CommandLine(spec); // optionally configure streams and handlers to be used - commandLine.setCaseInsensitiveEnumValuesAllowed(true) // configure a non-default parser option + commandLine.setCaseInsensitiveEnumValuesAllowed(true) //configure a parser option .setOut(myOutWriter()) // configure an alternative to System.out .setErr(myErrWriter()) // configure an alternative to System.err .setColorScheme(myColorScheme()) // configure a custom color scheme .setExitCodeExceptionMapper(myMapper()) // map exception to exit code - .setParameterExceptionHandler(Tmp::invalidUserInput) // configure a custom handler - .setExecutionExceptionHandler(Tmp::runtimeException) // configure a custom handler + .setParameterExceptionHandler(MyApp::invalidUserInput) // configure a custom handler + .setExecutionExceptionHandler(MyApp::runtimeException) // configure a custom handler ; // set an execution strategy so we can call CommandLine.execute(args) - commandLine.setExecutionStrategy(ProgrammaticCommand::run); + commandLine.setExecutionStrategy(MyApp::run); int exitCode = commandLine.execute(args); System.exit(exitCode); } static int run(ParseResult pr) { + // handle requests for help or version information + Integer helpExitCode = CommandLine.executeHelpRequest(pr); + if (helpExitCode != null) { return helpExitCode; } + + // implement the business logic int count = pr.matchedOptionValue('c', 1); List files = pr.matchedPositionalValue(0, Collections.emptyList()); for (File f : files) { diff --git a/docs/picocli-programmatic-api.html b/docs/picocli-programmatic-api.html index d585d2a6d..88c239d92 100644 --- a/docs/picocli-programmatic-api.html +++ b/docs/picocli-programmatic-api.html @@ -600,10 +600,12 @@

1. Example

public static void main(String[] args) { CommandSpec spec = CommandSpec.create(); spec.mixinStandardHelpOptions(true); // usageHelp and versionHelp options - spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT") + spec.addOption(OptionSpec.builder("-c", "--count") + .paramLabel("COUNT") .type(int.class) .description("number of times to execute").build()); - spec.addPositional(PositionalParamSpec.builder().paramLabel("FILES") + spec.addPositional(PositionalParamSpec.builder() + .paramLabel("FILES") .type(List.class).auxiliaryTypes(File.class) // List<File> .description("The files to process").build()); CommandLine commandLine = new CommandLine(spec); @@ -615,6 +617,11 @@

1. Example

} static int run(ParseResult pr) { + // handle requests for help or version information + Integer helpExitCode = CommandLine.executeHelpRequest(pr); + if (helpExitCode != null) { return helpExitCode; } + + // implement the business logic int count = pr.matchedOptionValue('c', 1); List<File> files = pr.matchedPositionalValue(0, Collections.<File>emptyList()); for (File f : files) { @@ -985,35 +992,42 @@

4.2. Execute Convenience Metho
-
public class ProgrammaticCommand {
+
public class MyApp {
 
     public static void main(String[] args) {
         CommandSpec spec = CommandSpec.create();
         spec.mixinStandardHelpOptions(true); // usageHelp and versionHelp options
-        spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT")
+        spec.addOption(OptionSpec.builder("-c", "--count")
+                .paramLabel("COUNT")
                 .type(int.class)
                 .description("number of times to execute").build());
-        spec.addPositional(PositionalParamSpec.builder().paramLabel("FILES")
+        spec.addPositional(PositionalParamSpec.builder()
+                .paramLabel("FILES")
                 .type(List.class).auxiliaryTypes(File.class) // List<File>
                 .description("The files to process").build());
         CommandLine commandLine = new CommandLine(spec);
 
         // optionally configure streams and handlers to be used
-        commandLine.setCaseInsensitiveEnumValuesAllowed(true) // configure a non-default parser option
+        commandLine.setCaseInsensitiveEnumValuesAllowed(true) //configure a parser option
             .setOut(myOutWriter()) // configure an alternative to System.out
             .setErr(myErrWriter()) // configure an alternative to System.err
             .setColorScheme(myColorScheme()) // configure a custom color scheme
             .setExitCodeExceptionMapper(myMapper()) //  map exception to exit code
-            .setParameterExceptionHandler(Tmp::invalidUserInput) // configure a custom handler
-            .setExecutionExceptionHandler(Tmp::runtimeException) // configure a custom handler
+            .setParameterExceptionHandler(MyApp::invalidUserInput) // configure a custom handler
+            .setExecutionExceptionHandler(MyApp::runtimeException) // configure a custom handler
         ;
         // set an execution strategy so we can call CommandLine.execute(args)
-        commandLine.setExecutionStrategy(ProgrammaticCommand::run);
+        commandLine.setExecutionStrategy(MyApp::run);
         int exitCode = commandLine.execute(args);
         System.exit(exitCode);
     }
 
     static int run(ParseResult pr) {
+        // handle requests for help or version information
+        Integer helpExitCode = CommandLine.executeHelpRequest(pr);
+        if (helpExitCode != null) { return helpExitCode; }
+
+        // implement the business logic
         int count = pr.matchedOptionValue('c', 1);
         List<File> files = pr.matchedPositionalValue(0, Collections.<File>emptyList());
         for (File f : files) {
@@ -1051,7 +1065,7 @@ 

4.2. Execute Convenience Metho