Thursday, 15 April 2010

java - How to pass (and after get) array of parameters with Commons CLI? -



java - How to pass (and after get) array of parameters with Commons CLI? -

i have next code:

options.addoption(optionbuilder.withlongopt(sources) .withdescription("path sources") .hasarg() .withargname("paths") .create()); ... commandlineparser parser = new gnuparser(); line = parser.parse(options, args); ... system.out.println(line.getoptionvalues(sources));

i invoke application parameters

-sources=path1, path2

in result see ["path1"]

i want both arguments. how can pass it?

what should write in command line

make argument values space-delimited, rather comma-delimited:

-source=path1 path2

also, need utilize .hasargs() rather .hasarg() homecoming more 1 argument value. can specify .hasargs(2) explicit number of argument values. here's working example:

input: -source=path1 path2

public class test { public static void main(string[] args) { options options = new options(); options.addoption(optionbuilder.withlongopt("sources").withdescription("path sources").hasargs().withargname("paths").create()); commandlineparser parser = new gnuparser(); commandline line = null; seek { line = parser.parse(options, args, true); } grab (parseexception exp) { system.err.println("parsing failed. reason: " + exp.getmessage()); } system.out.println(arrays.tostring(line.getoptionvalues("sources"))); } }

output: [path1, path2]

java command-line command-line-interface command-line-parsing

No comments:

Post a Comment