Skip navigation links

Package net.sf.mmm.util.cli.api

Provides the API for utilities that help to build command-line-interfaces (CLI).

See: Description

Package net.sf.mmm.util.cli.api Description

Provides the API for utilities that help to build command-line-interfaces (CLI).

CLI API

This package provides the API to create a command-line interface (CLI) for a main program easily.

The Problem

Typical java-applications start with a main-class - a class with a method with this magic signature:
public static void main(String[] args)
However you quickly notice that this was NOT the end of wisdom when designing an object-oriented language as java. A main-program often starts easy and then over the time options and arguments are added. When you want to write a maintainable main-program you want to have more infrastructure than just having a string-array lost in a static method.

The Solution

As a solution we provide AbstractMain and AbstractVersionedMain that you can extend to build your main program. For each option and argument there are Annotations to annotate your regular Fields. Via CliMode multiple modes can be defined to realize complex scenarios. Everything integrates with javax.validation (BV) and our excellent native-language-support including text wrapping and hyphenation. So you do not have to maintain any usage or help output that is all done for you.

Example

Here you can see a simple example to get started:
  @CliMode(id = CliMode.ID_DEFAULT, usage = NlsBundleMyExampleRoot.MSG_MAIN_MODE_USAGE_DEFAULT)
 public class MyCoolMain extends AbstractVersionedMain {

    @CliOption(name = "--flag", aliases = "-f", usage = NlsBundleMyExampleRoot.MSG_MAIN_OPTION_FLAG)
   private boolean flag;

    @Override
   public int runDefaultMode() {
     if (this.flag) {
       // ... do this ...
     } else {
       // ... do that ...
     }
   }

   public static void main(String[] args) {
     new MyCoolMain().runAndExit(args);
   }
 }
 

History

After using commons-cli, gnu-getopts and finally args4j, we created this library that brings the best ideas together (with most from args4j).
In the context of CLI we use the term parameter for a string given on the commandline (out of String[] args). Such parameter is either an option or a value. A value can belong to an option or an argument. E.g. the parameters --port 8080 start consist of the option --port with its value 8080 and the argument start.
Skip navigation links

Copyright © 2001–2014 mmm-Team. All rights reserved.