Go to repository

Class CLIApp

The CLIApp class is the class to extend in order to define your CLI based application.

Hierarchy (view full)

Constructors

Parameters

Returns CLIApp

Properties

Protected
currentArgs: unknown[]
Protected
currentOptions: unknown
Protected
hasAction: boolean = false
Protected
isSubcommand: boolean
Protected
onReadErrorMsg: string
Protected
options: WithRequired<CLIAppOptions, "flags">
Private
processArgs: string[]

The arguments passed to the application

Protected
program: Command

Methods

Set the action for this command. The action callback receives both the current command, and the arguments (Note this is one or more arguments, depending on the command definition. Mandatory or optional positional arguments are passed first, while the last element consists of the flags passed to the command)

Parameters

  • f: ((cliapp: this, ...args: unknown[]) => void)

    The callback to run when this command is called.

      • (cliapp, ...args): void
      • Parameters

        • cliapp: this
        • Rest
          ...args: unknown[]

        Returns void

Returns this

Define a new sub-command.

Parameters

  • name: string

    The new sub-command name

  • description: string

    The sub-command description, or translation key if a translator is used.

  • f: ((cmd: CLICommandBuilder) => void)

    A callback to construct the newly defined sub-command.

Returns this

Ensure a condition is met, and if not, show the given error message, and exit the application with 1.

Parameters

  • condition: boolean

    The condition that needs to satisfy

  • error: string

Returns void

Exit the application with the given value.

Parameters

  • value: number

    The value to exit with

Returns void

Returns true if the command received no arguments nor flags

Returns boolean

Make this command to be able to read input from a file.

Parameters

  • description: string

    The input flag description or the translation key if a translator is used.

  • onReadErrorMsg: string

    The error message or translation key if a translator is used.

Returns this

Add a new option to the command.

Parameters

  • flags: string

    The flags to trigger this option

  • Optional
    description: string

    The description or translation key if a translator is used.

  • Optional
    defaultValue: string | boolean

    A default value.

Returns this

Make this command to be able to write the output to a file.

Parameters

  • description: string

    The output flag description or translation key if a translator is used.

Returns this

Output the command help.

Returns void

Output the command's help if no arguments where given, then exit the application with 0.

Returns void

Read the input to this command. The input may be the first arguments passed to a command (without the flags, separated by space) if a mandatory argument or optional argument was given, or the contents of the input file if an input was configured.

Returns string

Read the contents of a file. Throws error if the file does not exist.

Parameters

  • fileName: string

    The file to read.

Returns string

Run the CLI app. Call when your CLI app has been completely configured over the main app.

Returns void

Write the given data to the standard output, or, to the expected file, if an output was configured. Note that when outputting to a file, if the file does not exists, it gets created. If it already exists, then the output is appended to the previously defined contents of that file.

Parameters

  • data: string

    The data to output

Returns void

Write a set of contents to the standard output.

Parameters

  • contents: string

    The contents to write.

Returns void

Write a set of contents to a given file.

Parameters

  • fileName: string

    The file to write to.

  • contents: string

    The contents to write.

Returns void