Skip to content

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
1
import { type CommandContext, Declare, Command, Options } from "seyfert";
2
import { CreateCommand } from "./create.command";
3
import { 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 this
10
@Options([CreateCommand, DeleteCommand])
11
export default class AccountCommand extends Command {}

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.