Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Using Groups

Header Image

Ahoy there! Are ye tired of rummaging through piles of command line options? Fear not, for Apache Commons CLI has got ye covered! In this article, we’ll be diving into one of its key features: grouping options together.

Grouping Options Together

Grouping options is a way to organize related options and make them easier to manage. Let’s say we have a command line tool that performs different actions based on a set of input parameters. We can group the options by the actions they correspond to, like so:

Options options = new Options();

OptionGroup actions = new OptionGroup();
actions.addOption(new Option("create", "create a new item"));
actions.addOption(new Option("update", "update an existing item"));
actions.addOption(new Option("delete", "delete an item"));

options.addOptionGroup(actions);

options.addOption("id", true, "item ID");
options.addOption("name", true, "item name");
options.addOption("price", true, "item price");

In the example above, we’ve created an OptionGroup called “actions” and added three options to it: “create”, “update”, and “delete”. We’ve then added the OptionGroup to the Options object.

Notice that we’ve also added some individual options outside of the OptionGroup. These options are related to the item we want to create, update, or delete.

Now, when we parse the command line arguments, we can specify that the “actions” OptionGroup is mutually exclusive:

CommandLineParser parser = new DefaultParser();

try {
    CommandLine cmd = parser.parse(options, args);

    if (cmd.hasOption("create")) {
        // handle create action
    } else if (cmd.hasOption("update")) {
        // handle update action
    } else if (cmd.hasOption("delete")) {
        // handle delete action
    } else {
        // no action specified
    }
} catch (ParseException e) {
    // handle parsing exception
}

If the user specifies more than one option in the “actions” OptionGroup, an exception will be thrown. This ensures that the user can only perform one action at a time.

Specifying Group Membership

In the previous section, we learned how to group options together in Apache Commons CLI. Now, let’s take a closer look at how we can specify group membership.

Group membership refers to the options that belong to a particular OptionGroup. By default, an option does not belong to any group. To add an option to a group, we can use the OptionGroup.addOption method:

OptionGroup actions = new OptionGroup();
actions.addOption(new Option("create", "create a new item"));
actions.addOption(new Option("update", "update an existing item"));
actions.addOption(new Option("delete", "delete an item"));

Option idOption = new Option("id", true, "item ID");
actions.addOption(idOption);

Option nameOption = new Option("name", true, "item name");
actions.addOption(nameOption);

Option priceOption = new Option("price", true, "item price");
actions.addOption(priceOption);

options.addOptionGroup(actions);

In the example above, we’ve created an OptionGroup called “actions” and added three options to it: “create”, “update”, and “delete”. We’ve also created three individual options for the “id”, “name”, and “price” fields.

By calling the addOption method on the actions object, we’ve specified that the “id”, “name”, and “price” options belong to the “actions” OptionGroup.

When we parse the command line arguments, we can check if an option belongs to a particular group by calling the OptionGroup.getSelected method:

CommandLineParser parser = new DefaultParser();

try {
    CommandLine cmd = parser.parse(options, args);

    OptionGroup actions = options.getOptionGroup("actions");
    if (actions.getSelected() == null) {
        // no action specified
    } else if (actions.getSelected().equals(idOption)) {
        // handle create or update action
    } else if (actions.getSelected().equals(nameOption)) {
        // handle delete action
    }
} catch (ParseException e) {
    // handle parsing exception
}

In the example above, we’ve retrieved the “actions” OptionGroup using the getOptionGroup method. We’ve then checked which option was selected by calling the getSelected method. Based on the selected option, we can determine which action to perform.

Conclusion

In this article, we’ve learned how to group options together and specify group membership in Apache Commons CLI. Grouping options is a useful technique for organizing command line options and making them easier to manage.

Remember to keep your code organized and your options grouped, me hearties! Happy coding and may the winds be at your back.