examples/subcommand.ts (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import { Command } from "../src/index";
const serveCmd = new Command()
.setName("serve")
.setDescription("start the server")
.addNumberArgument("port", { short: "p", required: true })
.setAction((ctx) => {
console.log(`port: ${ctx.port}`);
});
const buildCmd = new Command()
.setName("build")
.setDescription("build the project")
.addFlag("prod", { short: "p", description: "build for production" })
.setAction((ctx) => {
console.log(`production: ${ctx.prod}`);
});
const cli = new Command()
.setName("cli")
.setDescription("a CLI with subcommands")
.addFlag("debug", { short: "d" })
.addSubcommand(serveCmd)
.addSubcommand(buildCmd)
.setAction((ctx) => {
if (ctx.debug) console.log("debug mode enabled");
console.log("no subcommand provided. Use 'serve' or 'build'.");
});
cli.parse(process.argv.slice(2)).catch(console.error);
|