gunshi 0.18.0 → 0.19.0
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/lib/{cli-8GpxrUOM.js → cli-m93bHwFR.js} +14 -11
- package/lib/{context-DSuCy-i-.js → context-CBDAvGDh.js} +10 -8
- package/lib/context.d.ts +22 -19
- package/lib/context.js +2 -2
- package/lib/definition-Bdw7cMea.d.ts +26 -0
- package/lib/{definition-DZJeZYb2.js → definition-DyVBFqB7.js} +1 -1
- package/lib/definition.d.ts +3 -3
- package/lib/definition.js +1 -1
- package/lib/generator.d.ts +10 -3
- package/lib/generator.js +4 -4
- package/lib/index.d.ts +12 -5
- package/lib/index.js +5 -5
- package/lib/locales/en-US.json +1 -0
- package/lib/locales/ja-JP.json +1 -0
- package/lib/{renderer-BgAkL9Re.js → renderer-BDs5T_Z4.js} +62 -25
- package/lib/renderer.d.ts +19 -14
- package/lib/renderer.js +2 -2
- package/lib/{types.d-BqXvgR9J.d.ts → types-BVZKXMbJ.d.ts} +110 -118
- package/lib/{utils-6RnUrIMZ.js → utils-DWRmOGGa.js} +7 -7
- package/package.json +11 -10
- package/lib/definition.d-DaKM6lt0.d.ts +0 -21
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { COMMAND_OPTIONS_DEFAULT,
|
|
2
|
-
import { createCommandContext } from "./context-
|
|
3
|
-
import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-
|
|
1
|
+
import { COMMAND_OPTIONS_DEFAULT, COMMON_ARGS, create, resolveLazyCommand } from "./utils-DWRmOGGa.js";
|
|
2
|
+
import { createCommandContext } from "./context-CBDAvGDh.js";
|
|
3
|
+
import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-BDs5T_Z4.js";
|
|
4
4
|
import { parseArgs, resolveArgs } from "args-tokens";
|
|
5
5
|
|
|
6
6
|
//#region src/cli.ts
|
|
@@ -11,21 +11,24 @@ import { parseArgs, resolveArgs } from "args-tokens";
|
|
|
11
11
|
* @param opts A {@link CommandOptions | command options}
|
|
12
12
|
* @returns A rendered usage or undefined. if you will use {@link CommandOptions.usageSilent} option, it will return rendered usage string.
|
|
13
13
|
*/
|
|
14
|
-
async function cli(
|
|
15
|
-
const tokens = parseArgs(
|
|
14
|
+
async function cli(argv, entry, opts = {}) {
|
|
15
|
+
const tokens = parseArgs(argv);
|
|
16
16
|
const subCommand = getSubCommand(tokens);
|
|
17
17
|
const resolvedCommandOptions = resolveCommandOptions(opts, entry);
|
|
18
18
|
const [name, command] = await resolveCommand(subCommand, entry, resolvedCommandOptions, true);
|
|
19
19
|
if (!command) throw new Error(`Command not found: ${name || ""}`);
|
|
20
|
-
const
|
|
21
|
-
const { values, positionals, rest, error } = resolveArgs(
|
|
20
|
+
const args = resolveArguments(command.args);
|
|
21
|
+
const { values, positionals, rest, error } = resolveArgs(args, tokens, {
|
|
22
|
+
optionGrouping: true,
|
|
23
|
+
skipPositional: resolvedCommandOptions.subCommands.size > 0 ? 0 : -1
|
|
24
|
+
});
|
|
22
25
|
const omitted = !subCommand;
|
|
23
26
|
const ctx = await createCommandContext({
|
|
24
|
-
|
|
27
|
+
args,
|
|
25
28
|
values,
|
|
26
29
|
positionals,
|
|
27
30
|
rest,
|
|
28
|
-
|
|
31
|
+
argv,
|
|
29
32
|
tokens,
|
|
30
33
|
omitted,
|
|
31
34
|
command,
|
|
@@ -50,8 +53,8 @@ async function cli(args, entry, opts = {}) {
|
|
|
50
53
|
if (!command.run) throw new Error(`'run' not found on Command \`${name || ""}\``);
|
|
51
54
|
await command.run(ctx);
|
|
52
55
|
}
|
|
53
|
-
function
|
|
54
|
-
return Object.assign(create(), options,
|
|
56
|
+
function resolveArguments(options) {
|
|
57
|
+
return Object.assign(create(), options, COMMON_ARGS);
|
|
55
58
|
}
|
|
56
59
|
function resolveCommandOptions(options, entry) {
|
|
57
60
|
const subCommands = new Map(options.subCommands);
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, DEFAULT_LOCALE$1 as DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey,
|
|
1
|
+
import { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, DEFAULT_LOCALE$1 as DEFAULT_LOCALE, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, resolveArgKey, resolveLazyCommand } from "./utils-DWRmOGGa.js";
|
|
2
2
|
|
|
3
3
|
//#region src/locales/en-US.json
|
|
4
4
|
var COMMAND = "COMMAND";
|
|
5
5
|
var COMMANDS = "COMMANDS";
|
|
6
6
|
var SUBCOMMAND = "SUBCOMMAND";
|
|
7
7
|
var USAGE = "USAGE";
|
|
8
|
+
var ARGUMENTS = "ARGUMENTS";
|
|
8
9
|
var OPTIONS = "OPTIONS";
|
|
9
10
|
var EXAMPLES = "EXAMPLES";
|
|
10
11
|
var FORMORE = "For more info, run any command with the `--help` flag:";
|
|
@@ -18,6 +19,7 @@ var en_US_default = {
|
|
|
18
19
|
COMMANDS,
|
|
19
20
|
SUBCOMMAND,
|
|
20
21
|
USAGE,
|
|
22
|
+
ARGUMENTS,
|
|
21
23
|
OPTIONS,
|
|
22
24
|
EXAMPLES,
|
|
23
25
|
FORMORE,
|
|
@@ -70,11 +72,11 @@ const BUILT_IN_PREFIX_CODE = BUILT_IN_PREFIX.codePointAt(0);
|
|
|
70
72
|
* @param param A {@link CommandContextParams | parameters} to create a {@link CommandContext | command context}
|
|
71
73
|
* @returns A {@link CommandContext | command context}, which is readonly
|
|
72
74
|
*/
|
|
73
|
-
async function createCommandContext({
|
|
75
|
+
async function createCommandContext({ args, values, positionals, rest, argv, tokens, command, commandOptions, omitted = false }) {
|
|
74
76
|
/**
|
|
75
77
|
* normailize the options schema and values, to avoid prototype pollution
|
|
76
78
|
*/
|
|
77
|
-
const
|
|
79
|
+
const _args = Object.entries(args).reduce((acc, [key, value]) => {
|
|
78
80
|
acc[key] = Object.assign(create(), value);
|
|
79
81
|
return acc;
|
|
80
82
|
}, create());
|
|
@@ -128,11 +130,11 @@ async function createCommandContext({ options, values, positionals, rest, args,
|
|
|
128
130
|
omitted,
|
|
129
131
|
locale,
|
|
130
132
|
env,
|
|
131
|
-
|
|
133
|
+
args: _args,
|
|
132
134
|
values,
|
|
133
135
|
positionals,
|
|
134
136
|
rest,
|
|
135
|
-
_:
|
|
137
|
+
_: argv,
|
|
136
138
|
tokens,
|
|
137
139
|
log: commandOptions.usageSilent ? NOOP : log,
|
|
138
140
|
loadCommands,
|
|
@@ -141,12 +143,12 @@ async function createCommandContext({ options, values, positionals, rest, args,
|
|
|
141
143
|
/**
|
|
142
144
|
* load the command resources
|
|
143
145
|
*/
|
|
144
|
-
const loadedOptionsResources = Object.entries(
|
|
145
|
-
const description =
|
|
146
|
+
const loadedOptionsResources = Object.entries(args).map(([key, arg]) => {
|
|
147
|
+
const description = arg.description || "";
|
|
146
148
|
return [key, description];
|
|
147
149
|
});
|
|
148
150
|
const defaultCommandResource = loadedOptionsResources.reduce((res, [key, value]) => {
|
|
149
|
-
res[
|
|
151
|
+
res[resolveArgKey(key)] = value;
|
|
150
152
|
return res;
|
|
151
153
|
}, create());
|
|
152
154
|
defaultCommandResource.description = command.description || "";
|
package/lib/context.d.ts
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
|
-
import { Command, CommandContext, CommandOptions } from "./types
|
|
2
|
-
import {
|
|
1
|
+
import { Command, CommandContext, CommandOptions } from "./types-BVZKXMbJ.js";
|
|
2
|
+
import { ArgToken, ArgValues, Args } from "args-tokens";
|
|
3
3
|
|
|
4
4
|
//#region src/context.d.ts
|
|
5
5
|
/**
|
|
6
6
|
* Parameters of {@link createCommandContext}
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Parameters of {@link createCommandContext}
|
|
11
|
+
*/
|
|
12
|
+
interface CommandContextParams<A extends Args, V> {
|
|
10
13
|
/**
|
|
11
|
-
* An
|
|
14
|
+
* An arguments of target command
|
|
12
15
|
*/
|
|
13
|
-
|
|
16
|
+
args: A;
|
|
14
17
|
/**
|
|
15
18
|
* A values of target command
|
|
16
19
|
*/
|
|
17
|
-
values:
|
|
20
|
+
values: V;
|
|
18
21
|
/**
|
|
19
22
|
* A positionals arguments, which passed to the target command
|
|
20
23
|
*/
|
|
@@ -26,7 +29,7 @@ interface CommandContextParams<Options extends ArgOptions, Values> {
|
|
|
26
29
|
/**
|
|
27
30
|
* Original command line arguments
|
|
28
31
|
*/
|
|
29
|
-
|
|
32
|
+
argv: string[];
|
|
30
33
|
/**
|
|
31
34
|
* Argument tokens that are parsed by the `parseArgs` function
|
|
32
35
|
*/
|
|
@@ -38,26 +41,26 @@ interface CommandContextParams<Options extends ArgOptions, Values> {
|
|
|
38
41
|
/**
|
|
39
42
|
* A target {@link Command | command}
|
|
40
43
|
*/
|
|
41
|
-
command: Command<
|
|
44
|
+
command: Command<A>;
|
|
42
45
|
/**
|
|
43
46
|
* A command options, which is spicialized from `cli` function
|
|
44
47
|
*/
|
|
45
|
-
commandOptions: CommandOptions<
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
declare function createCommandContext<
|
|
53
|
-
|
|
48
|
+
commandOptions: CommandOptions<A>;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Create a {@link CommandContext | command context}
|
|
52
|
+
* @param param A {@link CommandContextParams | parameters} to create a {@link CommandContext | command context}
|
|
53
|
+
* @returns A {@link CommandContext | command context}, which is readonly
|
|
54
|
+
*/
|
|
55
|
+
declare function createCommandContext<A extends Args = Args, V extends ArgValues<A> = ArgValues<A>>({
|
|
56
|
+
args,
|
|
54
57
|
values,
|
|
55
58
|
positionals,
|
|
56
59
|
rest,
|
|
57
|
-
|
|
60
|
+
argv,
|
|
58
61
|
tokens,
|
|
59
62
|
command,
|
|
60
63
|
commandOptions,
|
|
61
64
|
omitted
|
|
62
|
-
}: CommandContextParams<
|
|
65
|
+
}: CommandContextParams<A, V>): Promise<Readonly<CommandContext<A, V>>>; //#endregion
|
|
63
66
|
export { createCommandContext };
|
package/lib/context.js
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Command, CommandLoader, LazyCommand } from "./types-BVZKXMbJ.js";
|
|
2
|
+
import { ArgSchema, ArgValues as ArgValues$1, Args, Args as Args$1 } from "args-tokens";
|
|
3
|
+
|
|
4
|
+
//#region src/definition.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Define a {@link Command | command} with type inference
|
|
7
|
+
* @param definition A {@link Command | command} definition
|
|
8
|
+
* @returns A {@link Command | command} definition with type inference
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Define a {@link Command | command} with type inference
|
|
13
|
+
* @param definition A {@link Command | command} definition
|
|
14
|
+
* @returns A {@link Command | command} definition with type inference
|
|
15
|
+
*/
|
|
16
|
+
declare function define<A extends Args = Args>(definition: Command<A>): Command<A>;
|
|
17
|
+
/**
|
|
18
|
+
* Define a {@link LazyCommand | lazy command} with command loader, which is attached with command definition as usage metadata.
|
|
19
|
+
* @param loader A {@link CommandLoader | command loader}
|
|
20
|
+
* @param definition A {@link Command | command} definition
|
|
21
|
+
* @returns A {@link LazyCommand | lazy command} loader
|
|
22
|
+
*/
|
|
23
|
+
declare function lazy<A extends Args = Args>(loader: CommandLoader<A>, definition?: Command<A>): LazyCommand<A>;
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
export { ArgSchema, ArgValues$1 as ArgValues, Args$1 as Args, define as define$1, lazy as lazy$1 };
|
|
@@ -17,7 +17,7 @@ function lazy(loader, definition) {
|
|
|
17
17
|
if (definition != null) {
|
|
18
18
|
loader.commandName = definition.name;
|
|
19
19
|
loader.description = definition.description;
|
|
20
|
-
loader.
|
|
20
|
+
loader.args = definition.args;
|
|
21
21
|
loader.examples = definition.examples;
|
|
22
22
|
loader.resource = definition.resource;
|
|
23
23
|
}
|
package/lib/definition.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import "./types
|
|
2
|
-
import {
|
|
3
|
-
export {
|
|
1
|
+
import "./types-BVZKXMbJ.js";
|
|
2
|
+
import { ArgSchema, ArgValues, Args, define$1 as define, lazy$1 as lazy } from "./definition-Bdw7cMea.js";
|
|
3
|
+
export { ArgSchema, ArgValues, Args, define, lazy };
|
package/lib/definition.js
CHANGED
package/lib/generator.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Command, CommandOptions } from "./types
|
|
2
|
-
import {
|
|
1
|
+
import { Command, CommandOptions } from "./types-BVZKXMbJ.js";
|
|
2
|
+
import { Args } from "args-tokens";
|
|
3
3
|
|
|
4
4
|
//#region src/generator.d.ts
|
|
5
5
|
/**
|
|
@@ -10,7 +10,14 @@ import { ArgOptions } from "args-tokens";
|
|
|
10
10
|
* @returns A rendered usage.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Generate the command usage.
|
|
15
|
+
* @param command - usage generate command, if you want to generate the usage of the default command where there are target commands and sub-commands, specify `null`.
|
|
16
|
+
* @param entry - A {@link Command | entry command}
|
|
17
|
+
* @param opts - A {@link CommandOptions | command options}
|
|
18
|
+
* @returns A rendered usage.
|
|
19
|
+
*/
|
|
20
|
+
declare function generate<A extends Args = Args>(command: string | null, entry: Command<A>, opts?: CommandOptions<A>): Promise<string>;
|
|
14
21
|
|
|
15
22
|
//#endregion
|
|
16
23
|
export { generate };
|
package/lib/generator.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { create } from "./utils-
|
|
2
|
-
import "./context-
|
|
3
|
-
import "./renderer-
|
|
4
|
-
import { cli } from "./cli-
|
|
1
|
+
import { create } from "./utils-DWRmOGGa.js";
|
|
2
|
+
import "./context-CBDAvGDh.js";
|
|
3
|
+
import "./renderer-BDs5T_Z4.js";
|
|
4
|
+
import { cli } from "./cli-m93bHwFR.js";
|
|
5
5
|
|
|
6
6
|
//#region src/generator.ts
|
|
7
7
|
/**
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Command,
|
|
2
|
-
import { define$1 as define, lazy$1 as lazy } from "./definition
|
|
3
|
-
import {
|
|
1
|
+
import { Command, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandLoader, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgs, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions } from "./types-BVZKXMbJ.js";
|
|
2
|
+
import { define$1 as define, lazy$1 as lazy } from "./definition-Bdw7cMea.js";
|
|
3
|
+
import { ArgSchema, ArgValues, Args, Args as Args$1, parseArgs, resolveArgs } from "args-tokens";
|
|
4
4
|
|
|
5
5
|
//#region src/cli.d.ts
|
|
6
6
|
/**
|
|
@@ -11,7 +11,14 @@ import { ArgOptionSchema, ArgOptions, ArgOptions as ArgOptions$1, ArgValues, par
|
|
|
11
11
|
* @returns A rendered usage or undefined. if you will use {@link CommandOptions.usageSilent} option, it will return rendered usage string.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Run the command.
|
|
16
|
+
* @param args Command line arguments
|
|
17
|
+
* @param entry A {@link Command | entry command} or an {@link CommandRunner | inline command runner}
|
|
18
|
+
* @param opts A {@link CommandOptions | command options}
|
|
19
|
+
* @returns A rendered usage or undefined. if you will use {@link CommandOptions.usageSilent} option, it will return rendered usage string.
|
|
20
|
+
*/
|
|
21
|
+
declare function cli<A extends Args$1 = Args$1>(argv: string[], entry: Command<A> | CommandRunner<A>, opts?: CommandOptions<A>): Promise<string | undefined>;
|
|
15
22
|
|
|
16
23
|
//#endregion
|
|
17
24
|
//#region src/translation.d.ts
|
|
@@ -25,4 +32,4 @@ declare class DefaultTranslation implements TranslationAdapter {
|
|
|
25
32
|
}
|
|
26
33
|
|
|
27
34
|
//#endregion
|
|
28
|
-
export {
|
|
35
|
+
export { ArgSchema, ArgValues, Args, Command, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandLoader, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, DefaultTranslation, GenerateNamespacedKey, KeyOfArgs, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions, cli, define, lazy, parseArgs, resolveArgs };
|
package/lib/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { DEFAULT_LOCALE$1 as DEFAULT_LOCALE } from "./utils-
|
|
2
|
-
import { DefaultTranslation } from "./context-
|
|
3
|
-
import { define, lazy } from "./definition-
|
|
4
|
-
import "./renderer-
|
|
5
|
-
import { cli } from "./cli-
|
|
1
|
+
import { DEFAULT_LOCALE$1 as DEFAULT_LOCALE } from "./utils-DWRmOGGa.js";
|
|
2
|
+
import { DefaultTranslation } from "./context-CBDAvGDh.js";
|
|
3
|
+
import { define, lazy } from "./definition-DyVBFqB7.js";
|
|
4
|
+
import "./renderer-BDs5T_Z4.js";
|
|
5
|
+
import { cli } from "./cli-m93bHwFR.js";
|
|
6
6
|
import { parseArgs, resolveArgs } from "args-tokens";
|
|
7
7
|
|
|
8
8
|
export { DEFAULT_LOCALE, DefaultTranslation, cli, define, lazy, parseArgs, resolveArgs };
|
package/lib/locales/en-US.json
CHANGED
package/lib/locales/ja-JP.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { COMMON_ARGS, create, resolveArgKey, resolveBuiltInKey } from "./utils-DWRmOGGa.js";
|
|
2
2
|
|
|
3
3
|
//#region src/renderer/header.ts
|
|
4
4
|
/**
|
|
@@ -13,7 +13,7 @@ function renderHeader(ctx) {
|
|
|
13
13
|
|
|
14
14
|
//#endregion
|
|
15
15
|
//#region src/renderer/usage.ts
|
|
16
|
-
const
|
|
16
|
+
const COMMON_ARGS_KEYS = Object.keys(COMMON_ARGS);
|
|
17
17
|
/**
|
|
18
18
|
* Render the usage.
|
|
19
19
|
* @param ctx A {@link CommandContext | command context}
|
|
@@ -27,20 +27,32 @@ async function renderUsage(ctx) {
|
|
|
27
27
|
}
|
|
28
28
|
messages.push(...await renderUsageSection(ctx), "");
|
|
29
29
|
if (ctx.omitted && await hasCommands(ctx)) messages.push(...await renderCommandsSection(ctx), "");
|
|
30
|
-
if (
|
|
30
|
+
if (hasPositionalArgs(ctx)) messages.push(...await renderPositionalArgsSection(ctx), "");
|
|
31
|
+
if (hasOptionalArgs(ctx)) messages.push(...await renderOptionalArgsSection(ctx), "");
|
|
31
32
|
const examples = renderExamplesSection(ctx);
|
|
32
33
|
if (examples.length > 0) messages.push(...examples, "");
|
|
33
34
|
return messages.join("\n");
|
|
34
35
|
}
|
|
35
36
|
/**
|
|
36
|
-
* Render the
|
|
37
|
+
* Render the positional arguments section
|
|
38
|
+
* @param ctx A {@link CommandContext | command context}
|
|
39
|
+
* @returns A rendered arguments section
|
|
40
|
+
*/
|
|
41
|
+
async function renderPositionalArgsSection(ctx) {
|
|
42
|
+
const messages = [];
|
|
43
|
+
messages.push(`${ctx.translate(resolveBuiltInKey("ARGUMENTS"))}:`);
|
|
44
|
+
messages.push(await generatePositionalArgsUsage(ctx));
|
|
45
|
+
return messages;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Render the optional arguments section
|
|
37
49
|
* @param ctx A {@link CommandContext | command context}
|
|
38
50
|
* @returns A rendered options section
|
|
39
51
|
*/
|
|
40
|
-
async function
|
|
52
|
+
async function renderOptionalArgsSection(ctx) {
|
|
41
53
|
const messages = [];
|
|
42
54
|
messages.push(`${ctx.translate(resolveBuiltInKey("OPTIONS"))}:`);
|
|
43
|
-
messages.push(await
|
|
55
|
+
messages.push(await generateOptionalArgsUsage(ctx, getOptionalArgsPairs(ctx)));
|
|
44
56
|
return messages;
|
|
45
57
|
}
|
|
46
58
|
/**
|
|
@@ -65,14 +77,14 @@ function renderExamplesSection(ctx) {
|
|
|
65
77
|
async function renderUsageSection(ctx) {
|
|
66
78
|
const messages = [`${ctx.translate(resolveBuiltInKey("USAGE"))}:`];
|
|
67
79
|
if (ctx.omitted) {
|
|
68
|
-
const defaultCommand = `${resolveEntry(ctx)}${await hasCommands(ctx) ? ` [${resolveSubCommand(ctx)}]` : ""} ${
|
|
80
|
+
const defaultCommand = `${resolveEntry(ctx)}${await hasCommands(ctx) ? ` [${resolveSubCommand(ctx)}]` : ""} ${[generateOptionsSymbols(ctx), generatePositionalSymbols(ctx)].filter(Boolean).join(" ")}`;
|
|
69
81
|
messages.push(defaultCommand.padStart(ctx.env.leftMargin + defaultCommand.length));
|
|
70
82
|
if (await hasCommands(ctx)) {
|
|
71
83
|
const commandsUsage = `${resolveEntry(ctx)} <${ctx.translate(resolveBuiltInKey("COMMANDS"))}>`;
|
|
72
84
|
messages.push(commandsUsage.padStart(ctx.env.leftMargin + commandsUsage.length));
|
|
73
85
|
}
|
|
74
86
|
} else {
|
|
75
|
-
const usageStr = `${resolveEntry(ctx)} ${resolveSubCommand(ctx)} ${generateOptionsSymbols(ctx)}`;
|
|
87
|
+
const usageStr = `${resolveEntry(ctx)} ${resolveSubCommand(ctx)} ${[generateOptionsSymbols(ctx), generatePositionalSymbols(ctx)].filter(Boolean).join(" ")}`;
|
|
76
88
|
messages.push(usageStr.padStart(ctx.env.leftMargin + usageStr.length));
|
|
77
89
|
}
|
|
78
90
|
return messages;
|
|
@@ -144,12 +156,20 @@ async function hasCommands(ctx) {
|
|
|
144
156
|
return loadedCommands.length > 1;
|
|
145
157
|
}
|
|
146
158
|
/**
|
|
147
|
-
* Check if the command has
|
|
159
|
+
* Check if the command has optional arguments
|
|
160
|
+
* @param ctx A {@link CommandContext | command context}
|
|
161
|
+
* @returns True if the command has options
|
|
162
|
+
*/
|
|
163
|
+
function hasOptionalArgs(ctx) {
|
|
164
|
+
return !!(ctx.args && Object.values(ctx.args).some((arg) => arg.type !== "positional"));
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Check if the command has positional arguments
|
|
148
168
|
* @param ctx A {@link CommandContext | command context}
|
|
149
169
|
* @returns True if the command has options
|
|
150
170
|
*/
|
|
151
|
-
function
|
|
152
|
-
return !!(ctx.
|
|
171
|
+
function hasPositionalArgs(ctx) {
|
|
172
|
+
return !!(ctx.args && Object.values(ctx.args).some((arg) => arg.type === "positional"));
|
|
153
173
|
}
|
|
154
174
|
/**
|
|
155
175
|
* Check if all options have default values
|
|
@@ -157,7 +177,7 @@ function hasOptions(ctx) {
|
|
|
157
177
|
* @returns True if all options have default values
|
|
158
178
|
*/
|
|
159
179
|
function hasAllDefaultOptions(ctx) {
|
|
160
|
-
return !!(ctx.
|
|
180
|
+
return !!(ctx.args && Object.values(ctx.args).every((arg) => arg.default));
|
|
161
181
|
}
|
|
162
182
|
/**
|
|
163
183
|
* Generate options symbols for usage
|
|
@@ -165,7 +185,7 @@ function hasAllDefaultOptions(ctx) {
|
|
|
165
185
|
* @returns Options symbols for usage
|
|
166
186
|
*/
|
|
167
187
|
function generateOptionsSymbols(ctx) {
|
|
168
|
-
return
|
|
188
|
+
return hasOptionalArgs(ctx) ? hasAllDefaultOptions(ctx) ? `[${ctx.translate(resolveBuiltInKey("OPTIONS"))}]` : `<${ctx.translate(resolveBuiltInKey("OPTIONS"))}>` : "";
|
|
169
189
|
}
|
|
170
190
|
function makeShortLongOptionPair(schema, name) {
|
|
171
191
|
let key = `--${name}`;
|
|
@@ -173,29 +193,30 @@ function makeShortLongOptionPair(schema, name) {
|
|
|
173
193
|
return key;
|
|
174
194
|
}
|
|
175
195
|
/**
|
|
176
|
-
* Get
|
|
196
|
+
* Get optional arguments pairs for usage
|
|
177
197
|
* @param ctx A {@link CommandContext | command context}
|
|
178
198
|
* @returns Options pairs for usage
|
|
179
199
|
*/
|
|
180
|
-
function
|
|
181
|
-
return Object.entries(ctx.
|
|
200
|
+
function getOptionalArgsPairs(ctx) {
|
|
201
|
+
return Object.entries(ctx.args).reduce((acc, [name, value]) => {
|
|
202
|
+
if (value.type === "positional") return acc;
|
|
182
203
|
let key = makeShortLongOptionPair(value, name);
|
|
183
204
|
if (value.type !== "boolean") key = value.default ? `${key} [${name}]` : `${key} <${name}>`;
|
|
184
205
|
acc[name] = key;
|
|
185
|
-
if (value.type === "boolean" && value.negatable && !
|
|
206
|
+
if (value.type === "boolean" && value.negatable && !COMMON_ARGS_KEYS.includes(name)) acc[`no-${name}`] = `--no-${name}`;
|
|
186
207
|
return acc;
|
|
187
208
|
}, create());
|
|
188
209
|
}
|
|
189
210
|
const resolveNegatableKey = (key) => key.split("no-")[1];
|
|
190
211
|
function resolveNegatableType(key, ctx) {
|
|
191
|
-
return ctx.
|
|
212
|
+
return ctx.args[key.startsWith("no-") ? resolveNegatableKey(key) : key].type;
|
|
192
213
|
}
|
|
193
214
|
function generateDefaultDisplayValue(ctx, schema) {
|
|
194
215
|
return `${ctx.translate(resolveBuiltInKey("DEFAULT"))}: ${schema.default}`;
|
|
195
216
|
}
|
|
196
217
|
function resolveDisplayValue(ctx, key) {
|
|
197
|
-
if (
|
|
198
|
-
const schema = ctx.
|
|
218
|
+
if (COMMON_ARGS_KEYS.includes(key)) return "";
|
|
219
|
+
const schema = ctx.args[key];
|
|
199
220
|
if ((schema.type === "boolean" || schema.type === "number" || schema.type === "string") && schema.default !== void 0) return `(${generateDefaultDisplayValue(ctx, schema)})`;
|
|
200
221
|
if (schema.type === "enum") {
|
|
201
222
|
const _default = schema.default !== void 0 ? generateDefaultDisplayValue(ctx, schema) : "";
|
|
@@ -205,19 +226,19 @@ function resolveDisplayValue(ctx, key) {
|
|
|
205
226
|
return "";
|
|
206
227
|
}
|
|
207
228
|
/**
|
|
208
|
-
* Generate
|
|
229
|
+
* Generate optional arguments usage
|
|
209
230
|
* @param ctx A {@link CommandContext | command context}
|
|
210
231
|
* @param optionsPairs Options pairs for usage
|
|
211
232
|
* @returns Generated options usage
|
|
212
233
|
*/
|
|
213
|
-
async function
|
|
234
|
+
async function generateOptionalArgsUsage(ctx, optionsPairs) {
|
|
214
235
|
const optionsMaxLength = Math.max(...Object.entries(optionsPairs).map(([_, value]) => value.length));
|
|
215
|
-
const optionSchemaMaxLength = ctx.env.usageOptionType ? Math.max(...Object.entries(optionsPairs).map(([key
|
|
236
|
+
const optionSchemaMaxLength = ctx.env.usageOptionType ? Math.max(...Object.entries(optionsPairs).map(([key]) => resolveNegatableType(key, ctx).length)) : 0;
|
|
216
237
|
const usages = await Promise.all(Object.entries(optionsPairs).map(([key, value]) => {
|
|
217
|
-
let rawDesc = ctx.translate(
|
|
238
|
+
let rawDesc = ctx.translate(resolveArgKey(key));
|
|
218
239
|
if (!rawDesc && key.startsWith("no-")) {
|
|
219
240
|
const name = resolveNegatableKey(key);
|
|
220
|
-
const schema = ctx.
|
|
241
|
+
const schema = ctx.args[name];
|
|
221
242
|
const optionKey = makeShortLongOptionPair(schema, name);
|
|
222
243
|
rawDesc = `${ctx.translate(resolveBuiltInKey("NEGATABLE"))} ${optionKey}`;
|
|
223
244
|
}
|
|
@@ -229,6 +250,22 @@ async function generateOptionsUsage(ctx, optionsPairs) {
|
|
|
229
250
|
}));
|
|
230
251
|
return usages.join("\n");
|
|
231
252
|
}
|
|
253
|
+
function getPositionalArgs(ctx) {
|
|
254
|
+
return Object.entries(ctx.args).filter(([_, schema]) => schema.type === "positional");
|
|
255
|
+
}
|
|
256
|
+
async function generatePositionalArgsUsage(ctx) {
|
|
257
|
+
const positionals = getPositionalArgs(ctx);
|
|
258
|
+
const argsMaxLength = Math.max(...positionals.map(([name]) => name.length));
|
|
259
|
+
const usages = await Promise.all(positionals.map(([name]) => {
|
|
260
|
+
const desc = ctx.translate(resolveArgKey(name)) || ctx.args[name].description || "";
|
|
261
|
+
const arg = `${name.padEnd(argsMaxLength + ctx.env.middleMargin)} ${desc}`;
|
|
262
|
+
return `${arg.padStart(ctx.env.leftMargin + arg.length)}`;
|
|
263
|
+
}));
|
|
264
|
+
return usages.join("\n");
|
|
265
|
+
}
|
|
266
|
+
function generatePositionalSymbols(ctx) {
|
|
267
|
+
return hasPositionalArgs(ctx) ? getPositionalArgs(ctx).map(([name]) => `<${name}>`).join(" ") : "";
|
|
268
|
+
}
|
|
232
269
|
|
|
233
270
|
//#endregion
|
|
234
271
|
//#region src/renderer/validation.ts
|
package/lib/renderer.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { CommandContext } from "./types
|
|
2
|
-
import {
|
|
1
|
+
import { CommandContext } from "./types-BVZKXMbJ.js";
|
|
2
|
+
import { Args } from "args-tokens";
|
|
3
3
|
|
|
4
4
|
//#region src/renderer/header.d.ts
|
|
5
5
|
/**
|
|
@@ -8,26 +8,31 @@ import { ArgOptions } from "args-tokens";
|
|
|
8
8
|
* @returns A rendered header.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Render the header.
|
|
13
|
+
* @param ctx A {@link CommandContext | command context}
|
|
14
|
+
* @returns A rendered header.
|
|
15
|
+
*/
|
|
16
|
+
declare function renderHeader<A extends Args = Args>(ctx: Readonly<CommandContext<A>>): Promise<string>;
|
|
12
17
|
|
|
13
18
|
//#endregion
|
|
14
19
|
//#region src/renderer/usage.d.ts
|
|
15
20
|
/**
|
|
16
|
-
* Render the usage.
|
|
17
|
-
* @param ctx A {@link CommandContext | command context}
|
|
18
|
-
* @returns A rendered usage.
|
|
19
|
-
*/
|
|
20
|
-
declare function renderUsage<
|
|
21
|
+
* Render the usage.
|
|
22
|
+
* @param ctx A {@link CommandContext | command context}
|
|
23
|
+
* @returns A rendered usage.
|
|
24
|
+
*/
|
|
25
|
+
declare function renderUsage<A extends Args = Args>(ctx: Readonly<CommandContext<A>>): Promise<string>;
|
|
21
26
|
|
|
22
27
|
//#endregion
|
|
23
28
|
//#region src/renderer/validation.d.ts
|
|
24
29
|
/**
|
|
25
|
-
* Render the validation errors.
|
|
26
|
-
* @param ctx A {@link CommandContext | command context}
|
|
27
|
-
* @param error An {@link AggregateError} of option in `args-token` validation
|
|
28
|
-
* @returns A rendered validation error.
|
|
29
|
-
*/
|
|
30
|
-
declare function renderValidationErrors<
|
|
30
|
+
* Render the validation errors.
|
|
31
|
+
* @param ctx A {@link CommandContext | command context}
|
|
32
|
+
* @param error An {@link AggregateError} of option in `args-token` validation
|
|
33
|
+
* @returns A rendered validation error.
|
|
34
|
+
*/
|
|
35
|
+
declare function renderValidationErrors<A extends Args = Args>(_ctx: CommandContext<A>, error: AggregateError): Promise<string>;
|
|
31
36
|
|
|
32
37
|
//#endregion
|
|
33
38
|
export { renderHeader, renderUsage, renderValidationErrors };
|
package/lib/renderer.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "./utils-
|
|
2
|
-
import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-
|
|
1
|
+
import "./utils-DWRmOGGa.js";
|
|
2
|
+
import { renderHeader, renderUsage, renderValidationErrors } from "./renderer-BDs5T_Z4.js";
|
|
3
3
|
|
|
4
4
|
export { renderHeader, renderUsage, renderValidationErrors };
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ArgToken, ArgValues, Args } from "args-tokens";
|
|
2
2
|
|
|
3
3
|
//#region rolldown:runtime
|
|
4
4
|
declare namespace constants_d_exports {
|
|
5
|
-
export { BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, COMMAND_BUILTIN_RESOURCE_KEYS, COMMAND_OPTIONS_DEFAULT,
|
|
5
|
+
export { ARG_PREFIX, BUILT_IN_KEY_SEPARATOR, BUILT_IN_PREFIX, COMMAND_BUILTIN_RESOURCE_KEYS, COMMAND_OPTIONS_DEFAULT, COMMON_ARGS, DEFAULT_LOCALE, NOOP };
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
|
-
* The default locale string, which format is BCP 47 language tag.
|
|
9
|
-
*/
|
|
8
|
+
* The default locale string, which format is BCP 47 language tag.
|
|
9
|
+
*/
|
|
10
10
|
declare const DEFAULT_LOCALE = "en-US";
|
|
11
11
|
declare const BUILT_IN_PREFIX = "_";
|
|
12
|
-
declare const
|
|
12
|
+
declare const ARG_PREFIX = "arg";
|
|
13
13
|
declare const BUILT_IN_KEY_SEPARATOR = ":";
|
|
14
14
|
declare const NOOP: () => void;
|
|
15
|
-
type
|
|
15
|
+
type CommonArgType = {
|
|
16
16
|
readonly help: {
|
|
17
17
|
readonly type: 'boolean';
|
|
18
18
|
readonly short: 'h';
|
|
@@ -24,52 +24,45 @@ type CommonOptionType = {
|
|
|
24
24
|
readonly description: string;
|
|
25
25
|
};
|
|
26
26
|
};
|
|
27
|
-
declare const
|
|
28
|
-
declare const COMMAND_OPTIONS_DEFAULT: CommandOptions<
|
|
29
|
-
declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "OPTIONS", "EXAMPLES", "FORMORE", "NEGATABLE", "DEFAULT", "CHOICES"];
|
|
27
|
+
declare const COMMON_ARGS: CommonArgType;
|
|
28
|
+
declare const COMMAND_OPTIONS_DEFAULT: CommandOptions<Args>;
|
|
29
|
+
declare const COMMAND_BUILTIN_RESOURCE_KEYS: readonly ["USAGE", "COMMAND", "SUBCOMMAND", "COMMANDS", "ARGUMENTS", "OPTIONS", "EXAMPLES", "FORMORE", "NEGATABLE", "DEFAULT", "CHOICES"];
|
|
30
30
|
|
|
31
31
|
//#endregion
|
|
32
32
|
//#region src/types.d.ts
|
|
33
33
|
type Awaitable<T> = T | Promise<T>;
|
|
34
34
|
type RemoveIndexSignature<T> = { [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K] };
|
|
35
|
-
|
|
36
35
|
/**
|
|
37
|
-
* Remove index signature from object or record type.
|
|
38
|
-
*/
|
|
36
|
+
* Remove index signature from object or record type.
|
|
37
|
+
*/
|
|
39
38
|
type RemovedIndex<T> = RemoveIndexSignature<{ [K in keyof T]: T[K] }>;
|
|
40
|
-
type
|
|
41
|
-
|
|
39
|
+
type KeyOfArgs<A extends Args> = keyof A | { [K in keyof A]: A[K]['type'] extends 'boolean' ? A[K]['negatable'] extends true ? `no-${Extract<K, string>}` : never : never }[keyof A];
|
|
42
40
|
/**
|
|
43
|
-
* Generate a namespaced key.
|
|
44
|
-
*/
|
|
41
|
+
* Generate a namespaced key.
|
|
42
|
+
*/
|
|
45
43
|
type GenerateNamespacedKey<Key extends string, Prefixed extends string = typeof BUILT_IN_PREFIX> = `${Prefixed}${typeof BUILT_IN_KEY_SEPARATOR}${Key}`;
|
|
46
|
-
|
|
47
44
|
/**
|
|
48
|
-
* Command i18n built-in
|
|
49
|
-
*/
|
|
50
|
-
type
|
|
51
|
-
|
|
45
|
+
* Command i18n built-in arguments keys.
|
|
46
|
+
*/
|
|
47
|
+
type CommandBuiltinArgsKeys = keyof (typeof constants_d_exports)['COMMON_ARGS'];
|
|
52
48
|
/**
|
|
53
|
-
* Command i18n built-in resource keys.
|
|
54
|
-
*/
|
|
49
|
+
* Command i18n built-in resource keys.
|
|
50
|
+
*/
|
|
55
51
|
type CommandBuiltinResourceKeys = (typeof constants_d_exports)['COMMAND_BUILTIN_RESOURCE_KEYS'][number];
|
|
56
|
-
|
|
57
52
|
/**
|
|
58
|
-
* Command i18n built-in keys.
|
|
59
|
-
* The command i18n built-in keys are used to {@link CommandContext.translate | translate} function.
|
|
60
|
-
*/
|
|
61
|
-
type CommandBuiltinKeys = GenerateNamespacedKey<
|
|
62
|
-
|
|
53
|
+
* Command i18n built-in keys.
|
|
54
|
+
* The command i18n built-in keys are used to {@link CommandContext.translate | translate} function.
|
|
55
|
+
*/
|
|
56
|
+
type CommandBuiltinKeys = GenerateNamespacedKey<CommandBuiltinArgsKeys> | GenerateNamespacedKey<CommandBuiltinResourceKeys> | 'description' | 'examples';
|
|
63
57
|
/**
|
|
64
|
-
* Command i18n option keys.
|
|
65
|
-
* The command i18n option keys are used to {@link CommandContext.translate | translate} function.
|
|
66
|
-
*/
|
|
67
|
-
type
|
|
68
|
-
|
|
58
|
+
* Command i18n option keys.
|
|
59
|
+
* The command i18n option keys are used to {@link CommandContext.translate | translate} function.
|
|
60
|
+
*/
|
|
61
|
+
type CommandArgKeys<A extends Args> = GenerateNamespacedKey<KeyOfArgs<RemovedIndex<A>>, typeof ARG_PREFIX>;
|
|
69
62
|
/**
|
|
70
|
-
* Command environment.
|
|
71
|
-
*/
|
|
72
|
-
interface CommandEnvironment<
|
|
63
|
+
* Command environment.
|
|
64
|
+
*/
|
|
65
|
+
interface CommandEnvironment<A extends Args = Args> {
|
|
73
66
|
/**
|
|
74
67
|
* Current working directory.
|
|
75
68
|
* @see {@link CommandOptions.cwd}
|
|
@@ -129,21 +122,20 @@ interface CommandEnvironment<Options extends ArgOptions = ArgOptions> {
|
|
|
129
122
|
/**
|
|
130
123
|
* Render function the command usage.
|
|
131
124
|
*/
|
|
132
|
-
renderUsage: ((ctx: CommandContext<
|
|
125
|
+
renderUsage: ((ctx: CommandContext<A>) => Promise<string>) | null | undefined;
|
|
133
126
|
/**
|
|
134
127
|
* Render function the header section in the command usage.
|
|
135
128
|
*/
|
|
136
|
-
renderHeader: ((ctx: CommandContext<
|
|
129
|
+
renderHeader: ((ctx: CommandContext<A>) => Promise<string>) | null | undefined;
|
|
137
130
|
/**
|
|
138
131
|
* Render function the validation errors.
|
|
139
132
|
*/
|
|
140
|
-
renderValidationErrors: ((ctx: CommandContext<
|
|
133
|
+
renderValidationErrors: ((ctx: CommandContext<A>, error: AggregateError) => Promise<string>) | null | undefined;
|
|
141
134
|
}
|
|
142
|
-
|
|
143
135
|
/**
|
|
144
|
-
* Command options.
|
|
145
|
-
*/
|
|
146
|
-
interface CommandOptions<
|
|
136
|
+
* Command options.
|
|
137
|
+
*/
|
|
138
|
+
interface CommandOptions<A extends Args = Args> {
|
|
147
139
|
/**
|
|
148
140
|
* Current working directory.
|
|
149
141
|
*/
|
|
@@ -178,11 +170,11 @@ interface CommandOptions<Options extends ArgOptions = ArgOptions> {
|
|
|
178
170
|
*/
|
|
179
171
|
middleMargin?: number;
|
|
180
172
|
/**
|
|
181
|
-
* Whether to display the usage
|
|
173
|
+
* Whether to display the usage optional argument type.
|
|
182
174
|
*/
|
|
183
175
|
usageOptionType?: boolean;
|
|
184
176
|
/**
|
|
185
|
-
* Whether to display the
|
|
177
|
+
* Whether to display the optional argument value.
|
|
186
178
|
*/
|
|
187
179
|
usageOptionValue?: boolean;
|
|
188
180
|
/**
|
|
@@ -192,25 +184,25 @@ interface CommandOptions<Options extends ArgOptions = ArgOptions> {
|
|
|
192
184
|
/**
|
|
193
185
|
* Render function the command usage.
|
|
194
186
|
*/
|
|
195
|
-
renderUsage?: ((ctx: Readonly<CommandContext<
|
|
187
|
+
renderUsage?: ((ctx: Readonly<CommandContext<A>>) => Promise<string>) | null;
|
|
196
188
|
/**
|
|
197
189
|
* Render function the header section in the command usage.
|
|
198
190
|
*/
|
|
199
|
-
renderHeader?: ((ctx: Readonly<CommandContext<
|
|
191
|
+
renderHeader?: ((ctx: Readonly<CommandContext<A>>) => Promise<string>) | null;
|
|
200
192
|
/**
|
|
201
193
|
* Render function the validation errors.
|
|
202
194
|
*/
|
|
203
|
-
renderValidationErrors?: ((ctx: Readonly<CommandContext<
|
|
195
|
+
renderValidationErrors?: ((ctx: Readonly<CommandContext<A>>, error: AggregateError) => Promise<string>) | null;
|
|
204
196
|
/**
|
|
205
197
|
* Translation adapter factory.
|
|
206
198
|
*/
|
|
207
199
|
translationAdapterFactory?: TranslationAdapterFactory;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
interface CommandContext<
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Command context.
|
|
203
|
+
* Command context is the context of the command execution.
|
|
204
|
+
*/
|
|
205
|
+
interface CommandContext<A extends Args = Args, V = ArgValues<A>> {
|
|
214
206
|
/**
|
|
215
207
|
* Command name, that is the command that is executed.
|
|
216
208
|
* The command name is same {@link CommandEnvironment.name}.
|
|
@@ -229,17 +221,17 @@ interface CommandContext<Options extends ArgOptions = ArgOptions, Values = ArgVa
|
|
|
229
221
|
* Command environment, that is the environment of the command that is executed.
|
|
230
222
|
* The command environment is same {@link CommandEnvironment}.
|
|
231
223
|
*/
|
|
232
|
-
env: Readonly<CommandEnvironment<
|
|
224
|
+
env: Readonly<CommandEnvironment<A>>;
|
|
233
225
|
/**
|
|
234
|
-
* Command
|
|
235
|
-
* The command
|
|
226
|
+
* Command arguments, that is the arguments of the command that is executed.
|
|
227
|
+
* The command arguments is same {@link Command.args}.
|
|
236
228
|
*/
|
|
237
|
-
|
|
229
|
+
args: A;
|
|
238
230
|
/**
|
|
239
231
|
* Command values, that is the values of the command that is executed.
|
|
240
|
-
* Resolve values with `resolveArgs` from command arguments and {@link Command.
|
|
232
|
+
* Resolve values with `resolveArgs` from command arguments and {@link Command.args}.
|
|
241
233
|
*/
|
|
242
|
-
values:
|
|
234
|
+
values: V;
|
|
243
235
|
/**
|
|
244
236
|
* Command positionals arguments, that is the positionals of the command that is executed.
|
|
245
237
|
* Resolve positionals with `resolveArgs` from command arguments.
|
|
@@ -274,19 +266,19 @@ interface CommandContext<Options extends ArgOptions = ArgOptions, Values = ArgVa
|
|
|
274
266
|
* The loaded commands are cached and returned when called again.
|
|
275
267
|
* @returns loaded commands.
|
|
276
268
|
*/
|
|
277
|
-
loadCommands: () => Promise<Command<
|
|
269
|
+
loadCommands: () => Promise<Command<A>[]>;
|
|
278
270
|
/**
|
|
279
271
|
* Translate function.
|
|
280
272
|
* @param key the key to be translated
|
|
281
273
|
* @param values the values to be formatted
|
|
282
274
|
* @returns A translated string.
|
|
283
275
|
*/
|
|
284
|
-
translate: <T extends string = CommandBuiltinKeys, O =
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
interface Command<
|
|
276
|
+
translate: <T extends string = CommandBuiltinKeys, O = CommandArgKeys<A>, K = CommandBuiltinKeys | O | T>(key: K, values?: Record<string, unknown>) => string;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Command interface.
|
|
280
|
+
*/
|
|
281
|
+
interface Command<A extends Args = Args> {
|
|
290
282
|
/**
|
|
291
283
|
* Command name.
|
|
292
284
|
* It's used to find command line arguments to execute from sub commands, and it's recommended to specify.
|
|
@@ -298,10 +290,10 @@ interface Command<Options extends ArgOptions = ArgOptions> {
|
|
|
298
290
|
*/
|
|
299
291
|
description?: string;
|
|
300
292
|
/**
|
|
301
|
-
* Command
|
|
302
|
-
* Each
|
|
293
|
+
* Command arguments.
|
|
294
|
+
* Each argument can include a description property to describe the argument in usage.
|
|
303
295
|
*/
|
|
304
|
-
|
|
296
|
+
args?: A;
|
|
305
297
|
/**
|
|
306
298
|
* Command examples.
|
|
307
299
|
* examples of how to use the command.
|
|
@@ -310,16 +302,16 @@ interface Command<Options extends ArgOptions = ArgOptions> {
|
|
|
310
302
|
/**
|
|
311
303
|
* Command runner. it's the command to be executed
|
|
312
304
|
*/
|
|
313
|
-
run?: CommandRunner<
|
|
305
|
+
run?: CommandRunner<A>;
|
|
314
306
|
/**
|
|
315
307
|
* Command resource fetcher.
|
|
316
308
|
*/
|
|
317
|
-
resource?: CommandResourceFetcher<
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
type CommandResource<
|
|
309
|
+
resource?: CommandResourceFetcher<A>;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Command resource.
|
|
313
|
+
*/
|
|
314
|
+
type CommandResource<A extends Args = Args> = {
|
|
323
315
|
/**
|
|
324
316
|
* Command description.
|
|
325
317
|
*/
|
|
@@ -328,22 +320,22 @@ type CommandResource<Options extends ArgOptions = ArgOptions> = {
|
|
|
328
320
|
* Examples usage.
|
|
329
321
|
*/
|
|
330
322
|
examples: string;
|
|
331
|
-
} & { [
|
|
323
|
+
} & { [Arg in GenerateNamespacedKey<KeyOfArgs<RemovedIndex<A>>, typeof ARG_PREFIX>]: string } & {
|
|
332
324
|
[key: string]: string;
|
|
333
|
-
};
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
type CommandResourceFetcher<
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
type TranslationAdapterFactory = (options: TranslationAdapterFactoryOptions) => TranslationAdapter;
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
325
|
+
};
|
|
326
|
+
/**
|
|
327
|
+
* Command resource fetcher.
|
|
328
|
+
* @param ctx A {@link CommandContext | command context}
|
|
329
|
+
* @returns A fetched {@link CommandResource | command resource}.
|
|
330
|
+
*/
|
|
331
|
+
type CommandResourceFetcher<A extends Args = Args, V = ArgValues<A>> = (ctx: Readonly<CommandContext<A, V>>) => Promise<CommandResource<A>>;
|
|
332
|
+
/**
|
|
333
|
+
* Translation adapter factory.
|
|
334
|
+
*/
|
|
335
|
+
type TranslationAdapterFactory = (options: TranslationAdapterFactoryOptions) => TranslationAdapter;
|
|
336
|
+
/**
|
|
337
|
+
* Translation adapter factory options.
|
|
338
|
+
*/
|
|
347
339
|
interface TranslationAdapterFactoryOptions {
|
|
348
340
|
/**
|
|
349
341
|
* A locale.
|
|
@@ -353,12 +345,12 @@ interface TranslationAdapterFactoryOptions {
|
|
|
353
345
|
* A fallback locale.
|
|
354
346
|
*/
|
|
355
347
|
fallbackLocale: string;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Translation adapter.
|
|
351
|
+
* This adapter is used to custom message formatter like {@link https://github.com/intlify/vue-i18n/blob/master/spec/syntax.ebnf | Intlify message format}, {@link https://github.com/tc39/proposal-intl-messageformat | `Intl.MessageFormat` (MF2)}, and etc.
|
|
352
|
+
* This adapter will support localization with your preferred message format.
|
|
353
|
+
*/
|
|
362
354
|
interface TranslationAdapter<MessageResource = string> {
|
|
363
355
|
/**
|
|
364
356
|
* Get a resource of locale.
|
|
@@ -387,29 +379,29 @@ interface TranslationAdapter<MessageResource = string> {
|
|
|
387
379
|
* @returns A translated message, if message is not translated, return `undefined`.
|
|
388
380
|
*/
|
|
389
381
|
translate(locale: string, key: string, values?: Record<string, unknown>): string | undefined;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
type CommandRunner<
|
|
396
|
-
type CommandLoader<
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
type LazyCommand<
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Command runner.
|
|
385
|
+
* @param ctx A {@link CommandContext | command context}
|
|
386
|
+
*/
|
|
387
|
+
type CommandRunner<A extends Args = Args> = (ctx: Readonly<CommandContext<A>>) => Awaitable<void>;
|
|
388
|
+
type CommandLoader<A extends Args = Args> = () => Awaitable<Command<A> | CommandRunner<A>>;
|
|
389
|
+
/**
|
|
390
|
+
* Lazy command interface.
|
|
391
|
+
* Lazy command that's not loaded until it is executed.
|
|
392
|
+
*/
|
|
393
|
+
type LazyCommand<A extends Args = Args> = {
|
|
402
394
|
/**
|
|
403
395
|
* Command load function
|
|
404
396
|
*/
|
|
405
|
-
(): Awaitable<Command<
|
|
397
|
+
(): Awaitable<Command<A> | CommandRunner<A>>;
|
|
406
398
|
/**
|
|
407
399
|
* Command name
|
|
408
400
|
*/
|
|
409
401
|
commandName?: string;
|
|
410
|
-
} & Omit<Command<
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
type Commandable<
|
|
415
|
-
export { Command,
|
|
402
|
+
} & Omit<Command<A>, 'run' | 'name'>;
|
|
403
|
+
/**
|
|
404
|
+
* Define a command type.
|
|
405
|
+
*/
|
|
406
|
+
type Commandable<A extends Args> = Command<A> | LazyCommand<A>; //#endregion
|
|
407
|
+
export { Command, CommandArgKeys, CommandBuiltinArgsKeys, CommandBuiltinKeys, CommandBuiltinResourceKeys, CommandContext, CommandEnvironment, CommandLoader, CommandOptions, CommandResource, CommandResourceFetcher, CommandRunner, Commandable, DEFAULT_LOCALE, GenerateNamespacedKey, KeyOfArgs, LazyCommand, RemovedIndex, TranslationAdapter, TranslationAdapterFactory, TranslationAdapterFactoryOptions };
|
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
*/
|
|
5
5
|
const DEFAULT_LOCALE = "en-US";
|
|
6
6
|
const BUILT_IN_PREFIX = "_";
|
|
7
|
-
const
|
|
7
|
+
const ARG_PREFIX = "arg";
|
|
8
8
|
const BUILT_IN_KEY_SEPARATOR = ":";
|
|
9
9
|
const NOOP = () => {};
|
|
10
|
-
const
|
|
10
|
+
const COMMON_ARGS = {
|
|
11
11
|
help: {
|
|
12
12
|
type: "boolean",
|
|
13
13
|
short: "h",
|
|
@@ -44,7 +44,7 @@ async function resolveLazyCommand(cmd, name, needRunResolving = false) {
|
|
|
44
44
|
command = Object.assign(create(), {
|
|
45
45
|
name: cmd.commandName,
|
|
46
46
|
description: cmd.description,
|
|
47
|
-
|
|
47
|
+
args: cmd.args,
|
|
48
48
|
examples: cmd.examples,
|
|
49
49
|
resource: cmd.resource
|
|
50
50
|
});
|
|
@@ -56,7 +56,7 @@ async function resolveLazyCommand(cmd, name, needRunResolving = false) {
|
|
|
56
56
|
command.run = loaded.run;
|
|
57
57
|
command.name = loaded.name;
|
|
58
58
|
command.description = loaded.description;
|
|
59
|
-
command.
|
|
59
|
+
command.args = loaded.args;
|
|
60
60
|
command.examples = loaded.examples;
|
|
61
61
|
command.resource = loaded.resource;
|
|
62
62
|
} else throw new TypeError(`Cannot resolve command: ${cmd.name || name}`);
|
|
@@ -68,8 +68,8 @@ async function resolveLazyCommand(cmd, name, needRunResolving = false) {
|
|
|
68
68
|
function resolveBuiltInKey(key) {
|
|
69
69
|
return `${BUILT_IN_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
|
|
70
70
|
}
|
|
71
|
-
function
|
|
72
|
-
return `${
|
|
71
|
+
function resolveArgKey(key) {
|
|
72
|
+
return `${ARG_PREFIX}${BUILT_IN_KEY_SEPARATOR}${key}`;
|
|
73
73
|
}
|
|
74
74
|
function mapResourceWithBuiltinKey(resource) {
|
|
75
75
|
return Object.entries(resource).reduce((acc, [key, value]) => {
|
|
@@ -93,4 +93,4 @@ function deepFreeze(obj) {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
//#endregion
|
|
96
|
-
export { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT,
|
|
96
|
+
export { BUILT_IN_PREFIX, COMMAND_OPTIONS_DEFAULT, COMMON_ARGS, DEFAULT_LOCALE as DEFAULT_LOCALE$1, NOOP, create, deepFreeze, log, mapResourceWithBuiltinKey, resolveArgKey, resolveBuiltInKey, resolveLazyCommand };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gunshi",
|
|
3
3
|
"description": "Modern javascript command-line library",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.19.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
"node": ">= 20"
|
|
31
31
|
},
|
|
32
32
|
"type": "module",
|
|
33
|
+
"sideEffects": false,
|
|
33
34
|
"files": [
|
|
34
35
|
"lib"
|
|
35
36
|
],
|
|
@@ -77,7 +78,7 @@
|
|
|
77
78
|
}
|
|
78
79
|
},
|
|
79
80
|
"dependencies": {
|
|
80
|
-
"args-tokens": "^0.
|
|
81
|
+
"args-tokens": "^0.17.1"
|
|
81
82
|
},
|
|
82
83
|
"devDependencies": {
|
|
83
84
|
"@eslint/markdown": "^6.4.0",
|
|
@@ -85,10 +86,10 @@
|
|
|
85
86
|
"@kazupon/eslint-config": "^0.29.0",
|
|
86
87
|
"@kazupon/prettier-config": "^0.1.1",
|
|
87
88
|
"@types/node": "^22.15.3",
|
|
88
|
-
"@vitest/eslint-plugin": "^1.1.
|
|
89
|
+
"@vitest/eslint-plugin": "^1.1.44",
|
|
89
90
|
"bumpp": "^10.1.0",
|
|
90
|
-
"deno": "^2.
|
|
91
|
-
"eslint": "^9.
|
|
91
|
+
"deno": "^2.3.1",
|
|
92
|
+
"eslint": "^9.26.0",
|
|
92
93
|
"eslint-config-prettier": "^10.1.2",
|
|
93
94
|
"eslint-import-resolver-typescript": "^4.3.4",
|
|
94
95
|
"eslint-plugin-import": "^2.31.0",
|
|
@@ -98,27 +99,27 @@
|
|
|
98
99
|
"eslint-plugin-regexp": "^2.7.0",
|
|
99
100
|
"eslint-plugin-unicorn": "^58.0.0",
|
|
100
101
|
"eslint-plugin-unused-imports": "^4.1.4",
|
|
101
|
-
"eslint-plugin-vue": "^10.0
|
|
102
|
+
"eslint-plugin-vue": "^10.1.0",
|
|
102
103
|
"eslint-plugin-vue-composable": "^1.0.0",
|
|
103
104
|
"eslint-plugin-yml": "^1.18.0",
|
|
104
105
|
"gh-changelogen": "^0.2.8",
|
|
105
106
|
"jsr": "^0.13.4",
|
|
106
107
|
"jsr-exports-lint": "^0.2.0",
|
|
107
|
-
"knip": "^5.
|
|
108
|
+
"knip": "^5.53.0",
|
|
108
109
|
"lint-staged": "^15.5.1",
|
|
109
110
|
"messageformat": "4.0.0-10",
|
|
110
111
|
"pkg-pr-new": "^0.0.43",
|
|
111
112
|
"prettier": "^3.5.3",
|
|
112
113
|
"publint": "^0.3.12",
|
|
113
|
-
"tsdown": "^0.10.
|
|
114
|
-
"typedoc": "^0.28.
|
|
114
|
+
"tsdown": "^0.10.2",
|
|
115
|
+
"typedoc": "^0.28.4",
|
|
115
116
|
"typedoc-plugin-markdown": "^4.6.3",
|
|
116
117
|
"typedoc-vitepress-theme": "^1.1.2",
|
|
117
118
|
"typescript": "^5.8.3",
|
|
118
119
|
"typescript-eslint": "^8.31.1",
|
|
119
120
|
"vitepress": "^1.6.3",
|
|
120
121
|
"vitepress-plugin-group-icons": "^1.5.2",
|
|
121
|
-
"vitepress-plugin-llms": "^1.1.
|
|
122
|
+
"vitepress-plugin-llms": "^1.1.3",
|
|
122
123
|
"vitest": "^3.1.2",
|
|
123
124
|
"vue": "^3.5.13"
|
|
124
125
|
},
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { Command, CommandLoader, LazyCommand } from "./types.d-BqXvgR9J.js";
|
|
2
|
-
import { ArgOptionSchema, ArgOptions, ArgOptions as ArgOptions$1, ArgValues as ArgValues$1 } from "args-tokens";
|
|
3
|
-
|
|
4
|
-
//#region src/definition.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Define a {@link Command | command} with type inference
|
|
7
|
-
* @param definition A {@link Command | command} definition
|
|
8
|
-
* @returns A {@link Command | command} definition with type inference
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
declare function define<Options extends ArgOptions = ArgOptions>(definition: Command<Options>): Command<Options>; /**
|
|
12
|
-
* Define a {@link LazyCommand | lazy command} with command loader, which is attached with command definition as usage metadata.
|
|
13
|
-
* @param loader A {@link CommandLoader | command loader}
|
|
14
|
-
* @param definition A {@link Command | command} definition
|
|
15
|
-
* @returns A {@link LazyCommand | lazy command} loader
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
declare function lazy<Options extends ArgOptions = ArgOptions>(loader: CommandLoader<Options>, definition?: Command<Options>): LazyCommand<Options>;
|
|
19
|
-
|
|
20
|
-
//#endregion
|
|
21
|
-
export { ArgOptionSchema, ArgOptions$1 as ArgOptions, ArgValues$1 as ArgValues, define as define$1, lazy as lazy$1 };
|