CLI Helper - library to help implementing commandline interfaces
Table of Contents
1. Overview
- See also: CLI Helper JavaDoc
This is library intended to facilitate creation of commandline applications in Java programming language. Library is packaged as an artifact to Maven repository. This makes it simple to add library as dependency to your project.
Library provides following general functionalities:
2. User input helper
The CLIHelper
provides user-friendly methods to read different data
types from standard input. It helps validate user input, display
prompts, handle default values, and enforce optional constraints like
numeric ranges or string lengths.
For quick usage, here’s a simple example showing how you might query users for a boolean value, an integer, and a string:
import eu.svjatoslav.commons.cli_helper.CLIHelper; public class Demo { public static void main(String[] args) { Boolean proceed = CLIHelper.askBoolean("Do you want to proceed?", true); Integer age = CLIHelper.askInteger("Please enter your age:", 18, 0, 120, false); String name = CLIHelper.askString("What is your name?", "Anonymous"); System.out.println("Proceed: " + proceed); System.out.println("Age: " + age); System.out.println("Name: " + name); } }
See Javadoc for complete API reference.
3. CLI argument helper
See also: Command Line Interface Guidelines.
3.1. Command and argument
Every command-line application has a way of receiving input from users, usually in the form of command-line arguments. A command-line argument is a piece of information provided to the command-line application when it's invoked. These arguments are provided as an array of strings. The first element of the array (argument 0) is typically the name of the command itself.
In the example below, 'my-video-coder' is our command, and the rest are arguments:
my-video-coder encode --input vid1.mp4 vid2.mp4 vid3.mp4 --quality 5
To better understand how these concepts work together, let's break down our example command:
argument # | value(s) | type |
---|---|---|
0 | my-video-coder | command |
1 | encode | subcommand |
2 | –input | option1 |
3, 4, 5 | vid1.mp4 vid2.mp4 vid3.mp4 | parameters for –input option |
6 | –quality | option2 |
7 | 5 | parameter for –quaily option |
3.2. Subcommand
Subcommands are arguments that invoke more specific action that a command can perform. They are often used with commands that have multiple functions. In our example, encode is a subcommand of my-video-coder.
3.3. Option
Options are arguments that change the behavior of a command or subcommand. They usually start with a dash (-) or double dash (–). For instance, –input and –quality are options in our example command.
3.4. Parameter
Parameter provides additional information to a command, subcommand or option.
For instance, in our example:
- 'vid1.mp4 vid2.mp4 vid3.mp4' are parameters for the –input option.
- '5' is a parameter for the –quality option.
4. Getting the library
Follow instructions to embed cli-helper library in your project.
Add following snippets to your project pom.xml file:
<dependencies> ... <dependency> <groupId>eu.svjatoslav</groupId> <artifactId>cli-helper</artifactId> <version>1.2</version> </dependency> ... </dependencies> <repositories> ... <repository> <id>svjatoslav.eu</id> <name>Svjatoslav repository</name> <url>http://www3.svjatoslav.eu/maven/</url> </repository> ... </repositories>
5. Getting the source code
- This program is free software: released under Creative Commons Zero (CC0) license
- Program author:
- Svjatoslav Agejenko
- Homepage: https://svjatoslav.eu
- Email: mailto://svjatoslav@svjatoslav.eu
- Other software projects hosted at svjatoslav.eu
5.1. Source code
- Download latest snapshot in TAR GZ format
- Browse Git repository online
Clone Git repository using command:
git clone https://www3.svjatoslav.eu/git/cli-helper.git
- See JavaDoc
6. TODO
List of improvement suggestions:
- Add more concrete examples of how to use the library in JavaDoc comments. This will help developers quickly get started and learn the API.
- Provide more comprehensive unit tests for CliHelper, ParameterParser, Options and subclasses. This will ensure robustness and stability.
- Add JavaDoc comments to all classes and methods where applicable. This will provide better visibility into the library's functionality for developers.
- Add more option types like date/time, regular expression etc.
- Document best practices for using the library in a larger project.
- Implement support for more complex CLI applications like option dependencies and conflicts resolution.