Apache Commons
commons.apache.org › proper › commons-cli › apidocs › org › apache › commons › cli › CommandLineParser.html
CommandLineParser (Apache Commons CLI 1.11.0 API)
declaration: package: org.apache.commons.cli, interface: CommandLineParser
GitHub
github.com › apache › commons-cli › blob › master › src › main › java › org › apache › commons › cli › CommandLineParser.java
commons-cli/src/main/java/org/apache/commons/cli/CommandLineParser.java at master · apache/commons-cli
package org.apache.commons.cli; · /** * A class that implements the {@code CommandLineParser} interface can parse a String array according to the · * {@link Options} specified and return a {@link CommandLine}. */ public interface CommandLineParser { ·
Author apache
Apache Commons
commons.apache.org › proper › commons-cli › apidocs › index.html
Overview (Apache Commons CLI 1.11.0 API)
This is commonly used to log usage of deprecated options rather than printing them on the standard output. ... void doSomething(Options options) { CommandLineParser parser = new DefaultParser(); CommandLine line; try { // parse the command line arguments line = parser.parse(options, new String[] ...
GitHub
github.com › apache › commons-cli › blob › master › src › main › java › org › apache › commons › cli › Parser.java
commons-cli/src/main/java/org/apache/commons/cli/Parser.java at master · apache/commons-cli
import org.apache.commons.cli.help.OptionFormatter; · /** * Creates {@link CommandLine} instances. * * @deprecated Since 1.3, the two-pass parsing with the flatten method is not enough flexible to handle complex cases. */ @Deprecated · public abstract class Parser implements CommandLineParser { ·
Author apache
Apache Commons
commons.apache.org › proper › commons-cli › apidocs › org › apache › commons › cli › DefaultParser.html
DefaultParser (Apache Commons CLI 1.11.0 API)
org.apache.commons.cli.DefaultParser · All Implemented Interfaces: CommandLineParser · public class DefaultParser extends Object implements CommandLineParser · Default parser. Since: 1.3 · clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait ·
TutorialsPoint
tutorialspoint.com › commons_cli › commons_cli_quick_guide.htm
Apache Commons CLI - Quick Guide
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.
TutorialsPoint
tutorialspoint.com › commons_cli › commons_cli_gnu_parser.htm
Apache Commons CLI - GNU Parser
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")); } } } Run the file while passing -p -g -n 10 as option and see the result.
Apache Commons
commons.apache.org › cli › apidocs › index.html
Package org.apache.commons.cli
The Apacahe Commons CLI component parses command-line arguments for your application.
Top answer 1 of 4
51
I used GnuParser() instead of DefaultParser() and it works well.
CommandLineParser parser = new GnuParser();
Update : In version 1.3.1 of CLI, GnuParser() is now deprecated. So I simply added import org.apache.commons.cli.DefaultParser;
and now I use CommandLineParser parser = new DefaultParser();
And all is fine!
2 of 4
13
What version of CLI are you using? DefaultParser wasn't added until the 1.3 snapshot.
http://commons.apache.org/proper/commons-cli/apidocs/org/apache/commons/cli/DefaultParser.html
Apache Commons
commons.apache.org › proper › commons-cli › apidocs › org › apache › commons › cli › Parser.html
Parser (Apache Commons CLI 1.11.0 API)
org.apache.commons.cli.Parser · All Implemented Interfaces: CommandLineParser · Direct Known Subclasses: BasicParser, GnuParser, PosixParser · @Deprecated public abstract class Parser extends Object implements CommandLineParser · Deprecated. Since 1.3, the two-pass parsing with the flatten method is not enough flexible to handle complex cases.
TutorialsPoint
tutorialspoint.com › commons_cli › commons_cli_usage_example.htm
Apache Commons CLI - Usage Example
package com.tutorialspoint; import java.io.IOException; 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; import org.apache.commons.cli.help.HelpFormatter; public class CLITester { public static void main(String[] args) throws ParseException, IOException { Options options = new Options(); options.addOption("p", "print", false, "Send print request to printer.") .addOption("g", "gui", false, "Show GUI Application") .addOption("n", true, "No.
Blogger
marxsoftware.blogspot.com › 2017 › 06 › apache-commons-cli.html
Inspired by Actual Events: Java Command-Line Interfaces (Part 1): Apache Commons CLI
June 20, 2017 - The Apache Commons CLI documentation's "Introduction" explains how Commons CLI accomplishes the "three stages [of] command line processing" ("definition", "parsing", and "interrogation"). These three stages map in Commons CLI to classes Option and Options ("definition"), to interface CommandLineParser ("parsing"), and to class CommandLine ("interrogation").
Liviutudor
liviutudor.com › home › blogroll › using apache commons cli to parse arguments
Using Apache Commons CLI to Parse Arguments | Liviu Tudor -- Of Man and Internet
April 18, 2013 - import java.io.File; import java.io.FileInputStream; import java.net.URL; import java.util.Date; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.cli.PosixParser; /** * Shows the usage of <a href="http://commons.apache.org/proper/commons-cli/" * target="_blank">Apache Commons CLI</a> with typed arguments.
Apothem
apothem.blog › apache-commons-cli.html
Apache Commons CLI | APOTHEM
August 31, 2020 - The task is certainly repetitive and prone to error when it is reimplemented time and again, but another little library from the Apache Commons project makes it definitely an easier and more enjoyable experience: enter Apache Commons CLI! Index: Setting up · Stages of processing · Definition stage: the Options class · Parsing stage: the CommandLineParser class ·
Blogger
self-learning-java-tutorial.blogspot.com › 2016 › 12 › commons-cli-parsing-command-line-options.html
Programming for beginners: Commons CLI: Parsing command line options
December 16, 2016 - 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 FileUpper { public static void main(String[] args) throws Exception { Options options = new Options(); Option input = new Option("i", "input", true, "input file to read data from"); input.setRequired(true); input.setArgName("FILE PATH"); options.addOption(input); Option output = ne