picocli
picocli copied to clipboard
referencing another option in variable interpolation
Is there a way to reference another option in an option string? The biggest purpose here is that I would like picocli to 1) verify the option exists (i.e. check spelling) and 2) more importantly, style the reference somehow (such as using the gold color already used for options).
I'm thinking it would be something like ${OPTION:foo}
. I might use it like ${OPTION:verbose}
below. (See issue #1737 for more context.)
/**
* Enables or disables quiet output. Mutually exclusive with {@link #setVerbose(boolean)}.
* @param quiet The new state of quietness.
*/
@Option(names = {"--quiet",
"-q"}, description = "Reduces or eliminates output of unnecessary information. Mutually exclusive with ${OPTION:verbose}.", scope = ScopeType.INHERIT)
protected void setQuiet(final boolean quiet) {
checkState(!isVerbose(), "Quiet and verbose options are mutually exclusive.");
this.quiet = quiet;
}
Interesting idea.
Fleshing out the spec in some more detail:
- if any description, or a header/footer text contains a variable with the
${OPTION:
prefix, then - take the portion between the
:
colon and the closing}
curly bracket as the option name and try to see if that option exists (using the semantics defined in CommandSpec.findOption ) - if that option exists, then replace the variable with that option's longest option name, using the ansi color style for option names as defined in the command's color scheme.
- if that option does not exist, then do nothing: leave the variable as is, so it shows
xxx xxx ${OPTION:non-existing-option} xxx xxx
Note that such option lookups will only work when used in the command that that option is part of (not in parent commands, sibling commands or subcommands).
Also, can we do something similar for positional parameters?
Thoughts?
I just realized I hadn't got back to you on this.
Fleshing out the spec in some more detail:
The summary sounds excellent! That's exactly what I was looking for. (What you do if the option doesn't exist is up to you. Best do whatever is consistent with what you already do for other replacement variables that don't exist.)