cbeust / jcommander

Command line parsing framework for Java
Apache License 2.0
1.97k stars 334 forks source link

java.io.File and java.nio.file.Path not supported by default parameter #558

Closed marekkrk closed 7 months ago

marekkrk commented 1 year ago

Example 1: java.lang.String works:

import com.beust.jcommander.JCommander; import com.beust.jcommander.Parameter;

public class Test {

@Parameter
String param;

public static void main(String[] args) {
    JCommander.newBuilder()
            .addObject(new Test())
            .args(args)
            .build();
}

}

Example 2: java.io.File does not work

import com.beust.jcommander.JCommander; import com.beust.jcommander.Parameter;

import java.io.File;

public class Test {

@Parameter
File param;

public static void main(String[] args) {
    JCommander.newBuilder()
            .addObject(new Test())
            .args(args)
            .build();
}

}

Exception in thread "main" com.beust.jcommander.ParameterException: Could not invoke null Reason: Can not set java.io.File field Test.param to java.lang.String at com.beust.jcommander.Parameterized.set(Parameterized.java:273) at com.beust.jcommander.JCommander$MainParameter.addValue(JCommander.java:108) at com.beust.jcommander.JCommander.parseValues(JCommander.java:789) at com.beust.jcommander.JCommander.parse(JCommander.java:363) at com.beust.jcommander.JCommander.parse(JCommander.java:342) at com.beust.jcommander.JCommander$Builder.build(JCommander.java:1198) at Test.main(Test.java:15)

Example 3: java.nio.file.Path does not work

import com.beust.jcommander.JCommander; import com.beust.jcommander.Parameter;

import java.nio.file.Path;

public class Test {

@Parameter
Path param;

public static void main(String[] args) {
    JCommander.newBuilder()
            .addObject(new Test())
            .args(args)
            .build();
}

}

Exception in thread "main" com.beust.jcommander.ParameterException: Could not invoke null Reason: Can not set java.nio.file.Path field Test.param to java.lang.String at com.beust.jcommander.Parameterized.set(Parameterized.java:273) at com.beust.jcommander.JCommander$MainParameter.addValue(JCommander.java:108) at com.beust.jcommander.JCommander.parseValues(JCommander.java:789) at com.beust.jcommander.JCommander.parse(JCommander.java:363) at com.beust.jcommander.JCommander.parse(JCommander.java:342) at com.beust.jcommander.JCommander$Builder.build(JCommander.java:1198) at Test.main(Test.java:15)

Example 4: List works

import com.beust.jcommander.JCommander; import com.beust.jcommander.Parameter;

import java.io.File; import java.util.List;

public class Test {

@Parameter
List<File> param;

public static void main(String[] args) {
    JCommander.newBuilder()
            .addObject(new Test())
            .args(args)
            .build();
}

}

mkarg commented 11 months ago

@marekkrk Can you please check your claim using the latest source code? There is a test that proofs both cases are working fine, see https://github.com/cbeust/jcommander/blob/master/src/test/java/com/beust/jcommander/JCommanderTest.java#L246-L247.

mkarg commented 7 months ago

@marekkrk As you did not respond to the open question, I assume my claim was correct and this is no longer an issue. So I am closing this ticket now.

marekkrk commented 7 months ago

Confirmed that 1.83 works as expected.

mkarg commented 7 months ago

Confirmed that 1.83 works as expected.

Thank you for your confirmation! :-)