Questions tagged [apache-commons-cli]

The Apache Commons CLI library provides an API for parsing command line options passed to programs. It's also able to print help messages detailing the options available for a command line tool.

The Apache Commons CLI library provides an API for parsing command line options passed to programs. It's also able to print help messages detailing the options available for a command line tool.

Commons CLI supports different types of options:

  • POSIX like options (ie. tar -zxvf foo.tar.gz)
  • GNU like long options (ie. du --human-readable --max-depth=1)
  • Java like properties (ie. java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo)
  • Short options with value attached (ie. gcc -O2 foo.c)
  • long options with single hyphen (ie. ant -projecthelp)

A typical help message displayed by Commons CLI looks like this:

usage: ls
 -A,--almost-all          do not list implied . and ..
 -a,--all                 do not hide entries starting with .
 -B,--ignore-backups      do not list implied entries ending with ~
 -b,--escape              print octal escapes for nongraphic characters
    --block-size <SIZE>   use SIZE-byte blocks
 -c                       with -lt: sort by, and show, ctime (time of last
                          modification of file status information) with
                          -l:show ctime and sort by name otherwise: sort
                          by ctime
 -C                       list entries by columns
131 questions
42
votes
5 answers

Apache Commons CLI - option type and default value

How can I give a CLI Option a type - such as int or Integer? (Later, how can I get the parsed value with a single function call?) How can I give a CLI Option a default value? Such that CommandLine.getOptionValue() or the function call mentioned…
aib
  • 45,516
  • 10
  • 73
  • 79
40
votes
2 answers

Get name of executable jar from within main() method

I've created an executable jar and using commons-cli to give the user the ability to specify command line parameters when he launches the client. Everything works fine. However, when I print the usage statement for the jar, I would like to show…
Eric B.
  • 23,425
  • 50
  • 169
  • 316
30
votes
2 answers

Commons CLI required groups

I am writing command line application in Java and I've chosen Apache Commons CLI to parse input arguments. Let's say I have two required options (ie. -input and -output). I create new Option object and set required flag. For now it's all good. But I…
26
votes
2 answers

How to fetch parameters when using the Apache Commons CLI library

I'm using the Apache Commons CLI to handle command line arguments in Java. I've declared the a and b options and I'm able to access the value using CommandLine.getOptionValue(). Usage: myapp [OPTION] [DIRECTORY] Options: -a Option A -b …
Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
24
votes
2 answers

Why are detected CLI options different when using DefaultParser instead of GnuParser?

I'm about to migrate some legacy code to contain less deprecated warnings from 3rd-party libraries. For Apache commons-cli library (version: 1.3.1) I detected in the official JavaDoc that GnuParser is deprecated and DefaultParser should be used…
MWiesner
  • 8,868
  • 11
  • 36
  • 70
23
votes
3 answers

Apache CLI: Required options contradicts with help option.

If I have 2 options defined as required such as: public static void main(String [] args){ Options options= new Options(); Option inputFileOp=Option.builder("i").longOpt("input").hasArg().desc("Input…
Mohamed Gad-Elrab
  • 636
  • 1
  • 6
  • 20
22
votes
2 answers

Apache Commons CLI: replacement for deprecated OptionBuilder?

IntelliJ shows that OptionBuilder is deprecated in this example code from http://commons.apache.org/proper/commons-cli/usage.html. What should I use as the replacement? import org.apache.commons.cli.*; Options options = new…
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
21
votes
4 answers

Can Apache Commons CLI options parser ignore unknown command-line options?

I am writing a Java application that takes command line arguments which are processed using Apache Commons CLI with the GnuParser. For reasons that are not interesting to get into, I would like it to silently ignore unknown command line options…
Michael Ridley
  • 10,378
  • 3
  • 22
  • 16
18
votes
4 answers

run jar in debug mode from terminal

I'm using intellij idea IDE and I'm trying to run my jar file from terminal in debug mode and set breakpoints in a few places in the code. the command I'm using is: java -jar myTestApp.jar -file "myfile.txt" -users myUser -Xdebug…
Pawel
  • 417
  • 1
  • 6
  • 25
16
votes
3 answers

Apache Commons CLI 1.3.1: How to ignore unknown Arguments?

I used to work with Apache Commons Cli 1.2. I wanted the parser to ignore arguments if they are unknown (not added to an Options-Object). Example (pseudocode): Options specialOptions; specialOptions.addOption(null, "help", false, "shows…
leif_good
  • 376
  • 6
  • 19
13
votes
1 answer

Mutually exclusive options using Apache Commons CLI

I can create 2 mutually exclusive options using the following: Option a = OptionBuilder.create("a"); Option b = OptionBuilder.create("b"); OptionGroup optgrp = new OptionGroup(); optgrp .setRequired(true); optgrp .addOption(a); optgrp…
Rory
  • 221
  • 2
  • 7
13
votes
3 answers

Java commons-cli, options with list of possible values

How can I make an option accept only some specified values like in the following example: $ java -jar Mumu.jar -a foo OK $ java -jar Mumu.jar -a bar OK $ java -jar Mumu.jar -a foobar foobar is not a valid value for -a
13
votes
6 answers

Apache Commons CLI - ordering help options?

I'm using the Apache Commons CLI. By default it orders help options on the command line alphabetically by key. So, what appears is: -csv -ip -msisdn -xml But I want to order them as follows: -csv -xml -ip -msisdn I know that there is a…
Rory
  • 1,805
  • 7
  • 31
  • 45
12
votes
1 answer

Defining positional parameters with apache commons cli

I would like to define an Apache Commons CLI parser that includes named arguments and positional arguments. program [-a optA] [-b optB] [-f] pos1 pos2 How do I validate pos1 and pos2?
terrywb
  • 3,740
  • 3
  • 25
  • 50
11
votes
2 answers

Required argument without option name in Commons CLI

I'm trying to parse the command line arguments in Java to the following usage: Usage: gibl FILE -h, --help Displays this help message. -v, --version Displays the program version. FILE Source file. With the Apache Commons CLI…
user5549921
1
2 3
8 9