A fluent TypeScript library for building command-line interfaces in Deno applications.
Use the default option to specify a fallback value when an argument isn't provided:
const cli = CLIBuilder.create()
.withArgument("port", "Port number", {
default: "3000"
})
.build();
await cli.run(async () => {
const port = cli.getArg("port"); // Will be "3000" if not specified
});While the required flag is supported in the type system, arguments marked as required will throw an error if not provided:
const cli = CLIBuilder.create()
.withArgument("config", "Config file path", {
required: true
})
.build();
await cli.run(async () => {
const config = cli.getArg("config"); // Will throw error if --config is not provided
});You can provide short-form aliases for both arguments and boolean flags:
const cli = CLIBuilder.create()
.withArgument("output", "Output file", {
alias: "o" // Allows using -o or --output
})
.withBooleanFlag("verbose", "Verbose logging", "v") // Allows using -v or --verbose
.build();
await cli.run(async () => {
// These are equivalent:
// --output ./file.txt
// -o ./file.txt
const outputFile = cli.getArg("output");
// These are equivalent:
// --verbose
// -v
const isVerbose = cli.getArg("verbose");
});Here's an example combining all three features:
const cli = CLIBuilder.create()
.withArgument("input", "Input file path", {
required: true,
alias: "i"
})
.withArgument("output", "Output file path", {
default: "output.txt",
alias: "o"
})
.withBooleanFlag("force", "Overwrite existing files", "f")
.build();
await cli.run(async () => {
// Command can be run as:
// deno run script.ts --input input.txt
// deno run script.ts -i input.txt
// deno run script.ts -i input.txt -o custom.txt
// deno run script.ts -i input.txt -f
const inputPath = cli.getArg("input"); // Required
const outputPath = cli.getArg("output"); // Defaults to "output.txt"
const force = cli.getArg("force"); // Defaults to false
});