Skip to content

Commit

Permalink
FIX #380 Main parameter doesn't support Converter
Browse files Browse the repository at this point in the history
  • Loading branch information
juewe committed Aug 26, 2017
1 parent 0483213 commit a2c687e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/beust/jcommander/JCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/beust/jcommander/JCommanderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
44 changes: 44 additions & 0 deletions src/test/java/com/beust/jcommander/args/ArgsMainParameter3.java
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit a2c687e

Please sign in to comment.