Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CLI Builder

A fluent TypeScript library for building command-line interfaces in Deno applications.

Configuration Options

Default Values

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
});

Required Arguments

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
});

Argument Aliases

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");
});

Combined Example

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
});

About

A Deno based CLI builder

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages