From e00c9bbbb9aa29c4c920815553aa70f5e78614e5 Mon Sep 17 00:00:00 2001 From: Andreas Deininger Date: Sun, 29 Jan 2023 21:24:57 +0100 Subject: [PATCH] Chapter 17.18, code sample: add Java version --- docs/index.adoc | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/docs/index.adoc b/docs/index.adoc index d6d237366..9c16f301c 100644 --- a/docs/index.adoc +++ b/docs/index.adoc @@ -9261,9 +9261,27 @@ To show that the subcommand is mandatory, use the `synopsisSubcommandLabel` attr There are some scenarios where picocli can not parse an option with a default value which is the same as a subcommand name. To work around this, you can parse the option yourself using <>. A simple implementation is shown below. See https://github.com/remkop/picocli/issues/1428[this issue] for more details. +.Java +[source,java,role="primary"] +---- +class ExecParameterConsumer implements IParameterConsumer { + public void consumeParameters(Stack args, + ArgSpec argSpec, + CommandSpec commandSpec) { + if (args.isEmpty()) { + throw new ParameterException( + commandSpec.commandLine(), + "Missing required parameter for option " + + ((OptionSpec) argSpec).longestName() + ); + } + argSpec.setValue(args.pop()); + } +} +---- .Kotlin -[source,kotlin,role="primary"] +[source,kotlin,role="secondary"] ---- class MyParameterConsumer : CommandLine.IParameterConsumer { override fun consumeParameters( @@ -9278,7 +9296,7 @@ class MyParameterConsumer : CommandLine.IParameterConsumer { (argSpec as CommandLine.Model.OptionSpec).longestName() ) } - argSpec.setValue(args.pop()); + argSpec.setValue(args.pop()) } } ----