func provides static command and option parsing while still allowing application code to inspect the current invocation when dynamic behavior is genuinely necessary.
Read the selected invocation with Args
@Args() is valid only on a Command Handler parameter. The runtime creates it after Command selection and parsing.
These values are available for business logic, but they are an escape hatch rather than the primary modeling tool. Heavy dependence on dynamic parsing—especially raw argv—gives up much of the type safety and static analysis that declared fields provide.
import { Args, Command, Handler } from 'func'
import type { Args as CommandArgs } from 'func'
@Command('config')
export class ConfigCommand {
@Handler(['profile', 'set'])
setProfile(@Args() args: CommandArgs) {
console.log(args.invokedPath) // ['config', 'profile', 'set']
console.log(args.inputs) // positional input after the path
console.log(args.options) // normalized root, module, and command options
}
}| Field | What it contains |
|---|---|
raw | The original application argv. |
invokedPath | The command and handler path tokens as invoked, including aliases. |
path | The canonical selected command and handler path. |
inputs | Remaining positional tokens after command and handler selection. |
options | A frozen long-name map containing root, owner-module, and command option values. |
The object, its nested arrays, and its options snapshot are all frozen.
Inject runtime Context
Context is a built-in DI value that can be injected into Commands, Services, and Modules. It provides application settings, full metadata, IO streams, cwd, the cancellation signal, and the current invocation exit code.
import { CommandMajor, Context, Handler } from 'func'
@CommandMajor()
export class MajorCommand {
constructor(private readonly context: Context) {}
@Handler({ flag: 'help', alias: 'h' })
help() {
this.context.commands.forEach(command => {
console.log(command.path.join(' '))
})
}
}Set context.exitCode to an integer from 0 through 255 when application behavior needs a specific process result. func defaults it to 1 after an execution error unless user code has already selected a value.
Context.io contains stdin, stdout, and stderr for the current invocation. See Input and output for stream conventions and testing.
Inject custom parameters
In addition to built-in values such as @Args(), a Command constructor may receive Providers registered by its Module. func creates and supplies the greeter below automatically:
import { Command, Handler, Injectable, Module } from 'func'
@Injectable()
class Greeter {
message = 'Hello'
}
@Command('hello')
class HelloCommand {
constructor(private readonly greeter: Greeter) {}
@Handler()
run() {
console.log(this.greeter.message)
}
}
@Module({
commands: [HelloCommand],
providers: [Greeter],
})
export class AppModule {}This is the minimal form of dependency injection. Modules covers Provider registration and sharing, explicit tokens, and dependency visibility in full.