diff --git a/src/main/java/com/beust/jcommander/JCommander.java b/src/main/java/com/beust/jcommander/JCommander.java index d3e16b5f..4c035c16 100644 --- a/src/main/java/com/beust/jcommander/JCommander.java +++ b/src/main/java/com/beust/jcommander/JCommander.java @@ -753,6 +753,13 @@ else if (commands.isEmpty()) { String value = a; // If there's a non-quoted version, prefer that one Object convertedValue = value; + // Fix + // Main parameter doesn't support Converter + // https://github.com/cbeust/jcommander/issues/380 + if (mainParameter.annotation.converter() != null && mainParameter.annotation.converter() != NoConverter.class){ + convertedValue = convertValue(mainParameter.parameterized, mainParameter.parameterized.getType(), null, value); + } + Type genericType = mainParameter.parameterized.getGenericType(); if (genericType instanceof ParameterizedType) { ParameterizedType p = (ParameterizedType) genericType; diff --git a/src/test/java/com/beust/jcommander/JCommanderTest.java b/src/test/java/com/beust/jcommander/JCommanderTest.java index b7b054b0..b1be11ea 100644 --- a/src/test/java/com/beust/jcommander/JCommanderTest.java +++ b/src/test/java/com/beust/jcommander/JCommanderTest.java @@ -1656,6 +1656,18 @@ class Args { .build(); } + @Test + public void mainWithConverter() { + + String path = ".."; + File file = new File(path); + ArgsMainParameter3 args = new ArgsMainParameter3(); + JCommander jc = new JCommander(args); + + jc.parse(path); + Assert.assertEquals(file.getAbsolutePath(), args.getPath().getAbsolutePath()); + } + @Test(enabled = false) public static void main(String[] args) { diff --git a/src/test/java/com/beust/jcommander/args/ArgsMainParameter3.java b/src/test/java/com/beust/jcommander/args/ArgsMainParameter3.java new file mode 100644 index 00000000..073dc5bc --- /dev/null +++ b/src/test/java/com/beust/jcommander/args/ArgsMainParameter3.java @@ -0,0 +1,44 @@ +/** + * Copyright (C) 2010 the original author or authors. + * See the notice.md file distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.beust.jcommander.args; + +import com.beust.jcommander.Parameter; +import com.beust.jcommander.converters.FileConverter; + +import java.io.File; + +/** + * A class with main parameter that needs a Converter + * + * @author juewe + */ +public class ArgsMainParameter3 { + + @Parameter(converter = FileConverter.class) + public File path; + + public File getPath() { + return path; + } + + public void setPath(File path) { + this.path = path; + } + +}