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

Code sample: add Java version #1945

Merged
merged 2 commits into from
Feb 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 20 additions & 2 deletions docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<Custom Parameter Processing,`IParameterConsumer`>>. 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<String> 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(
Expand All @@ -9278,7 +9296,7 @@ class MyParameterConsumer : CommandLine.IParameterConsumer {
(argSpec as CommandLine.Model.OptionSpec).longestName()
)
}
argSpec.setValue(args.pop());
argSpec.setValue(args.pop())
}
}
----
Expand Down