public class CommandLine extends Object
This parser parses a String array containing the command-line
parameters into options as defined by
CommandLine(String[], Option...). Include code like the following in
your main startup method:
List<Option> options = new ArrayList<Option>();
options.add(new Option('h', "help"));
options.add(new Option('C', "config-file", true));
CommandLine commandLine = new CommandLine(arguments, options);
After the command-line has been parsed successfully, querying it is quite
simple:
if (commandLine.getOption("h").isPresent()) {
showHelp();
return;
}
if (commandLine.getOption("C").isPresent()) {
String configFile = commandLine.getOption("C").getValue();
}
Additional arguments on the command line can be queried via the
getArguments() method. A line like
program -C config.txt file1.txt file2.txt with the constructor
from above and the code below would result in the output below the code:
for (String argument : commandLine.getArguments()) {
System.out.println(argument);
}
Output:
file1.txt file2.txt
| Constructor and Description |
|---|
CommandLine(String[] commandLineArguments,
Collection<Option> options)
Creates a new command line and parses the given command-line arguments
according to the given options.
|
CommandLine(String[] commandLineArguments,
Option... options)
Creates a new command line and parses the given command-line arguments
according to the given options.
|
| Modifier and Type | Method and Description |
|---|---|
String[] |
getArguments()
Returns all remaining arguments from the original command-line arguments.
|
Option |
getOption(char name)
Returns the option with the given name.
|
Option |
getOption(String name)
Returns the option with the given name.
|
public CommandLine(String[] commandLineArguments, Collection<Option> options) throws CommandLineException
commandLineArguments - The command-line argumentsoptions - The optionsCommandLineException - if a command-line argument could not be parsedpublic CommandLine(String[] commandLineArguments, Option... options) throws CommandLineException
commandLineArguments - The command-line argumentsoptions - The optionsCommandLineException - if a command-line argument could not be parsedpublic Option getOption(char name)
null is returned.name - The short name of the optionnull if no option could be foundpublic Option getOption(String name)
null is returned.name - The long or short name of the optionnull if no option could be foundpublic String[] getArguments()
Copyright © 2026. All rights reserved.