-
Notifications
You must be signed in to change notification settings - Fork 420
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
Support @Spec
-annotated members in ArgGroup classes (was: Question: Throwing a ParameterException inside an ArgGroup)
#1260
Comments
Good point. Do you think you’ll be able to provide a pull request for this? |
I've never used Java reflection before (and some call me lucky for that fact), so my ability to contribute on the issue is rather limited. A quick debug session shows that when I initialize the ArgGroup first the NPE is gone, although the CommandSpec remains null even though That quick session did not immediately provide any ideas for a solution though. I can investigate it a bit further, see if getting a bit more familiar with the codebase provides any leads. |
Hi @clone1612, thanks for investigating! That is helpful!
What I believe is happening is that the ArgGroup instance is replaced by a new separate instance when the first option in that group is recognized during parsing. Like you mention, the ArgGroup instances are created during parsing in That means the Object creation (especially for groups) is the most complex part of picocli, so I imagine this is not easy to get familiar with. Thanks for helping out! |
@Spec
-annotated members in ArgGroup classes (was: Question: Throwing a ParameterException inside an ArgGroup)
I pushed a fix to master that addresses this issue. I want to add more tests with a |
For our application we make heavy use of the ArgGroup feature, which works really well. Due to the amount of parameters present in said groups these classes are stored in separate files, rather than inside the command class where they are used.
One such parameter is a String where we want to limit the possible options. As these depend on the location where the application is deployed & its configuration we cannot use an enum and the build-in validation for that type offered by picocli. We therefore have to perform some manual validation, as in following (stripped-down) example:
Ideally we want to throw a ParameterException, however for such an exception we need access to the CommandLine object of the command. Injecting the CommandSpec in the ArgGroup sadly leads to NPE's:
@CommandLine.Spec CommandLine.Model.CommandSpec commandSpec;
Application does not launch at all, printing following stacktrace:
@CommandLine.Spec(CommandLine.Spec.Target.MIXEE) CommandLine.Model.CommandSpec commandSpec;
Application launches, but field remains null. As the ArgGroup isn't injected in the command as a Mixee this makes sense but it was worth a shot.
The documentation does not mention that it is possible to inject a Spec inside an ArgGroup so I wonder if it's supported in any way? If not, what would be the preferred way of throwing a ParameterException inside an ArgGroup?
Our current implementation lets the ArgGroup define a
validate()
function, which verifies the arguments inside the ArgGroup and throws an IllegalArgumentException. Before the actual command logic is then run we call said function, and map the IllegalArgumentException to a ParameterException. This is however rather convoluted, and we'd prefer to perform the actual validation where it makes the most sense (i.e. when & where the parameter is set).The text was updated successfully, but these errors were encountered: