Skip to main content

Command

@webda/core


Variable: Command

const Command: {(value, context): any; (...args): (value, context) => any; }

Defined in: packages/core/src/services/command.ts:107

Declare a CLI command on a service method.

The method parameters define the CLI arguments automatically:

  • Parameter name becomes the --flag name (e.g., port → --port)
  • TypeScript type annotation determines the flag type (string, number, boolean)
  • Default value from the method signature becomes the flag default
  • JSDoc @alias on parameter provides a single-character shorthand (e.g., -p)
  • JSDoc @description on parameter provides help text for the flag
  • JSDoc @deprecated on parameter marks the flag as deprecated

A handler returning a number sets the process exit code (the highest one when several services run the command); any other result exits with 0. Log counts rather than returning them.

Multiple services can declare the same command name — they all run in dependency-graph order. Use --service=ServiceName to filter at invocation. The CLI accepts the union of their parameters and calls every handler with the same positional values, in the merged order: declare shared parameters in the same order and type (a warning is logged otherwise).

A subclass inherits the commands of its parent class. Redeclaring the command on a subclass adds a provider, it does not replace the parent's: the application selects the providers with the webda.cli section of its package.json — "exclude": ["Webda/HttpServer:serve"] lets a RunnerHttpServer extends HttpServer serve alone, with its own parameters.

Subcommands use space-separated names. The service owns its subtree: @Command("aws s3") handles everything under webda aws s3.

Call Signature​

(value, context): any

Parameters​

value​

any

context​

ClassMethodDecoratorContext

Returns​

any

Call Signature​

(...args): (value, context) => any

Parameters​

args​

...[name: string, options: CommandOptions]

Returns​

(value, context) => any

Param​

name

Command name, space-separated for subcommands (e.g., "serve", "aws s3")

Param​

options

Command options including description

Examples​

Simple command

class HttpServer extends Service {
@Command("serve", { description: "Start the HTTP server" })
async serve(
/** @alias b @description Bind address */
bind: string = "127.0.0.1",
/** @alias p @description Port to listen on */
port: number = 18080
): Promise<void> { ... }
}
// Usage: webda serve --port 3000 --bind 0.0.0.0
// Usage: webda serve -p 3000 -b 0.0.0.0

Subcommand

class S3Service extends Service {
@Command("aws s3", { description: "Manage S3 buckets" })
async s3(bucket?: string): Promise<void> { ... }
}
// Usage: webda aws s3 --bucket my-bucket

Composable command (multiple services)

class PostgresMigrator extends Service {
@Command("migrate", { description: "Run PostgreSQL migrations" })
async migrate(dryRun: boolean = false): Promise<void> { ... }
}
class MongoMigrator extends Service {
@Command("migrate", { description: "Run MongoDB migrations" })
async migrate(dryRun: boolean = false): Promise<void> { ... }
}
// webda migrate → runs both
// webda migrate --service=PostgresMigrator → runs only PostgreSQL