CLI Commands
Add commands to Kora's interactive console with flags, subcommands, and tab-completion.
Kora has an interactive console. A command describes how it is called (its subcommands, flags, and
tab-completion); an action is the code that runs. They are split so the parser can validate and
autocomplete input before your logic runs. Commands load from src/cli/commands/ and actions from
src/cli/actions/, one *.js file each.
import { Commands } from "#kora/extensions";
export class Products extends Commands {
constructor(kora) {
super(kora, {
name: "products",
subcommands: {
list: { description: "List all products." },
create: {
description: "Create a product.",
flags: [
{ name: "name", required: true },
{ name: "price", required: false }
]
},
delete: {
description: "Delete a product.",
flags: [{ name: "product", required: true }]
}
}
});
}
async autocomplete(subcommand, flag) {
if (flag === "product") {
return (await this.kora.extensions.handlers.get(this.extension, "Products").listAll())
.map(product => product.name);
}
return [];
}
create(flags) {
return this.kora.extensions.actions.get(this.extension, "products").create(flags);
}
delete(flags) {
return this.kora.extensions.actions.get(this.extension, "products").delete(flags);
}
list(flags) {
return this.kora.extensions.actions.get(this.extension, "products").list(flags);
}
}The user types products create <name> --price 4.99. The command names each subcommand and its flags;
required flags are positional (<name>), optional ones use --flag value. Kora calls the method on the
command that matches the subcommand (create), and by convention that method delegates to the
action of the same name.
flagsis an object of the parsed values, keyed by flag name.autocomplete(subcommand, flag)returns suggestions for tab-completion of a flag's value.dynamic: trueon a subcommand lets you add flags at runtime by overridingdynamic(subcommand), which thecoreextension uses to expose config-defined fields as flags.
A command with no subcommands is invoked directly, and its execute(flags) method runs.

