Skip to content

Commit

Permalink
Support arity for main parameters.
Browse files Browse the repository at this point in the history
Partial fix for #90
  • Loading branch information
Cedric Beust committed May 15, 2017
1 parent f6c3dc5 commit 192ec86
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/beust/jcommander/JCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,26 @@ private void validateOptions() {
}

if (mainParameterDescription != null) {
// Make sure we have a main parameter if it was required
if (mainParameterDescription.getParameter().required() &&
!mainParameterDescription.isAssigned()) {
throw new ParameterException("Main parameters are required (\""
+ mainParameterDescription.getDescription() + "\")");
}

// If the main parameter has an arity, make sure the correct number of parameters was passed
int arity = mainParameterDescription.getParameter().arity();
if (arity != Parameter.DEFAULT_ARITY) {
Object value = mainParameterDescription.getParameterized().get(mainParameterObject);
if (List.class.isAssignableFrom(value.getClass())) {
int size = ((List<?>) value).size();
if (size != arity) {
throw new ParameterException("There should be exactly " + arity + " main parameters but "
+ size + " were found");
}
}

}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/beust/jcommander/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
* How many parameter values this parameter will consume. For example,
* an arity of 2 will allow "-pair value1 value2".
*/
int arity() default -1;
public static int DEFAULT_ARITY = -1;
int arity() default DEFAULT_ARITY;

/**
* If true, this parameter is a password and it will be prompted on the console
Expand Down
57 changes: 51 additions & 6 deletions src/test/java/com/beust/jcommander/JCommanderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
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 @@ -1518,16 +1521,58 @@ class Args {
Assert.assertEquals(args.f, false);
}

@Test
public void mainParameterWithCorrectArity() {
class Args {
@Parameter(arity = 2)
private List<String> main;
}

Args args = new Args();
JCommander.newBuilder()
.addObject(args)
.args(new String[]{"a", "b"})
.build();
Assert.assertEquals(args.main.size(), 2);
Assert.assertEquals(args.main.get(0), "a");
Assert.assertEquals(args.main.get(1), "b");
}

@Test(expectedExceptions = ParameterException.class)
public void mainParameterWithWrongArity() {
class Args {
@Parameter(arity = 2)
private List<String> main;
}

Args args = new Args();
JCommander.newBuilder()
.addObject(args)
.args(new String[]{"a"})
.build();
}

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

CommandTemplate template = new CommandTemplate();
JCommander jcommander = new JCommander(template);
jcommander.setProgramName("prog");
jcommander.parse("help");
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");
}
}
}

if (template.help) {
jcommander.usage();
class Args {
@Parameter(names = "--file", validateWith = FileValidator.class, required = true,
description = "The properties file containing setup information.")
private Path propertiesFile = Paths.get("");
}

JCommander.newBuilder()
.addObject(new Args())
.build()
.parse("--file", "README.markdown");
}
}

0 comments on commit 192ec86

Please sign in to comment.