I'd like to add this here as an answer to @Zangdak and to add my findings on the same problem.

If you do not call #setArgs(int) then a RuntimeException will occur. When you know the exact maximum amount of arguments to this option, then set this specific value. When this value is not known, the class Option has a constant for it: Option.UNLIMITED_VALUES

This would change gerrytans answer to the following:

Options options = new Options();
Option option = new Option("c", "c desc");
// Set option c to take 1 to oo arguments
option.setArgs(Option.UNLIMITED_VALUES);
options.addOption(option);
Answer from VeikkoW on Stack Overflow
🌐
Apache Commons
commons.apache.org › cli
Apache Commons CLI – Apache Commons CLI
November 8, 2025 - Apache Commons, Apache Commons CLI, Apache, the Apache logo, and the Apache Commons project logos are trademarks of The Apache Software Foundation.
🌐
Maven Repository
mvnrepository.com › artifact › commons-cli › commons-cli
Maven Repository: commons-cli » commons-cli
November 13, 2025 - Apache Commons CLI provides a simple API for presenting, processing, and validating a Command Line Interface.
🌐
GitHub
github.com › apache › commons-cli
GitHub - apache/commons-cli: Apache Commons CLI · GitHub
Apache Commons CLI provides a simple API for presenting, processing, and validating a Command Line Interface.
Starred by 389 users
Forked by 249 users
Languages   Java 97.4% | JavaScript 2.4%
🌐
Baeldung
baeldung.com › home › java › intro to the apache commons cli
Intro to the Apache Commons CLI | Baeldung
February 20, 2025 - The library can speed up the development of CLIs with its support for defining the CLI options and basic validation of them. It helps parse the command line arguments and their values. Finally, the argument values can be passed to the underlying services implementing the tool. Notably, the Apache Commons CLI library is also used in several of Apache’s products, including Kafka, Maven, Ant, and Tomcat.
🌐
Javadoc.io
javadoc.io › doc › commons-cli › commons-cli › latest › index.html
Apache Commons CLI 1.11.0 API
Bookmarks · Latest version of commons-cli:commons-cli · https://javadoc.io/doc/commons-cli/commons-cli · Current version 1.11.0 · https://javadoc.io/doc/commons-cli/commons-cli/1.11.0 · package-list path (used for javadoc generation -link option) · https://javadoc.io/doc/commons-cli/...
🌐
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.
Find elsewhere
🌐
Maven Central
central.sonatype.com › artifact › commons-cli › commons-cli
Maven Central: commons-cli:commons-cli
--> <project xmlns="http://mav... <inceptionYear>2002</inceptionYear> <description> Apache Commons CLI provides a simple API for presenting, processing, and validating a Command Line Interface....
🌐
Apache Commons
commons.apache.org › proper › commons-cli › apidocs › org › apache › commons › cli › CommandLine.html
CommandLine (Apache Commons CLI 1.11.0 API)
org.apache.commons.cli.CommandLine · All Implemented Interfaces: Serializable · public class CommandLine extends Object implements Serializable · Represents list of arguments parsed against a Options descriptor. It allows querying of a boolean hasOption(String optionName), in addition to ...
Top answer
1 of 4
30

This works for me (other parsers can be derived, too):

public class ExtendedGnuParser extends GnuParser {

    private boolean ignoreUnrecognizedOption;

    public ExtendedGnuParser(final boolean ignoreUnrecognizedOption) {
        this.ignoreUnrecognizedOption = ignoreUnrecognizedOption;
    }

    @Override
    protected void processOption(final String arg, final ListIterator iter) throws     ParseException {
        boolean hasOption = getOptions().hasOption(arg);

        if (hasOption || !ignoreUnrecognizedOption) {
            super.processOption(arg, iter);
        }
    }

}
2 of 4
9

As mentioned in a comment, the accepted solution is no more suitable because the processOption method has been deprecated and removed.

Here's my solution:

public class ExtendedParser extends DefaultParser {

    private final ArrayList<String> notParsedArgs = new ArrayList<>();

    public String[] getNotParsedArgs() {
        return notParsedArgs.toArray(new String[notParsedArgs.size()]);
    }

    @Override
    public CommandLine parse(Options options, String[] arguments, boolean stopAtNonOption) throws ParseException {
        if(stopAtNonOption) {
            return parse(options, arguments);
        }
        List<String> knownArguments = new ArrayList<>();
        notParsedArgs.clear();
        boolean nextArgument = false;
        for (String arg : arguments) {
            if (options.hasOption(arg) || nextArgument) {
                knownArguments.add(arg);
            } else {
                notParsedArgs.add(arg);
            }

        nextArgument = options.hasOption(arg) && options.getOption(arg).hasArg();
        }
        return super.parse(options, knownArguments.toArray(new String[knownArguments.size()]));
    }

}

Compared with the solution proposed by Pascal, it also checks for options with arguments and it keeps not parsed args in a separate list.

🌐
Apache Commons
commons.apache.org › cli › dependency-info.html
Maven Coordinates – Apache Commons CLI
Apache Commons, Apache Commons CLI, Apache, the Apache logo, and the Apache Commons project logos are trademarks of The Apache Software Foundation.
🌐
Apothem
apothem.blog › apache-commons-cli.html
Apache Commons CLI | APOTHEM
August 31, 2020 - A common task when developing user-facing applications is to parse command line parameters to turn them into runtime options. 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!
🌐
Apache Commons
commons.apache.org › exec › commandline.html
Apache Commons Exec - Building the command line – Apache Commons Exec
Apache Commons, Apache Commons Exec, Apache, the Apache logo, and the Apache Commons project logos are trademarks of The Apache Software Foundation.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Intro to the Apache Commons CLI - Java Code Geeks
May 24, 2024 - The Apache Commons CLI library is a powerful tool that enables developers to efficiently create command line interfaces (CLIs) for software applications. This framework streamlines the development process by offering support for defining CLI ...
🌐
Picocli
picocli.info › migrating-from-commons-cli.html
Migrating from Commons CLI to picocli
November 19, 2018 - Apache Commons CLI, initially released in 2002, is perhaps the most widely used java command line parser, but its API shows its age. Applications looking for a modern approach with a minimum of boilerplate code may be interested in picocli. Why is it worth the trouble to migrate, and how do ...
🌐
TutorialsPoint
tutorialspoint.com › commons_cli › index.htm
Apache Commons CLI Tutorial
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.
🌐
Oracle
docs.oracle.com › en › industries › health-sciences › healthcare-foundation › 8.2.4 › third-party-licenses › apache-commons-cli.html
Apache Commons CLI
August 19, 2024 - Apache Commons CLI Copyright 2002-2024 The Apache Software Foundation This product includes software developed at The Apache Software Foundation (http://www.apache.org/).
🌐
Apache
projects.apache.org › project.html
Apache Commons CLI - Apache Projects
This site relies heavily on JavaScript. Please enable it or get a browser that supports it · Managed by the Apache Community Development Project. Copyright© the Apache Software Foundation. Licensed under the Apache License, Version 2.0 Apache® and the Apache logo are trademarks of The Apache ...
🌐
Maven Repository
mvnrepository.com › artifact › commons-cli
Maven Repository: commons-cli
Apache Commons CLI provides a simple API for presenting, processing, and validating a Command Line Interface.