A command line argument parser utility for Java
Explore the docs »
Report Bug
·
Request Feature
Table of Contents
EasyCli is a light-weight command line argument parser for Java.
- Java
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
You need to have Java and Maven installed on your local machine.
These steps will install EasyCli on your local machine:
- Clone this repository:
$ git clone https://github.com/egehurturk/EasyCLI.git
- Install the project (
pom.xml
) into local maven repository.- This step will install the
JAR
andpom.xml
files to~/.m2/repository/
- This step will install the
$ cd EasyCLI
$ ./mvnw package
$ mvn install
- Add the following dependency to your Maven generated project:
<dependencies>
<!-- Add this snippet under <dependencies> in pom.xml -->
<dependency>
<groupId>com.easycli</groupId>
<artifactId>EasyCLI</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
- Use the API!
You can create command line arguments and flags with EasyCli API:
Arg directory = Arg.with()
.longOptionName("directory")
.shortOptionName("d")
.argName("project_dir")
.description("Maven generated project path")
.required()
.alias("dir")
.build();
longOptionName(String)
: sets the long name, i.e., the name that will be visible with--
prefix. For instance, if you set it asdirectory
, then it will be accessed with--directory
.shortOptionName(String)
: sets the short name, i.e., the name that will be visible with-
prefix. For instance, if you set is asd
, then it will be accessed with-d
.argName(String)
: sets the argument name that will be displayed in the help message.description(String)
: sets the description for the argument and will be displayed in the help message.required()
: marks theArg
as required. The opposite method isoptional()
, which marks theArg
as optional.alias(String)
: gives an alias value for the argument. This value is used to retrieve the value of anArg
.build()
: builds theArg
The code above will create an Arg
. An Arg
in a command line is a special parameter that has a value. For instance:
$ ./executable --directory /dev/null
Here, the --directory
is defined as an Arg
, which has a value after the flag.
Flag version = Flag.with()
.longOptionName("version")
.shortOptionName("v")
.description("Get the latest version")
.optional()
.alias("version")
.build();
longOptionName(String)
: sets the long name, i.e., the name that will be visible with--
prefix. For instance, if you set it asversion
, then it will be accessed with--version
.shortOptionName(String)
: sets the short name, i.e., the name that will be visible with-
prefix. For instance, if you set is asv
, then it will be accessed with-v
.description(String)
: sets the description for the argument and will be displayed in the help message.optional()
: marks theFlag
as required. The opposite method isrequired()
, which marks theFlag
as optional.alias(String)
: gives an alias value for the argument.build()
: builds theFlag
The code above will create a Flag
. A Flag
is a command line parameter that does not have a value. For instance,
./executable --version
Here, the --version
is defined as a Flag
, which does not have a value and is an indicator.
CmdOptions
is a class that stores Arg
s and Flag
s. You can create a new CmdOptions
with either:
CmdOptions options = new CmdOptions(arg1, flag1, /*...*/);
or
CmdOptions options = new CmdOptions();
options.add(arg1);
options.add(flag1);
//...
This class is the main class that provides methods to check and retrieve command line arguments. You can create a new EasyCli
with:
EasyCli cli = new EasyCli(args, options);
args
is the argument array. This must be the parameter of the main method:public static void main(String[] args)
options
is theCmdOptions
class that storesArg
s andFlag
s
EasyCli
class provides a method to check if all required Arg
s and Flag
s exist in the args
array. This method is matchAllArgs
and returns a boolean:
boolean success = cli.matchAllArgs();
- A typical usage of this method is to print the help message when required parameters do not exist in the array:
if (!cli.matchAllArgs()) { EasyCli.Synopsis helper = cli.new Synopsis("<exec_name>"); helper.print(); }
You can check if a parameter (an Arg
or a Flag
) exists with the has(String)
method. The method accepts the alias value set for the particular parameter:
boolean versionExists = cli.has("version");
- Here, the
"version"
is the alias name
You can get Arg
values with the get(String)
method. The method accepts the alias value set for an Arg
:
String dirValue = cli.get("dir");
- If a
Flag
alias is given, then the method throwsIllegalArgumentException
- If the
Arg
with the given alias is not found, the return value will benull
.- You can combine
has(String)
andget(String)
:if (cli.has("dir")) String dir = cli.get("dir");
- This will prevent
dir
from beingnull
.
- This will prevent
- You can combine
This class provides a method to print the synopsis of the given CmdOptions
object. You can instantiate this class with:
EasyCli.Synopsis synopsis = cli.new Synopsis("<executable>");
"<executable>"
is the name of the executable you are running.
You can print the help message with the print()
method:
synopsis.print();
This will produce an output like:
Usage: <executable> -d|--dir <project_dir> [-v|--version] [-h|--help]
-d|--directory <project_dir> Maven generated project
-v|--version Get the latest version
-h|--help Print this message
[]
indicates that the value is optional.|
is a separator for mutually exclusive items; choose one<>
is a placeholder for which you must supply a value
The recommended way to use EasyCli
is like this:
EasyCli cli = new EasyCli(args, options /* CmdOptions object */);
if (!cli.matchAllArgs()) {
EasyCli.Synopsis help = cli.new Synopsis("<exec_name>");
help.print();
System.exit(1);
}
String dirVal = cli.get("dir");
//...
See the open issues for a list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Your Name - @egehurturk - ege.hurturk@gmail.com
Project Link: https://github.com/egehurturk/EasyCli