Commands
Every CLI command in Moost is a method decorated with @Cli(). The decorator argument defines the command path — the words a user types to invoke it.
Defining a command
import { Cli, Controller } from '@moostjs/event-cli'
@Controller()
export class AppController {
@Cli('deploy')
deploy() {
return 'Deploying...'
}
}Running my-cli deploy calls the deploy() method and prints the return value.
Command paths
A command path is a sequence of segments separated by spaces or slashes (both are equivalent):
// These two are identical:
@Cli('config set')
@Cli('config/set')The user always types space-separated words:
my-cli config setPositional arguments
Segments that start with : become positional arguments:
@Cli('greet/:name')
greet(@Param('name') name: string) {
return `Hello, ${name}!`
}my-cli greet World # → Hello, World!Multiple arguments work the same way:
@Cli('copy/:source/:dest')
copy(
@Param('source') source: string,
@Param('dest') dest: string,
) {
return `Copying ${source} → ${dest}`
}my-cli copy fileA.txt fileB.txtEscaped colons
If a command name contains a literal colon (like build:dev), escape it with a backslash:
@Cli('build\\:dev')
buildDev() {
return 'Building for dev...'
}my-cli build:devDefault path from method name
When @Cli() is called without an argument, the method name becomes the command:
@Cli()
status() {
return 'All systems operational'
}my-cli statusCommand aliases
Use @CliAlias() to define alternative names for a command. Stack multiple decorators for multiple aliases:
import { Cli, CliAlias, Controller, Param } from '@moostjs/event-cli'
@Controller()
export class AppController {
@Cli('install/:package')
@CliAlias('i')
@CliAlias('add')
install(@Param('package') pkg: string) {
return `Installing ${pkg}...`
}
}All three invoke the same handler:
my-cli install lodash
my-cli i lodash
my-cli add lodashWARNING
An alias contains only the alternative command name — the positional arguments from the @Cli() path are appended to every alias automatically. Writing @CliAlias('i/:package') would register the route i/:package/:package (the :package arg is appended again), and my-cli i lodash would fail as an unknown command.
Return values
Whatever a command handler returns is printed to stdout:
- a string prints as-is
- an array prints one element per line (string elements as-is, other elements as JSON)
- any other object prints as pretty 2-space-indented JSON
- a returned Error is not printed — it goes to the error handler (by default a red
ERROR:message andprocess.exit(1))
@Cli('info')
info() {
return { name: 'my-app', version: '1.0.0' }
}my-cli info
# → {
# "name": "my-app",
# "version": "1.0.0"
# }What's next
- Options & Arguments — add flags like
--verboseand typed parameters - Controllers — group commands under prefixes