jcommander icon indicating copy to clipboard operation
jcommander copied to clipboard

JCommander should be Kotlin aware

Open dibog opened this issue 8 years ago • 2 comments

It would be nice, if using Kotlin I could write the following

class Args {
    @Parameter
    lateinit var file: File
}

Currently this will fail, as it seems that JCommand tries to read from file to see if there is an default value. With the new version of Kotlin (1.2 I think) you can check if the lateinit marked variable was already initialized. E.g. in the above case JCommander could detect, that file
a) is not initialized b) and therefore should be a required attribute.

dibog avatar Dec 06 '17 15:12 dibog

JCommander will not fail on this case. It leaves that property uninitialized if there was no argument passed for it. The application code will fail if it will try to access the uninitialized property after the arguments are parsed though. You can set required = true in the annotation, to make the parameter required.

h7io avatar Mar 31 '18 14:03 h7io

It might be worth considering if JCommander should be changed to have lateinit @Parameters required by default though. Since those will throw an exception if accessed non-initialized.

h7io avatar Mar 31 '18 14:03 h7io