|
38 | 38 |
|
39 | 39 | ## Usage 🎯 |
40 | 40 |
|
41 | | -### CLI 💻 <!-- if available --> |
| 41 | +Use the `Command` class to build and run CLI applications with arguments, |
| 42 | +options, and subcommands. |
42 | 43 |
|
43 | | -This package is a library for building CLI tools. Once you've defined your |
44 | | -command, you can run it from the command line using Deno or Node.js. For |
45 | | -example, save the script below as `mycli.ts` and execute it with |
46 | | -`deno run mycli.ts [args]` or `node mycli.js [args]`. |
| 44 | +```ts |
| 45 | +//cli.ts |
| 46 | +import { Command, CommandError } from "@dep/command"; |
47 | 47 |
|
48 | | -Example command execution: |
49 | | - |
50 | | -```bash |
51 | | -deno run mycli.ts input.txt --output output.txt |
52 | | -``` |
53 | | - |
54 | | -### API 🧩 |
55 | | - |
56 | | -Use the `Command` class to build and configure your CLI. Here's a basic example: |
| 48 | +const http = new Command() |
| 49 | + .name("http") |
| 50 | + .version("1.0.0") |
| 51 | + .description("A lightweight HTTP client CLI") |
| 52 | + .argument("url", "The target URL for the request") |
| 53 | + .option("--header", { |
| 54 | + shortFlag: "-H", |
| 55 | + description: 'Custom header (e.g., "Content-Type: application/json")', |
| 56 | + optional: true, |
| 57 | + }); |
57 | 58 |
|
58 | | -```typescript |
59 | | -import { Command } from "@dep/command"; |
| 59 | +http.command("get", "Perform a GET request").handler(({ args, options }) => { |
| 60 | + console.log(`GET ${args.url}`); |
| 61 | + console.log(`Header: ${options.header}`); |
| 62 | +}); |
60 | 63 |
|
61 | | -const cmd = new Command() |
62 | | - .name("mycli") |
63 | | - .description("A simple CLI tool example") |
64 | | - .version("1.0.0") |
65 | | - .argument("input", { description: "Input file path" }) |
66 | | - .option("--output", { |
67 | | - kind: "value", |
68 | | - description: "Output file path", |
69 | | - shortFlag: "-o", |
| 64 | +http |
| 65 | + .command("post", "Perform a POST request") |
| 66 | + .option("--body", { |
| 67 | + shortFlag: "-B", |
| 68 | + description: "The JSON body string to send", |
70 | 69 | }) |
71 | 70 | .handler(({ args, options }) => { |
72 | | - console.log("Input file:", args.input); |
73 | | - console.log("Output file:", options.output); |
| 71 | + console.log(`POST ${args.url}`); |
| 72 | + console.log(`Body: ${options.body}`); |
74 | 73 | }); |
75 | 74 |
|
76 | 75 | try { |
77 | | - await clit.run(); // (defaults tokens Deno.args | `process.argv.slice(2)`) |
| 76 | + // uses Deno.args or process.argv.slice(2) |
| 77 | + await http.run(); |
78 | 78 | } catch (err) { |
79 | 79 | if (err instanceof CommandError) { |
80 | 80 | console.error(`\nError: ${err.message}\n`); |
81 | | - cmd.help(); |
82 | | - Deno.exit(1); //or process.exit(1); |
| 81 | + http.help(); |
| 82 | + Deno.exit(1); // or process.exit(1) |
83 | 83 | } |
84 | 84 | throw err; |
85 | 85 | } |
86 | 86 | ``` |
87 | 87 |
|
88 | | -For more advanced usage, including subcommands: |
| 88 | +### Running the CLI 💻 |
89 | 89 |
|
90 | | -```typescript |
91 | | -import { Command } from "@dep/command"; |
| 90 | +```bash |
| 91 | +deno run cli.ts --help |
| 92 | +``` |
92 | 93 |
|
93 | | -const cmd = new Command() |
94 | | - .name("mycli") |
95 | | - .description("CLI with subcommands") |
96 | | - .command("sub", "Subcommand description") |
97 | | - .argument("arg", "Subcommand argument") |
98 | | - .handler(({ args }) => { |
99 | | - console.log("Subcommand arg:", args.arg); |
100 | | - }); |
| 94 | +```text |
| 95 | +Usage: http <url> [options] [command] |
101 | 96 |
|
102 | | -try { |
103 | | - await clit.run(); // (defaults tokens Deno.args | `process.argv.slice(2)`) |
104 | | -} catch (err) { |
105 | | - if (err instanceof CommandError) { |
106 | | - console.error(`\nError: ${err.message}\n`); |
107 | | - cmd.help(); |
108 | | - Deno.exit(1); //or process.exit(1); |
109 | | - } |
110 | | - throw err; |
111 | | -} |
| 97 | +A lightweight HTTP client CLI |
| 98 | +
|
| 99 | +Arguments: |
| 100 | + <url> The target URL for the request |
| 101 | +
|
| 102 | +Options: |
| 103 | +--header, -H [header] Custom header (e.g., "Content-Type: application/json") |
| 104 | +--help, -h Show help |
| 105 | +--version, -v Show version |
| 106 | +
|
| 107 | +Commands: |
| 108 | + get Perform a GET request |
| 109 | + post Perform a POST request |
112 | 110 | ``` |
113 | 111 |
|
114 | | -Run with `mycli sub value` to execute the subcommand. |
| 112 | +```bash |
| 113 | +deno run cli.ts get 'https://estarlincito.com' --header "Authorization: Kyumiu" |
| 114 | +``` |
115 | 115 |
|
116 | | ---- |
| 116 | +```text |
| 117 | +GET https://estarlincito.com |
| 118 | +Header: Authorization: Kyumiu |
| 119 | +``` |
| 120 | + |
| 121 | +```bash |
| 122 | +deno run cli.ts post 'https://estarlincito.com' --body "user: estarlincito" |
| 123 | +``` |
| 124 | + |
| 125 | +```text |
| 126 | +POST https://estarlincito.com |
| 127 | +Body: user: estarlincito |
| 128 | +``` |
117 | 129 |
|
118 | 130 | ## License 📄 |
119 | 131 |
|
|
0 commit comments