Skip to content

Instantly share code, notes, and snippets.

@OlivierLD
Last active October 20, 2023 06:50
Show Gist options
  • Save OlivierLD/93907af6254f02b93ed9c8b4eb54c184 to your computer and use it in GitHub Desktop.
Save OlivierLD/93907af6254f02b93ed9c8b4eb54c184 to your computer and use it in GitHub Desktop.
Manage CLI parameters from Java

Manage Command Lin Interface (CLI) Parameters from Java

This is just an example, a suggestion, a "generic" way to do the job.
Like when you have the possibility to add those parameters to your command line, like in

$ java -cp archive.jar sample.SampleMain --first-prm:value01 --second-prm:123 --third-prm:true

The goal here is to show a way to parse those parameter and extract the expected values, for the rest of the process to use them.

Look into the code, how we are using BiConsumer<String, CLIPrm>. The parsing itself happens in the main method, obviously.


$ java -cp archive.jar sample.SampleMain --help

would display

CLI Parameters:
--help, Help!!!
--first-prm:, String example.
--second-prm:, Headless version.
--third-prm:, Headless version.

And

$ java -cp archive.jar sample.SampleMain --first-prm:value01 --second-prm:123 --third-prm:true

would display

Managed --first-prm:, String example., --first-prm:value01
Managed --second-prm:, Int example., --second-prm:123
Managed --third-prm:, Boolean example., --third-prm:true
Value One: value01
Value Two: 123
Value Three: true
. . .
package sample;
import java.util.Arrays;
import java.util.function.BiConsumer;
public class SampleMain {
private final static String HELP_PRM = "--help";
private final static String FIRST_PRM = "--first-prm:";
private final static String SECOND_PRM = "--second-prm:";
private final static String THIRD_PRM = "--third-prm:";
public static class CLIPrm {
private final String prefix;
private final String desc;
private final BiConsumer<String, CLIPrm> action;
public CLIPrm(String prefix, String desc, BiConsumer<String, CLIPrm> action) {
this.prefix = prefix;
this.desc = desc;
this.action = action;
}
public String getPrefix() {
return prefix;
}
public String getDesc() {
return desc;
}
public BiConsumer<String, CLIPrm> getAction() {
return action;
}
}
// The values depending on CLI prms
private static String valueOne;
private static int valueTwo = 0;
private static boolean valueThree = false;
private static CLIPrm[] CLI_PRMS = {
new CLIPrm(HELP_PRM, "Help!!!", (str, prm) -> {
System.out.println("CLI Parameters:");
displayHelp();
System.exit(0);
}),
new CLIPrm(FIRST_PRM, "String example.", (str, prm) -> {
valueOne = str.substring(prm.getPrefix().length());
System.out.printf("Managed %s, %s, %s\n", prm.getPrefix(), prm.getDesc(), str);
}),
new CLIPrm(SECOND_PRM, "Int example.", (str, prm) -> {
try {
valueTwo = Integer.parseInt(str.substring(prm.getPrefix().length()));
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
System.out.printf("Managed %s, %s, %s\n", prm.getPrefix(), prm.getDesc(), str);
}),
new CLIPrm(THIRD_PRM, "Boolean example.", (str, prm) -> {
valueThree = "true".equals(str.substring(prm.getPrefix().length()));
System.out.printf("Managed %s, %s, %s\n", prm.getPrefix(), prm.getDesc(), str);
}),
};
private static void displayHelp() {
Arrays.asList(CLI_PRMS).forEach(prm -> System.out.printf("%s, %s\n", prm.getPrefix(), prm.getDesc()));
}
public static void main(String... args) {
for (String arg : args) {
final CLIPrm cliPrm = Arrays.asList(CLI_PRMS).stream()
.filter(prm -> arg.startsWith(prm.getPrefix()))
.findFirst()
.orElse(null);
if (cliPrm != null) {
cliPrm.getAction().accept(arg, cliPrm);
} else {
System.out.printf("CLI Prm [%s] not managed.\n", arg);
}
}
// Now, move on !
System.out.printf("Value One: %s\n", valueOne);
System.out.printf("Value Two: %d\n", valueTwo);
System.out.printf("Value Three: %s\n", valueThree);
// . . .
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment