![]() ![]() HelpFormatter helper = new HelpFormatter() įinally, add some conditionals to analyze the options provided by the user as command-line input (discovered and stored in the cmd variable). define parserĬommandLineParser parser = new BasicParser() Finally, you can create a HelpFormatter object (which I call helper) to automatically provide helpful messages to the user when either a required option is missing, or the -help or -h option is used. To do this, you create an instance of the CommandLine itself, which contains all arguments provided by the user (valid options and otherwise.) You also create a CommandLineParser object, which I call parser in my code, to facilitate interaction over those arguments. With all possible options defined, you can now iterate over arguments provided by the user, checking to see whether any argument matches your approved list of valid short options. ![]() With the builder function, you can set the short version, long version, whether it's required (I set this to true in this code, so my application can't run unless this option is provided by the user at launch time), the help message, and so on. Once it's defined, you add the new option to your options group: Option config = Option.builder("c").longOpt("config") For this, you use the builder method, creating attributes for an option based on its short version (for example, -c is a short option, -config is a long option). You might want to let a user refer to a configuration file, an input file, or any setting like a date or a color. Sometimes you want users to provide information other than just true or false along with an option. Options.addOption(alpha) Defining options with arguments in Java Option alpha = new Option("a", "alpha", false, "Activate feature alpha") In this example, I create just one option, arbitrarily called alpha: //define options You then set whether the option is required or not, and finally add the option to the options object, which contains all of your options. Next, define your individual options by listing a short option, a long option, a default Boolean value, and a help message. ![]() Use the Option (singular) class to create option objects and the Options (plural) class to help keep track of all the options you've created in your project.įirst, create a group for your options, and call it options according to convention: //code The first thing you must do to parse options is to define the valid options your application can accept. For this simple option parsing example, you can populate a file called Main.java with the standard minimal code: package To use the commons-cli library in your code, you must import it. The only required JAR is the commons-cli-X.Y.jar (where X and Y are the latest version numbers.) Add that JAR to your project, either manually or in your IDE, and then you can use it in your code. If you're managing libraries manually, you can download the latest release from the Apache website. If you're using a project management system like Maven and an IDE, you can install the Apache Commons CLI library in your project properties (such as pom.xml or a configuration screen in Eclipse or NetBeans). My favorite is the Apache Commons CLI library, called commons-cli for short. There are several ways to parse options in Java. Free online course: Developing cloud-native applications with microservices architectures.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |