Skip to content

Repository files navigation

@dep/command 🛠️

A type-safe CLI command builder for Deno and Node.js, enabling easy creation of commands with arguments, options, subcommands, and handlers.

JSR version

Features ✨

  • 🔒 Type-safe definitions for arguments and options
  • 📚 Support for nested subcommands and aliases
  • ⚙️ Automatic handling of --help and --version flags
  • ✅ Built-in validation for configurations and inputs
  • 🔄 Variadic arguments and options for flexible parsing
  • 📦 Seamless integration with Deno and Node.js environments

Installation 📦

  • Deno:

    deno add jsr:@dep/command
  • Node.js (18+):

    npx jsr add @dep/command

    Then import as an ES module:

    import{Command}from"@dep/command";

Usage 🎯

Use the Command class to build and run CLI applications with arguments, options, and subcommands.

//cli.tsimport{Command,CommandError}from"@dep/command";consthttp=newCommand().name("http").version("1.0.0").description("A lightweight HTTP client CLI").argument("url","The target URL for the request").option("--header",{shortFlag: "-H",description: 'Custom header (e.g., "Content-Type: application/json")',optional: true,});http.command("get","Perform a GET request").handler(({ args, options })=>{console.log(`GET ${args.url}`);console.log(`Header: ${options.header}`);});http.command("post","Perform a POST request").option("--body",{shortFlag: "-B",description: "The JSON body string to send",}).handler(({ args, options })=>{console.log(`POST ${args.url}`);console.log(`Body: ${options.body}`);});try{// uses Deno.args or process.argv.slice(2)awaithttp.run();}catch(err){if(errinstanceofCommandError){console.error(`\nError: ${err.message}\n`);http.help();Deno.exit(1);// or process.exit(1)}throwerr;}

Running the CLI 💻

deno run cli.ts --help
Usage: http <url> [options] [command]
A lightweight HTTP client CLI
Arguments:
<url> The target URL for the request
Options:
--header, -H [header] Custom header (e.g., "Content-Type: application/json")
--help, -h Show help
--version, -v Show version
Commands:
get Perform a GET request
post Perform a POST request
deno run cli.ts get 'https://estarlincito.com' --header "Authorization: Kyumiu"
GET https://estarlincito.com
Header: Authorization: Kyumiu
deno run cli.ts post 'https://estarlincito.com' --body "user: estarlincito"
POST https://estarlincito.com
Body: user: estarlincito

License 📄

MIT License – see LICENSE for details.

Author: Estarlin R (estarlincito.com)

About

A lightweight, type-safe CLI command builder for Deno, Node.js, and browsers.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages