Skip to main content
zerotal

Documentation


Documentation / zerotal / index / Command

Abstract Class: Command

Defined in: packages/core/src/command/Command.ts:63

Base class for console commands; subclasses declare their name/args/flags via static properties and implement run().

A command's identity and CLI surface are described with the static fields (commandName, description, args, flags, and needsApp); the work happens in run(), where parsed this.args and this.flags are available along with output helpers (info, error, line, table, …) and interactive prompts (ask, confirm, choice, secret). Register the class with CommandRunner.register to expose it as bun zt <commandName>.

Example

import { Command } from "@zerotal/core";
import type { ArgDef, FlagDef } from "@zerotal/core";

export class GreetCommand extends Command {
  static commandName = "greet";
  static description = "Print a greeting";
  static needsApp = false;
  static args: ArgDef[] = [{ name: "name", required: true }];
  static flags: FlagDef[] = [
    { name: "loud", short: "l", type: "boolean", default: false },
  ];

  async run(): Promise<void> {
    const greeting = `Hello, ${this.args["name"]}!`;
    this.info(this.flags["loud"] ? greeting.toUpperCase() : greeting);
  }
}

Invoke it once registered:

bun zt greet Ada --loud

Extended by

Constructors

Constructor

new Command(): Command

Returns

Command

Properties

commandName

static commandName: string

Defined in: packages/core/src/command/Command.ts:64


description

static description: string

Defined in: packages/core/src/command/Command.ts:65


needsApp

static needsApp: boolean

Defined in: packages/core/src/command/Command.ts:66


args

static args: ArgDef[] = []

Defined in: packages/core/src/command/Command.ts:67


flags

static flags: FlagDef[] = []

Defined in: packages/core/src/command/Command.ts:68


_writer

_writer: OutputWriter

Defined in: packages/core/src/command/Command.ts:71

Output destination. Replaced with BufferWriter by Artisan.call().


args

args: Record<string, string> = {}

Defined in: packages/core/src/command/Command.ts:74

Parsed positional arguments — set by CommandRunner before run().


flags

flags: Record<string, string | boolean | number> = {}

Defined in: packages/core/src/command/Command.ts:76

Parsed flags — set by CommandRunner before run().


app

app: unknown = undefined

Defined in: packages/core/src/command/Command.ts:78

Application instance — set by CommandRunner when needsApp is true.

Methods

run()

abstract run(): Promise<void>

Defined in: packages/core/src/command/Command.ts:80

Returns

Promise<void>


info()

info(msg): void

Defined in: packages/core/src/command/Command.ts:82

Parameters

msg

string

Returns

void


error()

error(msg): void

Defined in: packages/core/src/command/Command.ts:85

Parameters

msg

string

Returns

void


warn()

warn(msg): void

Defined in: packages/core/src/command/Command.ts:88

Parameters

msg

string

Returns

void


line()

line(msg): void

Defined in: packages/core/src/command/Command.ts:91

Parameters

msg

string

Returns

void


dim()

dim(msg): void

Defined in: packages/core/src/command/Command.ts:94

Parameters

msg

string

Returns

void


write()

write(msg): void

Defined in: packages/core/src/command/Command.ts:97

Parameters

msg

string

Returns

void


newLine()

newLine(): void

Defined in: packages/core/src/command/Command.ts:100

Returns

void


section()

section(title): void

Defined in: packages/core/src/command/Command.ts:103

Parameters

title

string

Returns

void


table()

table(rows, indent?): void

Defined in: packages/core/src/command/Command.ts:106

Parameters

rows

[string, string][]

indent?

number = 2

Returns

void


ask()

ask(question, defaultValue?): Promise<string>

Defined in: packages/core/src/command/Command.ts:127

Prompt the user for text input and wait for Enter.

Parameters

question

string

defaultValue?

string

Returns

Promise<string>

Example

const name = await this.ask('What is your name?');
const env  = await this.ask('Environment?', 'production');

confirm()

confirm(question, defaultValue?): Promise<boolean>

Defined in: packages/core/src/command/Command.ts:142

Prompt the user for a yes/no confirmation. Returns true for y/yes, false otherwise.

Parameters

question

string

defaultValue?

boolean = false

Returns

Promise<boolean>

Example

const ok = await this.confirm('Run migrations?');
const ok = await this.confirm('Overwrite file?', true);

choice()

choice(question, options): Promise<string>

Defined in: packages/core/src/command/Command.ts:157

Prompt the user to select one option from a numbered list. Returns the selected string. Defaults to the first option on invalid input.

Parameters

question

string

options

string[]

Returns

Promise<string>

Example

const env = await this.choice('Environment:', ['local', 'staging', 'production']);

secret()

secret(question): Promise<string>

Defined in: packages/core/src/command/Command.ts:170

Parameters

question

string

Returns

Promise<string>


_readLine()

_readLine(): Promise<string>

Defined in: packages/core/src/command/Command.ts:213

Read one line from stdin. Override in tests to avoid blocking.

Returns

Promise<string>