gunshi 0.5.4 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -13
- package/lib/index.d.ts +1 -1
- package/lib/index.js +13 -6
- package/lib/renderer/index.js +1 -1
- package/lib/{renderer-MpQ9U28q.js → renderer-v5Uq0km9.js} +8 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -62,9 +62,11 @@ Gunshi has a simple API that is a facade:
|
|
|
62
62
|
```js
|
|
63
63
|
import { cli } from 'gunshi'
|
|
64
64
|
|
|
65
|
+
const args = process.argv.slice(2)
|
|
65
66
|
// run a simple command
|
|
66
|
-
cli(
|
|
67
|
-
|
|
67
|
+
cli(args, () => {
|
|
68
|
+
// something logic ...
|
|
69
|
+
console.log('Hello from Gunshi!', args)
|
|
68
70
|
})
|
|
69
71
|
```
|
|
70
72
|
|
|
@@ -75,7 +77,7 @@ Configure commands declaratively:
|
|
|
75
77
|
```js
|
|
76
78
|
import { cli } from 'gunshi'
|
|
77
79
|
|
|
78
|
-
// define a command with declarative configuration
|
|
80
|
+
// define a command with declarative configuration, using commandable object
|
|
79
81
|
const command = {
|
|
80
82
|
name: 'greet',
|
|
81
83
|
description: 'A greeting command',
|
|
@@ -99,6 +101,8 @@ const command = {
|
|
|
99
101
|
}
|
|
100
102
|
}
|
|
101
103
|
|
|
104
|
+
// run a command that is defined above
|
|
105
|
+
// (the 3rd argument of `cli` is the command option)
|
|
102
106
|
cli(process.argv.slice(2), command, {
|
|
103
107
|
name: 'my-app',
|
|
104
108
|
version: '1.0.0',
|
|
@@ -119,7 +123,7 @@ import type { ArgOptions, Command, CommandContext } from 'gunshi'
|
|
|
119
123
|
// type-safe arguments parsing example
|
|
120
124
|
// this demonstrates how to define and use typed command options with `satisfies`
|
|
121
125
|
|
|
122
|
-
// define options with types
|
|
126
|
+
// define 'type-safe' command options with types
|
|
123
127
|
const options = {
|
|
124
128
|
// define string option with short alias
|
|
125
129
|
name: {
|
|
@@ -139,7 +143,7 @@ const options = {
|
|
|
139
143
|
}
|
|
140
144
|
} satisfies ArgOptions
|
|
141
145
|
|
|
142
|
-
//
|
|
146
|
+
// define 'type-safe' command
|
|
143
147
|
const command = {
|
|
144
148
|
name: 'type-safe',
|
|
145
149
|
options,
|
|
@@ -156,12 +160,12 @@ For more detailed examples, check out the [playground/type-safe](https://github.
|
|
|
156
160
|
|
|
157
161
|
### 🧩 Composable Sub-commands
|
|
158
162
|
|
|
159
|
-
|
|
163
|
+
Run a CLI with composable sub-commands:
|
|
160
164
|
|
|
161
165
|
```js
|
|
162
166
|
import { cli } from 'gunshi'
|
|
163
167
|
|
|
164
|
-
// define
|
|
168
|
+
// define 'create' command
|
|
165
169
|
const createCommand = {
|
|
166
170
|
name: 'create',
|
|
167
171
|
description: 'Create a new resource',
|
|
@@ -173,6 +177,7 @@ const createCommand = {
|
|
|
173
177
|
}
|
|
174
178
|
}
|
|
175
179
|
|
|
180
|
+
// define 'list' command
|
|
176
181
|
const listCommand = {
|
|
177
182
|
name: 'list',
|
|
178
183
|
description: 'List all resources',
|
|
@@ -181,12 +186,12 @@ const listCommand = {
|
|
|
181
186
|
}
|
|
182
187
|
}
|
|
183
188
|
|
|
184
|
-
//
|
|
189
|
+
// prepare a Map of sub-commands
|
|
185
190
|
const subCommands = new Map()
|
|
186
191
|
subCommands.set('create', createCommand)
|
|
187
192
|
subCommands.set('list', listCommand)
|
|
188
193
|
|
|
189
|
-
// define the main command
|
|
194
|
+
// define the main ('resource-manager') command
|
|
190
195
|
const mainCommand = {
|
|
191
196
|
name: 'resource-manager',
|
|
192
197
|
description: 'Manage resources',
|
|
@@ -229,7 +234,7 @@ const lazyCommand = async () => {
|
|
|
229
234
|
}
|
|
230
235
|
}
|
|
231
236
|
|
|
232
|
-
//
|
|
237
|
+
// prepare a Map of sub-commands with lazy-loaded commands
|
|
233
238
|
const subCommands = new Map()
|
|
234
239
|
subCommands.set('lazy', lazyCommand)
|
|
235
240
|
|
|
@@ -261,6 +266,7 @@ const command = {
|
|
|
261
266
|
recursive: { type: 'boolean', short: 'r' },
|
|
262
267
|
operation: { type: 'string', short: 'o', required: true }
|
|
263
268
|
},
|
|
269
|
+
// define usage with object
|
|
264
270
|
usage: {
|
|
265
271
|
options: {
|
|
266
272
|
path: 'File or directory path',
|
|
@@ -290,7 +296,7 @@ Customize the usage message generation:
|
|
|
290
296
|
```js
|
|
291
297
|
import { cli } from 'gunshi'
|
|
292
298
|
|
|
293
|
-
// custom header renderer
|
|
299
|
+
// define custom header renderer
|
|
294
300
|
const customHeaderRenderer = ctx => {
|
|
295
301
|
return Promise.resolve(`
|
|
296
302
|
╔═══════════════════════╗
|
|
@@ -301,7 +307,7 @@ Version: ${ctx.env.version}
|
|
|
301
307
|
`)
|
|
302
308
|
}
|
|
303
309
|
|
|
304
|
-
// custom usage renderer
|
|
310
|
+
// define custom usage renderer
|
|
305
311
|
const customUsageRenderer = ctx => {
|
|
306
312
|
const lines = []
|
|
307
313
|
lines.push('USAGE:')
|
|
@@ -386,7 +392,7 @@ If you are interested in contributing to `gunshi`, I highly recommend checking o
|
|
|
386
392
|
|
|
387
393
|
## 💖 Credits
|
|
388
394
|
|
|
389
|
-
This project is inspired by:
|
|
395
|
+
This project is inspired and powered by:
|
|
390
396
|
|
|
391
397
|
- [`citty`](https://github.com/unjs/citty), created by UnJS team and contributors
|
|
392
398
|
- cline and claude 3.7 sonnet, examples and docs is generated
|
package/lib/index.d.ts
CHANGED
|
@@ -9,6 +9,6 @@ export { f as CommandBuiltinKeys, d as CommandBuiltinOptionsKeys, e as CommandBu
|
|
|
9
9
|
* @param entry - A {@link Command | entry command} or an {@link CommandRunner | inline command runner}
|
|
10
10
|
* @param opts - A {@link CommandOptions | command options}
|
|
11
11
|
*/
|
|
12
|
-
declare function cli<Options extends ArgOptions = ArgOptions>(args: string[], entry: Command<Options> | CommandRunner<Options>, opts?: CommandOptions<Options>): Promise<
|
|
12
|
+
declare function cli<Options extends ArgOptions = ArgOptions>(args: string[], entry: Command<Options> | CommandRunner<Options>, opts?: CommandOptions<Options>): Promise<string | undefined>;
|
|
13
13
|
|
|
14
14
|
export { Command, CommandOptions, CommandRunner, cli };
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { COMMAND_OPTIONS_DEFAULT, COMMON_OPTIONS, createCommandContext } from "./context-DmZAeiph.js";
|
|
2
2
|
import { create, log, resolveLazyCommand } from "./utils-NHs5DuHk.js";
|
|
3
|
-
import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-
|
|
3
|
+
import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-v5Uq0km9.js";
|
|
4
4
|
import { parseArgs, resolveArgs } from "args-tokens";
|
|
5
5
|
|
|
6
6
|
//#region src/cli.ts
|
|
@@ -26,10 +26,13 @@ async function cli(args, entry, opts = {}) {
|
|
|
26
26
|
showVersion(ctx);
|
|
27
27
|
return;
|
|
28
28
|
}
|
|
29
|
-
|
|
29
|
+
const usageBuffer = [];
|
|
30
|
+
const header = await showHeader(ctx);
|
|
31
|
+
if (header) usageBuffer.push(header);
|
|
30
32
|
if (values.help) {
|
|
31
|
-
await showUsage(ctx);
|
|
32
|
-
|
|
33
|
+
const usage = await showUsage(ctx);
|
|
34
|
+
if (usage) usageBuffer.push(usage);
|
|
35
|
+
return usageBuffer.join("\n");
|
|
33
36
|
}
|
|
34
37
|
if (error) {
|
|
35
38
|
await showValidationErrors(ctx, error);
|
|
@@ -50,8 +53,11 @@ function getSubCommand(tokens) {
|
|
|
50
53
|
}
|
|
51
54
|
async function showUsage(ctx) {
|
|
52
55
|
if (ctx.env.renderUsage === null) return;
|
|
53
|
-
const
|
|
54
|
-
|
|
56
|
+
const usage = await (ctx.env.renderUsage || renderUsage)(ctx);
|
|
57
|
+
if (usage) {
|
|
58
|
+
log(usage);
|
|
59
|
+
return usage;
|
|
60
|
+
}
|
|
55
61
|
}
|
|
56
62
|
function showVersion(ctx) {
|
|
57
63
|
log(ctx.env.version);
|
|
@@ -62,6 +68,7 @@ async function showHeader(ctx) {
|
|
|
62
68
|
if (header) {
|
|
63
69
|
log(header);
|
|
64
70
|
log();
|
|
71
|
+
return header;
|
|
65
72
|
}
|
|
66
73
|
}
|
|
67
74
|
async function showValidationErrors(ctx, error) {
|
package/lib/renderer/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import "../utils-NHs5DuHk.js";
|
|
2
|
-
import { renderHeader, renderUsage, renderValidationErrors } from "../renderer-
|
|
2
|
+
import { renderHeader, renderUsage, renderValidationErrors } from "../renderer-v5Uq0km9.js";
|
|
3
3
|
|
|
4
4
|
export { renderHeader, renderUsage, renderValidationErrors };
|
|
@@ -10,7 +10,10 @@ function renderHeader(ctx) {
|
|
|
10
10
|
//#region src/renderer/usage.ts
|
|
11
11
|
async function renderUsage(ctx) {
|
|
12
12
|
const messages = [];
|
|
13
|
-
if (!ctx.omitted
|
|
13
|
+
if (!ctx.omitted) {
|
|
14
|
+
const description = resolveDescription(ctx);
|
|
15
|
+
if (description) messages.push(description, "");
|
|
16
|
+
}
|
|
14
17
|
messages.push(...await renderUsageSection(ctx), "");
|
|
15
18
|
if (ctx.omitted && await hasCommands(ctx)) messages.push(...await renderCommandsSection(ctx), "");
|
|
16
19
|
if (hasOptions(ctx)) messages.push(...await renderOptionsSection(ctx), "");
|
|
@@ -99,12 +102,12 @@ function resolveSubCommand(ctx) {
|
|
|
99
102
|
return ctx.name || ctx.translation("SUBCOMMAND");
|
|
100
103
|
}
|
|
101
104
|
/**
|
|
102
|
-
*
|
|
105
|
+
* Resolve the command description
|
|
103
106
|
* @param ctx A {@link CommandContext | command context}
|
|
104
|
-
* @returns
|
|
107
|
+
* @returns resolved command description
|
|
105
108
|
*/
|
|
106
|
-
function
|
|
107
|
-
return
|
|
109
|
+
function resolveDescription(ctx) {
|
|
110
|
+
return ctx.translation("description") || ctx.description || "";
|
|
108
111
|
}
|
|
109
112
|
/**
|
|
110
113
|
* Check if the command has sub commands
|