Skip to content

Commit

Permalink
GITHUB-417: Trimming parameters
Browse files Browse the repository at this point in the history
Fixes #417
  • Loading branch information
cbeust committed Dec 1, 2017
1 parent b794de2 commit 4aec38b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/beust/jcommander/JCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ private int processFixedArity(String[] args, int originalIndex, ParameterDescrip
Object finalValue = null;
if (index + arity < args.length) {
for (int j = 1; j <= arity; j++) {
String value = trim(args[index + j + offset]);
String value = args[index + j + offset];

This comment has been minimized.

Copy link
@phillip-urioste

phillip-urioste Jul 4, 2019

Won't the arg here already be trimmed since you trimmed it in parseValues()? See https://github.com/cbeust/jcommander/blob/master/src/main/java/com/beust/jcommander/JCommander.java#L692

finalValue = pd.addValue(arg, value, false, validate, j - 1);
requiredFields.remove(pd.getParameterized());
}
Expand Down
36 changes: 14 additions & 22 deletions src/test/java/com/beust/jcommander/JCommanderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
import java.io.*;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
Expand Down Expand Up @@ -524,7 +521,7 @@ public void getParametersShouldNotNpe() {
public void validationShouldWork1() {
ArgsValidate1 a = new ArgsValidate1();
JCommander jc = new JCommander(a);
jc.parse("-age", "2 ");
jc.parse("-age", "2");
Assert.assertEquals(a.age, new Integer(2));
}

Expand Down Expand Up @@ -1418,27 +1415,22 @@ public void mainWithConverter() {
Assert.assertEquals(file.getAbsolutePath(), args.getPath().getAbsolutePath());
}

@Test(enabled = false)
public static void main(String[] args) {

class FileValidator implements IParameterValidator {
@Override
public void validate(String name, String value) throws ParameterException {
if (!Files.exists(Paths.get(value))) {
throw new ParameterException("FILE_DOES_NOT_EXIST");
}
}
}

@Test
public void trimTest() {
class Args {
@Parameter(names = "--file", validateWith = FileValidator.class, required = true,
description = "The properties file containing setup information.")
private Path propertiesFile = Paths.get("");
@Parameter(names = {"-L", "--replace-new-line"}, description = "Char used for replacing new line in value")
protected String escapeNl;
}

Args args = new Args();
JCommander.newBuilder()
.addObject(new Args())
.addObject(args)
.build()
.parse("--file", "README.markdown");
.parse("-L", " ");
Assert.assertEquals(args.escapeNl, " ");
}

@Test(enabled = false)
public static void main(String[] args) {
new JCommanderTest().trimTest();
}
}

0 comments on commit 4aec38b

Please sign in to comment.