Sub-Zero (SubCommands)
We have touching on the basics of creating a command, but what if we want to create a command that has sub-commands? For example, we want to create a command that has a create
and delete
sub-command. This is where sub-commands come in.
Creating a Sub-Command
To create a sub-command, we need to create a new class that extends SubCommand
and implement the run
method. We then need to add the sub-command to the parent command.
It’s suppose you have the following directory structure:
- src
Directorycommands
Directoryaccount
- create.command.ts
- delete.command.ts
- parent.ts
- index.ts
- seyfert.config.js
- package.json
- tsconfig.json
1import { type CommandContext, Declare, Command, Options } from "seyfert";2import { CreateCommand } from "./create.command";3import { DeleteCommand } from "./delete.command";4
5@Declare({6 name: "account",7 description: "account command"8})9// Being in the same folder with @AutoLoad() you can save this10@Options([CreateCommand, DeleteCommand])11export default class AccountCommand extends Command {}
1import { type CommandContext, Declare, SubCommand } from "seyfert";2
3@Declare({4 name: "create",5 description: "create a new something"6})7export class CreateCommand extends SubCommand {8 run(ctx: CommandContext) {9 // some logic there10
11 return ctx.write({12 content: "create command executed"13 });14 }15}
1import { type CommandContext, Declare, SubCommand } from "seyfert";2
3@Declare({4 name: "delete",5 description: "delete something"6})7export class DeleteCommand extends SubCommand {8 run(ctx: CommandContext) {9 // some logic here10
11 return ctx.write({12 content: "delete command executed"13 });14 }15}
In the example above, we have created a create
and delete
sub-command. We then added the sub-commands to the parent command using the @Options
decorator.