Skip to content

Ernstsen/CommandLineParser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

86 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Testsuite Test Coverage Codacy Badge CodeScene Code Health Maven Central

CommandLineParser

Simple parser for commandline arguments written in Java.

The parser is configured by implementing ConfigBuilder and mapping "modes" to ConfigBuilders. ConfigBuilders are then to produce a Configuration from the given arguments, which are given.

Using CommandLineParser

CommandLineParser can be retrieved with maven by adding the following to the POM:

<dependency>
    <groupId>dk.e-software</groupId>
    <artifactId>commandLineParser</artifactId>
    <version>2.1.0</version>
</dependency>

As newer versions are released they can be found here

Parser specifications

All commands parsable by the commandLineParser is of the form:

--mode --param1 -arg1 -arg2 --param2 --param3 -arg1

mode is what determines which ConfigBuilder is used

Note that the first argument that the ConfigBuilder receives is always the mode argument. The mode is also able to receive get arguments, however they wont affect choice of ConfigBuilder

Both commands and arguments are stripped of their "-" or "--" prefix

Configurations

A CommandLineParser is configured by the mapping between commandline comments and ConfigBuilders.

Generalized Parser

A generalized parser, GeneralConfigurationBuilder, exists as a way to use this library in a generalized way.

To use this, create an implementation of the Configuration interface, and annotate the fields with name and help information.

Example

public class ExampleConfiguration implements Configuration {

    @Name(name = "val")
    @Help(helpString = "boolean value help-message")
    private boolean value;

    public boolean isValue() {
        return value;
    }
}

In this example the value booleanValue can be set in the following ways:

  • --val=true
  • --val -true
  • --value=true
  • --value -true

Nesting

It is also possible to nest configurations.

Example

public class ExampleConfiguration2 implements Configuration {

    @Name(name = "nested")
    @Help(helpString = "nested configuration")
    private ExampleConfiguration nested;

    public ExampleConfiguration2 getNested() {
        return nested;
    }

}

The ways to set the same boolean as in the example above is as follows:

  • --nested.val=true
  • --nested.val -true
  • --nested.value=true
  • --nested.value -true

JavaDocs

JavaDocs can be found here