Command line arguments are aggregated into an String[] in Java and passed into your main un-processed.
public static void main(final String[] args) { // code }
With your example input of D:/input a.conf,b.conf,c.conf D:/output the value of args becomes ["D:/input", "a.conf,b.conf,c.conf", "D:/output"] where the [] delimit an array of String.
You have two options:
- manually parse this
argsarray and pull it apart manually and process each element yourself. In this case you need toArrays.asList(args[2].split(","));the second argument to get it into aList<String>as you desire. - Use something like JSAP ( Java Simple Argument Parser ) and let a well tested and mature library parse, validate and format the input for you. Specifically it can parse the second argument you are passing in for you.
Needless to say, I prefer the second option because the first is just brittle busy work.
Answer from user177800 on Stack OverflowVideos
Command line arguments are aggregated into an String[] in Java and passed into your main un-processed.
public static void main(final String[] args) { // code }
With your example input of D:/input a.conf,b.conf,c.conf D:/output the value of args becomes ["D:/input", "a.conf,b.conf,c.conf", "D:/output"] where the [] delimit an array of String.
You have two options:
- manually parse this
argsarray and pull it apart manually and process each element yourself. In this case you need toArrays.asList(args[2].split(","));the second argument to get it into aList<String>as you desire. - Use something like JSAP ( Java Simple Argument Parser ) and let a well tested and mature library parse, validate and format the input for you. Specifically it can parse the second argument you are passing in for you.
Needless to say, I prefer the second option because the first is just brittle busy work.
public static void main(String[] args) {
int argc = 0;
if(args.length>2){
String inputFileLocation = args[0];
String configFileList = args[1];
String outputFileLocation = args[2];
List<String> lList=Arrays.asList(configFileList.split(","));
System.out.println(inputFileLocation);
System.out.println(lList);
System.out.println(outputFileLocation);
}
}
Output:
D:/input
[a.conf, b.conf, c.conf]
D:/output
Use the Apache Commons CLI library commandline.getArgs() to get arg1, arg2, arg3, and arg4. Here is some code:
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.Option.Builder;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.ParseException;
public static void main(String[] parameters)
{
CommandLine commandLine;
Option option_A = Option.builder("A")
.required(true)
.desc("The A option")
.longOpt("opt3")
.build();
Option option_r = Option.builder("r")
.required(true)
.desc("The r option")
.longOpt("opt1")
.build();
Option option_S = Option.builder("S")
.required(true)
.desc("The S option")
.longOpt("opt2")
.build();
Option option_test = Option.builder()
.required(true)
.desc("The test option")
.longOpt("test")
.build();
Options options = new Options();
CommandLineParser parser = new DefaultParser();
String[] testArgs =
{ "-r", "opt1", "-S", "opt2", "arg1", "arg2",
"arg3", "arg4", "--test", "-A", "opt3", };
options.addOption(option_A);
options.addOption(option_r);
options.addOption(option_S);
options.addOption(option_test);
try
{
commandLine = parser.parse(options, testArgs);
if (commandLine.hasOption("A"))
{
System.out.print("Option A is present. The value is: ");
System.out.println(commandLine.getOptionValue("A"));
}
if (commandLine.hasOption("r"))
{
System.out.print("Option r is present. The value is: ");
System.out.println(commandLine.getOptionValue("r"));
}
if (commandLine.hasOption("S"))
{
System.out.print("Option S is present. The value is: ");
System.out.println(commandLine.getOptionValue("S"));
}
if (commandLine.hasOption("test"))
{
System.out.println("Option test is present. This is a flag option.");
}
{
String[] remainder = commandLine.getArgs();
System.out.print("Remaining arguments: ");
for (String argument : remainder)
{
System.out.print(argument);
System.out.print(" ");
}
System.out.println();
}
}
catch (ParseException exception)
{
System.out.print("Parse error: ");
System.out.println(exception.getMessage());
}
}
You could just do it manually.
NB: might be better to use a HashMap instead of an inner class for the opts.
/** convenient "-flag opt" combination */
private class Option {
String flag, opt;
public Option(String flag, String opt) { this.flag = flag; this.opt = opt; }
}
static public void main(String[] args) {
List<String> argsList = new ArrayList<String>();
List<Option> optsList = new ArrayList<Option>();
List<String> doubleOptsList = new ArrayList<String>();
for (int i = 0; i < args.length; i++) {
switch (args[i].charAt(0)) {
case '-':
if (args[i].length < 2)
throw new IllegalArgumentException("Not a valid argument: "+args[i]);
if (args[i].charAt(1) == '-') {
if (args[i].length < 3)
throw new IllegalArgumentException("Not a valid argument: "+args[i]);
// --opt
doubleOptsList.add(args[i].substring(2, args[i].length));
} else {
if (args.length-1 == i)
throw new IllegalArgumentException("Expected arg after: "+args[i]);
// -opt
optsList.add(new Option(args[i], args[i+1]));
i++;
}
break;
default:
// arg
argsList.add(args[i]);
break;
}
}
// etc
}