Apache Commons CLI – GNU Parser ”; Previous Next A GNU parser is use to parse gnu like arguments passed. It is now deprecated and is replaced by DefaultParser. Example CLITester.java import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.GnuParser; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; public class CLITester { public static void main(String[] args) throws ParseException { //Create GNU like options Options gnuOptions = new Options(); gnuOptions.addOption(“p”, “print”, false, “Print”) .addOption(“g”, “gui”, false, “GUI”) .addOption(“n”, true, “Scale”); CommandLineParser gnuParser = new GnuParser(); CommandLine cmd = gnuParser.parse(gnuOptions, args); if( cmd.hasOption(“p”) ) { System.out.println(“p option was used.”); } if( cmd.hasOption(“g”) ) { System.out.println(“g option was used.”); } if( cmd.hasOption(“n”) ) { System.out.println(“Value passed: ” + cmd.getOptionValue(“n”)); } } } Output Run the file while passing -p -g -n 10 as option and see the result. java CLITester -p -g -n 10 p option was used. g option was used. Value passed: 10 Print Page Previous Next Advertisements ”;
Category: commons Cli
Discussion
Discuss Apache Commons CLI ”; Previous Next The Apache Commons CLI are the components of the Apache Commons which are derived from Java API and provides an API to parse command line arguments/options which are passed to the programs. This API also enables to print the help related to options available. This tutorial covers most of the topics required for a basic understanding of Apache Commons CLI and to get a feel of how it works. Print Page Previous Next Advertisements ”;
Boolean Option
Apache Commons CLI – Boolean Option ”; Previous Next A boolean option is represented on a command line by its presence. For example, if option is present, then its value is true, otherwise, it is considered as false. Consider the following example, where we are printing current date and if -t flag is present. Then, we will print time too. Example CLITester.java import java.util.Calendar; import java.util.Date; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; public class CLITester { public static void main(String[] args) throws ParseException { Options options = new Options(); options.addOption(“t”, false, “display time”); CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse( options, args); Calendar date = Calendar.getInstance(); int day = date.get(Calendar.DAY_OF_MONTH); int month = date.get(Calendar.MONTH); int year = date.get(Calendar.YEAR); int hour = date.get(Calendar.HOUR); int min = date.get(Calendar.MINUTE); int sec = date.get(Calendar.SECOND); System.out.print(day + “/” + month + “/” + year); if(cmd.hasOption(“t”)) { System.out.print(” ” + hour + “:” + min + “:” + sec); } } } Output Run the file without passing any option and see the result. java CLITester 12/11/2017 Run the file, while passing -t as option and see the result. java CLITester 12/11/2017 4:13:10 Print Page Previous Next Advertisements ”;
Argument Option
Apache Commons CLI – Argument Option ”; Previous Next An Argument option is represented on a command line by its name and its corresponding value. For example, if option is present, then user has to pass its value. Consider the following example, if we are printing logs to some file, for which, we want user to enter name of the log file with the argument option logFile. Example CLITester.java import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; public class CLITester { public static void main(String[] args) throws ParseException { Options options = new Options(); Option logfile = Option.builder() .longOpt(“logFile”) .argName(“file” ) .hasArg() .desc(“use given file for log” ) .build(); options.addOption(logfile); CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse( options, args); // has the logFile argument been passed? if(cmd.hasOption(“logFile”)) { //get the logFile argument passed System.out.println( cmd.getOptionValue( “logFile” ) ); } } } Output Run the file, while passing –logFile as option, name of the file as value of the option and see the result. java CLITester –logFile test.log test.log Print Page Previous Next Advertisements ”;