Closed gwenn closed 12 months ago
@cbeust Chime in if needed, but I personally do neither see that this is a bug nor that it could get solved. The reproducer deliberately is using int
instead of Integer
, so the default is 0
, not null
. Hence defaultObject
is not null, but an Integer
instance holding the value 0
. Hence it is correct that JCommander applies the validator.
@gwenn There is an implied default provided by the Java language itself, even if the application programmer does not explicitly provide a default value (try to find out whether there is no default or whether the default is 0
for an int
variable in Java, then you will understand this explanation). There is a simple workaround: Replace int
by Integer
and your code will work just fine. That's why Integer
exists in Java, as it allows to tell between the value 0
and "having no value" (null
). JCommander cannot and will not change the way Java deals with defaults.
@mkarg But for a required parameter, using a non-nullable type (int
instead of Integer
) seems wise, no ?
And I don't want to change java defaults, I want JCommander to ignore any default value for required parameter because default value is meaningless for required parameter.
@gwenn I do not see why int
should be more wise than Integer
, actually: You could instantly workaround your problem that way, which is very wise. ;-) Implementing the requested change just for required values is acceptable on the other hand. Feel free to post a PR, I would be happy to review it. :-)
@mkarg see #567 (I am not sure that unit test should be added to the 1500 lines JCommanderTest
class)
Minimal example:
If we remove the default meaningless (because parameter is required) value of
shift
(private int shift;
), JCommander throws:See https://github.com/cbeust/jcommander/blob/748ddba20a28956332b413eaa99d7e8b5422104d/src/main/java/com/beust/jcommander/ParameterDescription.java#L152-L159