🌐
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 ...
🌐
Apache Commons
commons.apache.org › proper › commons-exec › apidocs › org › apache › commons › exec › CommandLine.html
CommandLine (Apache Commons Exec 1.6.0 API)
org.apache.commons.exec.CommandLine · public class CommandLine extends Object · CommandLine objects help handling command lines specifying processes to execute. The class can be used to a command line by an application. Constructors · Constructor · Description ·
🌐
Apache Commons
commons.apache.org › cli
Apache Commons CLI – Apache Commons CLI
November 8, 2025 - The Apache Commons CLI library provides an API for parsing command-line options passed to an application. It can also print help detailing the options available for that application · Commons CLI supports different types of options:
🌐
Apache Commons
commons.apache.org › exec › commandline.html
Apache Commons Exec - Building the command line – Apache Commons Exec
No matter which approach you are using commons-exec does change your command line arguments in the following two cases · The following executable arguments
🌐
Apache Commons
commons.apache.org › cli › apidocs › org › apache › commons › cli › CommandLine.html
CommandLine (Apache Commons CLI 1.8.1-SNAPSHOT API)
October 23, 2021 - 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 ...
🌐
DZone
dzone.com › coding › java › java command-line interfaces (part 1): apache commons cli
Java Command-Line Interfaces (Part 1): Apache Commons CLI
June 22, 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").
🌐
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.
Top answer
1 of 1
3

Here's a very stripped down version of something I use. It basically sets up a singleton that gets initialized once and then can be used wherever you want within your program. I chose to store the info in HashMap's and ArrayList's because they were easier to deal with later.

//****************************************************************************
//***** File Name: MianCLIOptions.java
//****************************************************************************

package com.test.mian;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.cli.AlreadySelectedException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.MissingArgumentException;
import org.apache.commons.cli.MissingOptionException;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.UnrecognizedOptionException;
import org.apache.commons.lang.WordUtils;


//****************************************************************************
//****************************************************************************
//****************************************************************************
//****************************************************************************
public class MianCLIOptions
{
    //***** constants *****

    //***** public data members *****

    //***** private data members *****
    private static MianCLIOptions singletonObj = null;

    private HashMap<String,Object> options = new HashMap<String,Object>();
    private ArrayList<String> arguments = new ArrayList<String>();

    //****************************************************************************
    public static MianCLIOptions getopts()
    {
        if (singletonObj == null) {
            throw new IllegalStateException("[MianCLIOptions] Command line not yet initialized.");
        }

        return singletonObj;
    }

    //****************************************************************************
    public static synchronized void initialize(Options optsdef, String[] args)
        throws MianCLIOptionsException, UnrecognizedOptionException, MissingArgumentException,
               MissingOptionException, AlreadySelectedException, ParseException
    {
        if (singletonObj == null) {
            singletonObj = new MianCLIOptions(optsdef, args);
        }
        else {
            throw new IllegalStateException("[MianCLIOptions] Command line already initialized.");
        }
    }

    //****************************************************************************
    //----- prevent cloning -----
    public Object clone() throws CloneNotSupportedException
    {
        throw new CloneNotSupportedException();
    }

    //****************************************************************************
    public boolean isset(String opt)
    {
        return options.containsKey(opt);
    }

    //****************************************************************************
    public Object getopt(String opt)
    {
        Object rc = null;

        if (options.containsKey(opt)) {
            rc = options.get(opt);
        }

        return rc;
    }

    //****************************************************************************
    //***** finally parse the command line
    //****************************************************************************
    private MianCLIOptions(Options optsdef, String[] args)
        throws UnrecognizedOptionException, MissingArgumentException,
               MissingOptionException, AlreadySelectedException, ParseException
    {
        //***** (blindly) parse the command line *****
        CommandLineParser parser = new GnuParser();
        CommandLine cmdline = parser.parse(optsdef, args);

        //***** store options and arguments *****
        //----- options -----
        for (Option opt : cmdline.getOptions()) {
            String key = opt.getOpt();
            if (opt.hasArgs()) {
                options.put(key, opt.getValuesList());
            }
            else {
                options.put(key, opt.getValue());
            }
        }

        //----- arguments -----
        for (String str : cmdline.getArgs()) {
            //----- account for ant/build.xml/generic -----
            if (str.length() > 0) {
                arguments.add(str);
            }
        }
    }
}

//****************************************************************************
//***** EOF  ***** EOF  ***** EOF  ***** EOF  ***** EOF  ***** EOF  **********

In your main, you can then call it like so:

    //***** build up options *****
    Options options = new Options();

    // ... .... ...

    //***** process command line *****
    try {
        MianCLIOptions.initialize(options, args);
    }
    catch (UnrecognizedOptionException ex) {
        // do something
    }

And finally, in some other class you can call it like so:

    MianCLIOptions opts = MianCLIOptions.getopts();
    if (opts.isset("someopt")) {
        // do something exciting
    }

Hope that helps!

🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
Find elsewhere
🌐
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.
🌐
GitHub
github.com › apache › commons-exec › blob › master › src › main › java › org › apache › commons › exec › CommandLine.java
commons-exec/src/main/java/org/apache/commons/exec/CommandLine.java at master · apache/commons-exec
import org.apache.commons.exec.util.StringUtils; · /** * CommandLine objects help handling command lines specifying processes to execute. The class can be used to a command line by an application. */ public class CommandLine { · /** * Encapsulates a command line argument.
Author   apache
🌐
Lyris
lunar.lyris.com › help › lm_help › 4.0 › ApacheCommandLine.html
Apache Command Line
Lyris User's Guide [previous] [next] [contents] Apache Command Line Table of Contents Introduction Email Commands Web Interface for Users Server Administrator Site Administrator List Administrator Other Topics Add-On Packages NT Authentication Option Lyris List Manager ODBC Driver Random Document ...
🌐
Linuxize
linuxize.com › home › apache › apache commands: manage and troubleshoot your web server
Apache Commands: Manage and Troubleshoot Your Web Server | Linuxize
April 8, 2026 - Quick reference for essential Apache commands on Linux. Covers service control, configuration testing, modules, virtual hosts, version info, and log files.
🌐
TecAdmin
tecadmin.net › common-apache-commands-on-ubuntu-debian
Common Apache Commands on Ubuntu & Debian – TecAdmin
April 26, 2025 - Use -v command-line option to check the running Apache version on Ubuntu and other Debina-based systems.
🌐
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.
🌐
Apache Commons
commons.apache.org › proper › commons-cli › apidocs › index.html
Overview (Apache Commons CLI 1.11.0 API)
The parse methods of CommandLineParser are used to parse the command line arguments. There may be several implementations of the CommandLineParser interface, the recommended one is the DefaultParser.