Skip to content
Christian Cwienk edited this page May 29, 2014 · 7 revisions

CLI-Parsec follows a strictly declarative approach to describe command-line interfaces. It consists of two aspects:

  • declaration of a CLI
  • parsing of concrete CL arguments

Declaration of a CLI

Command line interfaces are regarded to consist of and arbitrary combination of

  • options
  • commands Each option is defined in the context of a command. Each CLI has one implicit command, which is the 'root command'. To describe a CLI, each command is represented with a dedicated type. Each option in the context of a command is represented with a field that is annotated with the @Option annotation. Sub-commands are represented with a field of the type representing the respective command and is annotated using the @Command annotation.

Annotations

@Option

declares an option in the context of a command (this context is implicitly defined by the type possessing the field that is annotated).

parameter default explanation
shortOption first character of field name specifies the option character
longOption field name specifies the long option
maxOccurs 1 if field is neither collection nor array, unlimited otherwise specifies the maximum amount this option is allowed to be given in a concrete command line. Note: if set to > 1 for non-array, non-collection fields, values will be overwritten if the option is specified more than once ("last one wins").
argCount 0 if field is boolean, 1 if field is neither collection nor array, unlimited otherwise specifies the amount of arguments that are expected after this option
required the empty string (semantically meaning 'not required / false') specified whether or not the option is `required`. If an option is required, the parser will report it as an error if the option is not present at least once in a parsed command line argument set. See required options
converter the identity converter specifies a `Function` that performs a conversion from the raw command line argument (which is a string) to the target type (e.g. a `File`)

@HelpOption

used to declare a help option in the context of a command. The presence of at least one help option implies help handling by the CLI parser (displaying of command-specific help).

parameter default
shortOption h
longOption help

@Command

declares a comand in the context of a command (this context is implicitly defined by the type possessing the field that is annotated).

parameter explanation
name the command name

Parsing of concrete CL arguments

Based on the definition of a CLI, concrete command line arguments may be parsed. During the parsing process, the given command line definition object is filled according to the processed command line arguments.

Clone this wiki locally