Create Command
In func, creating a command is simple. Add the
Command decorator to any class:
import { Command } from 'func'
@Command({
name: 'apple',
alias: 'a',
})
export class MyFirstCommand {
constructor() {
console.log('ok')
}
} Create a command in file apple.ts.
Assuming your command-line tool is named hello-func, run
hello-func apple or hello-func a and the terminal
will display ok.
Create Option
Command-line tools often need top-level options, such as
<command> --version. The Option decorator handles this
elegantly:
import { Option } from 'func'
@Option({
name: 'version',
alias: 'v',
})
export class Version {
constructor() {
console.log('1.0.0')
}
} Create an option command in file version.ts.
After adding this class, it is triggered by --version or its alias,
-v.
func lets you create aliases for any command. For a global
Option, the alias uses a single -, which follows the
standard CLI convention.
Create Sub-options
func also provides a SubOptions decorator for commands like
<command> --<option>:
import { Command, CommandArgsProvider, SubOptions } from 'func'
@Command({
name: 'apple',
})
@SubOptions([{ name: 'help', alias: 'h' }])
export class MyFirstSubOption {
constructor(private arg: CommandArgsProvider) {
if (arg.option.help) {
console.log('HELP MESSAGE: The apple is red')
}
}
}
Create a command containing a sub-option in file apple.ts.
See Parameters for how to read option values.
Create Main Command
The main command is triggered when users run only the command-line tool name without parameters:
import { CommandMajor } from 'func'
@CommandMajor()
export class Main {}
If your command-line tool is named hello-func, running
hello-func will trigger the main command.
Get Missing Command
In some cases, users enter parameters or commands incorrectly. Use
CommandMissing to guide them with a helpful message when no
Command or Option can be found:
import { CommandMissing } from 'func'
@CommandMissing()
export class Missing {
constructor() {
console.log('not found any commands!')
}
} CommandNotFound is deprecated. Use CommandMissing
for new projects.