Argparse4j - The Java command-line argument parser library

https://api.travis-ci.com/argparse4j/argparse4j.svg?branch=master

Argparse4j is a command line argument parser library for Java based on Python’s argparse module.

Argparse4j is available in Maven central repository:

<dependency>
  <groupId>net.sourceforge.argparse4j</groupId>
  <artifactId>argparse4j</artifactId>
  <version>0.9.0</version>
</dependency>

IMPORTANT: When upgrading, read Migration. There is an important change in 0.5.0 which might break your code. The documentation describes the change and how to migrate from earlier versions.

There are still missing features which exist in argparse but not in argparse4j, but there are also new features which only exist in argparse4j.

Here is summary of features:

  • Supported positional arguments and named arguments.

  • Variable number of arguments.

  • Generates well formatted line-wrapped help message.

  • Suggests named arguments/sub-command if unrecognized arguments/sub-command were given, e.g.:

    unrecognized argument '--tpye'
    Did you mean:
      --type
    
  • Takes into account East Asian Width ambiguous characters when line-wrap.

  • Sub-commands like, git add.

  • Sub-command alias names, e.g., co for checkout.

  • Customizable option prefix characters, e.g. +f and /h.

  • Print default values in help message.

  • Choice from given collection of values.

  • Type conversion from option strings.

  • Can directly assign values into user defined classes using annotation.

  • Group arguments so that it will be printed in help message in more readable way.

  • Mutually exclusive argument group.

  • Read additional arguments from file.

  • Argument/sub-command abbreviations.

Requirements

Java 8 or higher is needed.

The main JAR contains module information for the Java Module System. The module name is net.sourceforge.argparse4j.

Building

To build you need Java 9 or higher, and Maven 3.2.3 or higher.

The primary documentation is done using Sphinx. You need Sphinx to run mvn site.

To see how to use argparse4j, see The Argparse4j User Manual. See also Examples.

Demo

Here is the working demo program to calculate checksum. Argparse4j is used to parse command line arguments (Java 7 required to compile this source code):

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;

public class Checksum {

    public static void main(String[] args) {
        ArgumentParser parser = ArgumentParsers.newFor("Checksum").build()
                .defaultHelp(true)
                .description("Calculate checksum of given files.");
        parser.addArgument("-t", "--type")
                .choices("SHA-256", "SHA-512", "SHA1").setDefault("SHA-256")
                .help("Specify hash function to use");
        parser.addArgument("file").nargs("*")
                .help("File to calculate checksum");
        Namespace ns = null;
        try {
            ns = parser.parseArgs(args);
        } catch (ArgumentParserException e) {
            parser.handleError(e);
            System.exit(1);
        }
        MessageDigest digest = null;
        try {
            digest = MessageDigest.getInstance(ns.getString("type"));
        } catch (NoSuchAlgorithmException e) {
            System.err.printf("Could not get instance of algorithm %s: %s",
                    ns.getString("type"), e.getMessage());
            System.exit(1);
        }
        for (String name : ns.<String> getList("file")) {
            Path path = Paths.get(name);
            try (ByteChannel channel = Files.newByteChannel(path,
                    StandardOpenOption.READ);) {
                ByteBuffer buffer = ByteBuffer.allocate(4096);
                while (channel.read(buffer) > 0) {
                    buffer.flip();
                    digest.update(buffer);
                    buffer.clear();
                }
            } catch (IOException e) {
                System.err
                        .printf("%s: failed to read data: %s", e.getMessage());
                continue;
            }
            byte md[] = digest.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0, len = md.length; i < len; ++i) {
                String x = Integer.toHexString(0xff & md[i]);
                if (x.length() == 1) {
                    sb.append("0");
                }
                sb.append(x);
            }
            System.out.printf("%s  %s\n", sb.toString(), name);
        }
    }

}

When executed:

$ java Checksum -h
usage: Checksum [-h] [-t {SHA-256,SHA-512,SHA1}] [file [file ...]]

Calculate checksum of given files.

positional arguments:
  file                   File to calculate checksum

named arguments:
  -h, --help             show this help message and exit
  -t {SHA-256,SHA-512,SHA1}, --type {SHA-256,SHA-512,SHA1}
                         Specify hash function to use (default: SHA-256)
$ java Checksum file1.cc file1.h
6bd85bf4b936bc8870c70bea04cd12d4fe3745934f511e6e188d718d32154a79  file1.cc
839ef370cbd54f62985bac7b974cc575eaaa24a8edd6ae7787cfc71829ceda40  file1.h

$ java Checksum --tpye file1.cc
usage: Checksum [-h] [-t {SHA-256,SHA-512,SHA1}] [file [file ...]]
Checksum: error: unrecognized arguments: --tpye

Did you mean:
        --type

$ java Checksum -t SHA1 file1.cc
20bada64dde97b98faaba09ebbfdb70af71476f1  file1.cc