Function: commanderCLIEntrypoint()
commanderCLIEntrypoint(
options?):EntrypointAdapter<CLIContext<{ }>>
Create the CLI adapter of an entrypoint, parsing the command-line arguments with commander: each CLI action (@CLIAction) becomes a command, and the properties of its input DTO become its options. It needs the optional peer dependency commander (and @inquirer/prompts for the interactive prompts).
A property becomes a kebab-case option (deviceId → --device-id <value>), parsed by its type: a number, true or false, a JSON object, an array (--tags <values...>), a boolean as a flag (with a --no-<name> negation when it defaults to true); the enums, the defaults and the descriptions appear in the help. The command text may declare positional arguments, given to the properties of their names ('copy <files...> [target]'), and its leading words before the last one name its groups ('package exe <file>' is the command exe of the group package).
The command runs once the application has launched (all its objects, its bootstrap and the other adapters are ready). Its result is written to stdout (see format; an EventsResult writes each event's data, a FileResult the file's bytes); a failure writes error: <message> to stderr and ends with the code 1. Then the application exits with the command's code (see exit). When the entrypoint stops first, a command not started yet does not run, and the prompts and the event streams end.
Parameters
options?
The adapter options; by default it parses process.argv, prompts in a terminal, and exits the application once the command ended.
Returns
EntrypointAdapter<CLIContext<{ }>>
The adapter, to give to the cli option of buildEntrypoints.
Example
import {Application, DTO} from 'lakutata'
import {buildEntrypoints, Controller} from 'lakutata/com/entrypoint'
import {CLIAction} from 'lakutata/decorator/ctrl'
import {Expect} from 'lakutata/decorator/dto'
import {commanderCLIEntrypoint} from 'lakutata/adapter/commander'
class GreetDTO extends DTO {
@Expect(DTO.String().required().description('The name to greet'))
public name!: string
@Expect(DTO.Number().integer().optional().default(1).description('How many times'))
public times?: number
@Expect(DTO.Boolean().optional().default(false))
public loud?: boolean
}
class GreetController extends Controller {
//tool greet Ada --times 2 --loud
@CLIAction('greet <name>', GreetDTO)
public async greet(input: GreetDTO): Promise<string> {
const text: string = `Hello, ${input.name}`
return Array(input.times ?? 1).fill(input.loud ? text.toUpperCase() : text).join('\n')
}
}
Application.run({
id: 'tool.app',
name: 'Tool',
components: {
entrypoint: buildEntrypoints({
controllers: [GreetController],
cli: commanderCLIEntrypoint({bin: 'tool', description: 'A greeting tool', version: '1.0.0'})
})
},
bootstrap: ['entrypoint']
})