import com.github.yameaned.mainargs.MainArgs;
import com.github.yameaned.mainargs.MainArgsException;
public class MyProgram {
public static void main(String[] args) {
try {
MainArgs.parse(MyProgram.class, args);
} catch (MainArgsException e) {
e.printStackTrace();
}
}
public static void command(
@Parameter(description = "The first parameter")
String firstParam,
@Parameter(names = {"-s", "--secondParam"}, description = "The second parameter")
int secondParam
) {
System.out.println("First parameter: " + firstParam);
System.out.println("Second parameter: " + secondParam);
}
}
import org.apache.commons.cli.*;
public class MyProgram {
public static void main(String[] args) {
Options options = new Options();
options.addOption("f", "firstParam", true, "The first parameter");
options.addOption("s", "secondParam", true, "The second parameter");
CommandLineParser parser = new DefaultParser();
try {
CommandLine cmd = parser.parse(options, args);
String firstParam = cmd.getOptionValue("f");
int secondParam = Integer.parseInt(cmd.getOptionValue("s"));
System.out.println("First parameter: " + firstParam);
System.out.println("Second parameter: " + secondParam);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
import com.beust.jcommander.*;
public class MyProgram {
@Parameter(names = {"-f", "--firstParam"}, description = "The first parameter", required = true)
private String firstParam;
@Parameter(names = {"-s", "--secondParam"}, description = "The second parameter", required = true)
private int secondParam;
public static void main(String[] args) {
MyProgram program = new MyProgram();
JCommander.newBuilder()
.addObject(program)
.build()
.parse(args);
System.out.println("First parameter: " + program.firstParam);
System.out.println("Second parameter: " + program.secondParam);
}
}