Creating Commands


This example shows how to define sub-commands for a given command, and how to easily associate a command handler. This example defines a thing command with get and set subcommands.

package things;

import io.dropwizard.cli.Command;
import io.dropwizard.setup.Bootstrap;
import net.sourceforge.argparse4j.inf.FeatureControl;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;

public class ThingCommand extends Command {

    private static final String THING_COMMAND_NAME = "thing";

    private static final String THING_SUBCOMMAND_GET = "get";
    private static final String THING_SUBCOMMAND_SET = "set";
    public ThingCommand() {
        super(THING_COMMAND_NAME, "Thing management.");

    public void configure(Subparser subparser) {
            .help("Gets a thing."), new GetCommand());

            .help("Sets a thing."), new SetCommand());

    private void addSubCommand(Subparser parser, Command cmd) {
        // associate the subcommand with a Command object

    public void run(Bootstrap<?> bootstrap, Namespace namespace) throws Exception {
        Command cmd = namespace.get("subcommand");
        assert cmd != null;, namespace);

    class GetCommand extends Command {

        public GetCommand() {
            super(THING_SUBCOMMAND_GET, "Gets a thing.");

        public void configure(Subparser cmd) {
            cmd.addArgument("--name").type(String.class).required(true).help("Name of the thing");

        public void run(Bootstrap<?> bootstrap, Namespace namespace) throws Exception {
            String thingName = namespace.getString("name");
            System.out.println("Getting the thing named: " + thingName);

    class SetCommand extends Command {
        // ...   

Here’s an example invocation of the command (assuming an app named myapp):

$ bin/myapp thing get --name mything
Getting the thing named: mything