Created
June 19, 2019 17:21
-
-
Save nickname55/880addec70a8303b2359680376d5d066 to your computer and use it in GitHub Desktop.
apache commons cli example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import org.apache.commons.cli.CommandLine; | |
import org.apache.commons.cli.CommandLineParser; | |
import org.apache.commons.cli.DefaultParser; | |
import org.apache.commons.cli.HelpFormatter; | |
import org.apache.commons.cli.Option; | |
import org.apache.commons.cli.Options; | |
import org.apache.commons.cli.ParseException; | |
public class Application { | |
public static void main(String[] args) { | |
Options options = new Options(); | |
options.addOption(Option.builder("i") | |
.longOpt("interactionId") | |
.hasArg(true) | |
.desc("interaction id ([REQUIRED] or use --clientId)") | |
.required(false) | |
.build()); | |
options.addOption(Option.builder("c") | |
.longOpt("clientId") | |
.hasArg(true) | |
.desc("client id ([REQUIRED] or use --interactionId)") | |
.required(false) | |
.build()); | |
options.addOption(Option.builder("f") | |
.longOpt("file") | |
.hasArg(true) | |
.desc("[REQUIRED] one log-file or list of many log-files as input for log-parser") | |
.numberOfArgs(Option.UNLIMITED_VALUES) | |
.required() | |
.build()); | |
CommandLineParser parser = new DefaultParser(); | |
CommandLine cmd = null; | |
try { | |
cmd = parser.parse(options, args); | |
if (cmd.hasOption("c")) { | |
String clientId = cmd.getOptionValue("c"); | |
System.out.println("We have --clientId option = " + clientId); | |
if (cmd.hasOption("i")) { | |
System.out.println("( --interactionId option is omitted because --clientId option is defined)"); | |
} | |
} else if (cmd.hasOption("i")) { | |
String interactionId = cmd.getOptionValue("i"); | |
System.out.println("We have --interactionId option " + interactionId); | |
if (cmd.hasOption("c")) { | |
System.out.println("( --clientId option is omitted because --interactionId option is defined)"); | |
} | |
} else { | |
System.out.println("please specify one of the command line options: " | |
+ " -c,--c <arg> client id\n" | |
+ "OR\n" | |
+ " -i,--interaction <arg> interaction id"); | |
} | |
if (cmd.hasOption("f")) { | |
String[] files = cmd.getOptionValues("f"); | |
System.out.println("Number of files: " + files.length); | |
System.out.println("FileName(s): " + String.join(", ", Arrays.asList(files))); | |
} | |
} catch (ParseException pe) { | |
System.out.println("Error parsing command-line arguments!"); | |
System.out.println("Please, follow the instructions below:"); | |
HelpFormatter formatter = new HelpFormatter(); | |
formatter.printHelp( "Log messages to sequence diagrams converter", options ); | |
System.exit(1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment