jcommander
jcommander copied to clipboard
Dynamic Parameters are getting ignored when using the @ syntax to pass in parameters
Using JCommander 1.72 No issues when passing dynamic parameters directly in the cmdline. But moving these into a separate file using the @syntax will make JCommander ignoring these params.
I think the issue is related to following codelines.
JCommander, L422
private String[] expandArgs(String[] originalArgv) {
List<String> vResult1 = Lists.newArrayList();
//
// Expand @
//
for (String arg : originalArgv) {
if (arg.startsWith("@") && options.expandAtSign) {
String fileName = arg.substring(1);
vResult1.addAll(readFile(fileName));
} else {
List<String> expanded = expandDynamicArg(arg);
vResult1.addAll(expanded);
}
}
Can you provide a failing example?
After isolating the code i was better able to discover the problem. When using the default separator (a space), then the external parameter file using the @-syntax must contain new-lines
-t
whatever
-P
param1=value1
-P
param2=value2
When using a non-default separator, example colon : , then the external parameters file can look like
-t:whatever
-P:param1=value1
-P:param2=value2
but also working is
-t
whatever
-P
param1=value1
-P
param2=value2
I was a bit confused by this behavior that i considered this as an issue. Up to you if you agree with this. At least, not an issue for me any more.
Test Java class used.
package com.tde;
import com.beust.jcommander.DynamicParameter;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import java.util.HashMap;
import java.util.Map;
public final class Main {
public static void main(String... args) {
Main main = new Main();
main.process(args);
}
void process(String... args) {
final InputParameters parameters = new InputParameters();
final JCommander commander = new JCommander(parameters);
commander.setAcceptUnknownOptions(true);
try {
commander.parse(args);
logInputParameters(parameters);
} catch (final ParameterException e) {
System.err.println("Parameter parsing exception");
System.err.println(e.getLocalizedMessage());
final StringBuilder out = new StringBuilder();
commander.usage(out);
System.err.println(out.toString());
throw e;
}
}
private void logInputParameters(InputParameters parameters) {
System.out.println("param t : "+parameters.getMyParam());
for (Map.Entry<String, String> entry : parameters.getDynamicParameters().entrySet()) {
System.out.println("param p: "+entry.getKey() + "->"+entry.getValue());
}
}
//@Parameters(separators = ":")
class InputParameters {
@Parameter(names = {"-t"}, description = "Another parameter", required = true)
private String myParam;
@DynamicParameter(names = "-P", description = "Dynamic parameters examples")
private Map<String, String> dynamicParameters = new HashMap<>();
public String getMyParam() {
return myParam;
}
public Map<String, String> getDynamicParameters() {
return dynamicParameters;
}
}
}
Hi Cedric,
Example added. Not sure however if you consider this an issue or not.
Regards, thomas
On Thu, Sep 21, 2017 at 6:04 PM, Cedric Beust [email protected] wrote:
Can you provide a failing example?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/cbeust/jcommander/issues/401#issuecomment-331203675, or mute the thread https://github.com/notifications/unsubscribe-auth/AKbKYFDQyWfhniq0zqKpTtNkm36BCAzuks5skokogaJpZM4PfeNh .