Class CliMain

java.lang.Object
io.github.mmm.cli.CliMain

public abstract class CliMain extends Object
This is the abstract base class for a main-program.
You simply need to extend this class and implement run(CliArgs). For advanced CLIs please extend io.github.mmm.nls.cli.NlsMain from mmm-nls-cli.
Since:
1.0.0
  • Field Details

  • Constructor Details

    • CliMain

      public CliMain()
      The constructor.
    • CliMain

      public CliMain(CliConsole console)
      The constructor.
      Parameters:
      console - the CliConsole to use.
  • Method Details

    • addCommands

      protected void addCommands()
      Adds the CliCommands for this program.
    • getContainer

      public CliContainer getContainer()
      Returns:
      the CliContainer with the groups, commands and their properties.
    • console

      public CliConsole console()
      Returns:
      the CliConsole.
    • error

      protected void error(CliArgument argument)
      Parameters:
      argument - the CliArgument that was unexpected at this place.
    • error

      protected int error(Throwable error)
      Parameters:
      error - the Throwable that occurred.
      Returns:
      the exit code corresponding to the given error.
    • group

      protected CliAddCommand group()
      Returns:
      the root group where CliCommands can be added.
    • group

      protected CliAddCommand group(String name)
      Parameters:
      name - the group name.
      Returns:
      the group with the given group name where CliCommands can be added.
    • isTolerateDuplicateOptions

      protected boolean isTolerateDuplicateOptions()
      Typically a program shall provide multiple values for a CliOption by providing them as multiple CliValues after the CliOption (e.g. "--keys foo bar some"). However, some programs like to repeat the same CliOption for that purpose (e.g. "--key foo --key bar --key some"). In case this shall cause an error you can override this method and return false.
      Returns:
      true to tolerate duplicate occurrences of the same CliOption, false otherwise (default).
    • getProgramName

      public String getProgramName()
      Returns:
      the name of this program.
    • getVersion

      public String getVersion()
      Returns:
      the version of this program.
    • run

      public final int run(CliArgs args)
      Has to be implemented to handle that given CliArgs and do the program logic.
      Parameters:
      args - the CliArgs.
      Returns:
      the exit code.
    • run

      public int run(String... args)
      This method parses the given args as CliArgs and delegates to run(CliArgs). Additionally it will catch and handle any kind of errors.
      Parameters:
      args - are the command-line arguments.
      Returns:
      the exit code.
    • runAndExit

      protected void runAndExit(String... args)
      This method delegates to run(String...) and then calls System.exit(int) with the returned exit code. Typically you only need to call this method from your actual main method.
      Parameters:
      args - are the command-line arguments.