Command Line Interface
A small Java library, featuring a Command Line Interface and a custom JSON Parser.
Command Line Interface
The CLI is split into options and the command line.
Creating an option
You’ll have to create a new Option
using the OptionBuilder
. An example implementation would be:
private static final Option INPUT = new OptionBuilder().setAbbreviation("-i").addAlias("--input").setRequired(true).setTakesInput(true).setDescription("Input file").create();
The aliases are optional, but each option requires an abbreviation. You can also toggle, whether an option is required and/or reads the following parameter as argument.
Creating a CLI parser
Next, you’ll need to create an CommandLine
object, allowing you to parse args. Again, here’s an example implementation, using the CmdLineBuilder
class:
private static final String CMD_BASE = "java -jar program.jar";
private static final String DESCRIPTION = "Magic Tool doing something special.";
private static final String COPYRIGHT = "Copyright (c) 2024 To_Craft. Licensed under the Crafted License 1.0";
private static final CommandLine CMDLINE = new CmdLineBuilder().setCmdBase("java -jar program.jar").setHeader(DESCRIPTION).setFooter(COPYRIGHT).setForceArgInput(false).addOptions(INPUT).create();
If forceArgInput
is set to true
, other options can be read as input for other options.
You’ll notice that you can define the base command. Every command option will then be amended to it on the help page. Every required argument is enclosed by brackets: [-i]
. Moreover, every option that takes an input has <arg
amended. The description for each option will be added to the more detailed help view. The Description/Header for the CommandLine
will be added between the command and the detailed explanation. The Footer/Copyright will be added as the last line of help page.
The help page for the example above will look like this:
usage: java -jar program.jar -i <arg>
Magic Tool doing something special.
-i,--input <arg> Input file
Copyright (c) 2024 To_Craft. Licensed under the Crafted License 1.0
If you add more options, you might see a help page, similar to this one:
usage: java -jar program.jar -i <arg> -o [-s <arg>] [-d] [-b <arg>]
Magic Tool doing something special.
-i,--input <arg> Input file
-o,--output Output if specified
-s,--second <arg> Define a second input
-d,--directory,--dir Do it in a directory
-b <arg> Do it vise-versa
Copyright (c) 2024 To_Craft. Licensed under the Crafted License 1.0
Parsing Arguments
You can parse arguments by using CommandLine.parseArgs(String[]);
. Assuming you have a String Array called args
:
Map<Option, String> parsedArgs = CMDLINE.parseArgs(args);
parseArgs
returns null
, if there was an error parsing the args.
Each option will be mapped to an input value. If no input value is required, it will be mapped to null. Therefore, check if an option is present by using:
boolean inputIsPresent = parsedArgs.containsKey(INPUT);
Help Page
You can get the help page as a String by calling cmdLine.getHelpPage();
.
JSON Parsing
The CLI library also contains a logic for parsing and encoding JSON.
Parsing
Assuming you have a String json
, you can call JsonParser.parseJson(json);
to get a JsonElement
. Depending on what type of json you parsed, you can call one of the following methods, where each one throws a JsonParseException
if the json String is of another type:
jsonElement.asBool()
— returns aJsonBool
, where you can calljsonBool.get()
to get the valuejsonElement.asDouble()
— returns aJsonDouble
, where you can calljsonDouble.get()
to get the valuejsonElement.asInt()
— returns aJsonInt
, where you can calljsonInt.get()
to get the valuejsonElement.asInt()
— returns aJsonInt
, where you can calljsonInt.get()
to get the valuejsonElement.asString()
— returns aJsonString
, where you can calljsonString.get()
to get the valuejsonElement.asArray()
— returns aJsonArray
, which you can use like anArrayList<JsonElement>
jsonElement.asObject()
— returns aJsonObject
, which you can use like aHashMap<String, JsonElement>
Each of these types is a descendant of JsonElement
.
Encoding
If you have a JsonElement jsonElement
, you can simply call jsonElement.toJson();
in order to get a compacted JSON String. To get a pretty JSON, just call jsonElement.toPrettyJson();
and you will get a pretty JSON String.