Argparse4j - The Java command-line argument parser library

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

Argparse4j is available in Maven central repository:


IMPORTANT: When upgrading from 0.4.4 or earlier version, read Migration. There is an important change in 0.5.0 which might break you 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:
  • 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.

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

Upgrading to 0.5.0 or later versions

Please consult the documentation section for Migration.

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


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.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 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()
                .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");
                .help("File to calculate checksum");
        Namespace ns = null;
        try {
            ns = parser.parseArgs(args);
        } catch (ArgumentParserException e) {
        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());
        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 ( > 0) {
            } catch (IOException e) {
                        .printf("%s: failed to read data: %s", e.getMessage());
            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) {
            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.h
839ef370cbd54f62985bac7b974cc575eaaa24a8edd6ae7787cfc71829ceda40  file1.h

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

Did you mean:

$ java Checksum -t SHA1