Skip to content

Java command line arguments parser. Do not parsing main(String[] args) anymore !

License

Notifications You must be signed in to change notification settings

silentsoft/arguments-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Arguments Parser

Maven Central Build Status Quality Gate Status Coverage Hits

Do not parsing main(args) anymore !

Arguments Parser is a simple java library to parse command line arguments.

Supported Formats

  • -a -b
  • -key1=value1 -key2=value2
  • -key1 value1 -key2 value2
  • -key1=value1 value2 -key2=value3 value4
  • -key1 value1 value2 -key2 value3 value4
  • --a --b
  • --key1=value1 --key2=value2
  • --key1 value1 --key2 value2
  • --key1=value1 value2 --key2=value3 value4
  • --key1 value1 value2 --key2 value3 value4

Maven Central

<dependency>
    <groupId>org.silentsoft</groupId>
    <artifactId>arguments-parser</artifactId>
    <version>2.0.1</version>
</dependency>

Usage

public static void main(String[] args) throws Exception {
    Arguments arguments = Arguments.parser(args).parse();
}

Advanced Topics

Arguments Helper Usage

If the args contains one of the following help commands then ArgumentsHelper.help(Arguments) will be invoked.

  • -help
  • --help
  • -?
  • --?
public static void main(String[] args) throws Exception {
    Arguments arguments = parseArguments(args);
}

private static Arguments parseArguments(String[] args) throws InvalidArgumentsException {
    return Arguments.parser(args).help(arguments -> {
        StringBuilder builder = new StringBuilder();
        builder.append("Usage: java -jar application.jar [arguments]\n");
        builder.append("\n");
        builder.append("Common arguments:\n");
        // ...
        System.out.println(builder.toString());

        System.exit(0);
    }).parse();
}

Arguments Validator Usage

public static void main(String[] args) throws Exception {
    Arguments arguments = parseArguments(args);
}

private static Arguments parseArguments(String[] args) throws InvalidArgumentsException {
    return Arguments.parser(args).validate(arguments -> {
        if (arguments.containsKey("-a") && arguments.containsKey("-b")) {
            throw new InvalidArgumentsException("'-a' and '-b' cannot be exists together.");
        }

        return true;
    }).parse();
}

Parsing Options with dash prefix

  • LEAVE_DASH_PREFIX (default)
  • CASE_SENSITIVE_SINGLE_DASH (default)
  • CASE_INSENSITIVE_SINGLE_DASH
  • CASE_SENSITIVE_DOUBLE_DASH
  • CASE_INSENSITIVE_DOUBLE_DASH (default)

Parsing Options without dash prefix

  • REMOVE_DASH_PREFIX
  • CASE_SENSITIVE
  • CASE_INSENSITIVE

Usage

public static void main(String[] args) throws Exception {
    Arguments arguments = Arguments.parser(args, ParsingOptions...).parse();
}

Packaging

$ mvn clean package

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please note we have a CODE_OF_CONDUCT, please follow it in all your interactions with the project.

License

Please refer to LICENSE.