zod-args-parser 1.2.7 → 1.2.8
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 +13 -13
- package/lib/commonjs/autocomplete-scripts/{bash-autocomplete-script.js → bash-autocomplete-script.cjs} +2 -2
- package/lib/commonjs/autocomplete-scripts/bash-autocomplete-script.cjs.map +1 -0
- package/lib/commonjs/autocomplete-scripts/{powershell-autocomplete-script.js → powershell-autocomplete-script.cjs} +3 -3
- package/lib/commonjs/autocomplete-scripts/powershell-autocomplete-script.cjs.map +1 -0
- package/lib/commonjs/autocomplete-scripts/{zsh-autocomplete-script.js → zsh-autocomplete-script.cjs} +4 -4
- package/lib/commonjs/autocomplete-scripts/zsh-autocomplete-script.cjs.map +1 -0
- package/lib/commonjs/help-message/format-arguments.cjs +1 -0
- package/lib/commonjs/help-message/format-arguments.cjs.map +1 -0
- package/lib/commonjs/help-message/format-cli.cjs +1 -0
- package/lib/commonjs/help-message/format-cli.cjs.map +1 -0
- package/lib/commonjs/help-message/format-options.cjs +1 -0
- package/lib/commonjs/help-message/format-options.cjs.map +1 -0
- package/lib/commonjs/help-message/format-subcommands.cjs +1 -0
- package/lib/commonjs/help-message/format-subcommands.cjs.map +1 -0
- package/lib/commonjs/help-message/styles.cjs +1 -0
- package/lib/commonjs/help-message/styles.cjs.map +1 -0
- package/lib/commonjs/index.cjs +1 -0
- package/lib/commonjs/index.cjs.map +1 -0
- package/lib/commonjs/markdown/generate-markdown.cjs +1 -0
- package/lib/commonjs/markdown/generate-markdown.cjs.map +1 -0
- package/lib/commonjs/metadata/get-arguments-metadata.cjs +1 -0
- package/lib/commonjs/metadata/get-arguments-metadata.cjs.map +1 -0
- package/lib/commonjs/metadata/get-cli-metadata.cjs +1 -0
- package/lib/commonjs/metadata/{get-cli-metadata.js.map → get-cli-metadata.cjs.map} +1 -1
- package/lib/commonjs/metadata/get-options-metadata.cjs +1 -0
- package/lib/commonjs/metadata/get-options-metadata.cjs.map +1 -0
- package/lib/commonjs/metadata/get-subcommands-metadata.cjs +1 -0
- package/lib/commonjs/metadata/get-subcommands-metadata.cjs.map +1 -0
- package/lib/commonjs/parser/parse/parse.cjs +1 -0
- package/lib/commonjs/parser/parse/parse.cjs.map +1 -0
- package/lib/commonjs/parser/parse/parser-helpers.cjs +1 -0
- package/lib/commonjs/parser/parse/parser-helpers.cjs.map +1 -0
- package/lib/commonjs/parser/safe-parse.cjs +1 -0
- package/lib/commonjs/parser/safe-parse.cjs.map +1 -0
- package/lib/commonjs/parser/unsafe-parse.cjs +1 -0
- package/lib/commonjs/parser/unsafe-parse.cjs.map +1 -0
- package/lib/commonjs/parser/validate/validate-type.cjs.map +1 -0
- package/lib/commonjs/parser/validate/validate.cjs +1 -0
- package/lib/commonjs/parser/validate/validate.cjs.map +1 -0
- package/lib/commonjs/utilities.cjs +1 -0
- package/lib/commonjs/utilities.cjs.map +1 -0
- package/lib/commonjs/zod-utilities.cjs +1 -0
- package/lib/commonjs/zod-utilities.cjs.map +1 -0
- package/lib/module/autocomplete-scripts/{bash-autocomplete-script.js → bash-autocomplete-script.mjs} +2 -2
- package/lib/module/autocomplete-scripts/bash-autocomplete-script.mjs.map +1 -0
- package/lib/module/autocomplete-scripts/powershell-autocomplete-script.mjs +31 -0
- package/lib/module/autocomplete-scripts/powershell-autocomplete-script.mjs.map +1 -0
- package/lib/module/autocomplete-scripts/{zsh-autocomplete-script.js → zsh-autocomplete-script.mjs} +4 -4
- package/lib/module/autocomplete-scripts/zsh-autocomplete-script.mjs.map +1 -0
- package/lib/module/help-message/format-arguments.mjs +1 -0
- package/lib/module/help-message/format-arguments.mjs.map +1 -0
- package/lib/module/help-message/format-cli.mjs +1 -0
- package/lib/module/help-message/format-cli.mjs.map +1 -0
- package/lib/module/help-message/format-options.mjs +1 -0
- package/lib/module/help-message/format-options.mjs.map +1 -0
- package/lib/module/help-message/format-subcommands.mjs +1 -0
- package/lib/module/help-message/format-subcommands.mjs.map +1 -0
- package/lib/module/help-message/styles.mjs +1 -0
- package/lib/module/help-message/styles.mjs.map +1 -0
- package/lib/module/index.mjs +1 -0
- package/lib/module/index.mjs.map +1 -0
- package/lib/module/markdown/generate-markdown.mjs +1 -0
- package/lib/module/markdown/generate-markdown.mjs.map +1 -0
- package/lib/module/metadata/get-arguments-metadata.mjs +1 -0
- package/lib/module/metadata/get-arguments-metadata.mjs.map +1 -0
- package/lib/module/metadata/get-cli-metadata.mjs +1 -0
- package/lib/module/metadata/{get-cli-metadata.js.map → get-cli-metadata.mjs.map} +1 -1
- package/lib/module/metadata/get-options-metadata.mjs +1 -0
- package/lib/module/metadata/get-options-metadata.mjs.map +1 -0
- package/lib/module/metadata/get-subcommands-metadata.mjs +1 -0
- package/lib/module/metadata/get-subcommands-metadata.mjs.map +1 -0
- package/lib/module/parser/parse/parse.mjs +1 -0
- package/lib/module/parser/parse/parse.mjs.map +1 -0
- package/lib/module/parser/parse/parser-helpers.mjs +1 -0
- package/lib/module/parser/parse/parser-helpers.mjs.map +1 -0
- package/lib/module/parser/safe-parse.mjs +1 -0
- package/lib/module/parser/safe-parse.mjs.map +1 -0
- package/lib/module/parser/unsafe-parse.mjs +1 -0
- package/lib/module/parser/unsafe-parse.mjs.map +1 -0
- package/lib/module/parser/validate/validate-type.mjs.map +1 -0
- package/lib/module/parser/validate/validate.mjs +1 -0
- package/lib/module/parser/validate/validate.mjs.map +1 -0
- package/lib/module/utilities.mjs +1 -0
- package/lib/module/utilities.mjs.map +1 -0
- package/lib/module/zod-utilities.mjs +1 -0
- package/lib/module/zod-utilities.mjs.map +1 -0
- package/lib/typescript/autocomplete-scripts/bash-autocomplete-script.d.ts +1 -1
- package/lib/typescript/autocomplete-scripts/bash-autocomplete-script.d.ts.map +1 -1
- package/lib/typescript/autocomplete-scripts/powershell-autocomplete-script.d.ts +1 -1
- package/lib/typescript/autocomplete-scripts/powershell-autocomplete-script.d.ts.map +1 -1
- package/lib/typescript/autocomplete-scripts/zsh-autocomplete-script.d.ts +1 -1
- package/lib/typescript/autocomplete-scripts/zsh-autocomplete-script.d.ts.map +1 -1
- package/lib/typescript/help-message/format-arguments.d.ts +2 -2
- package/lib/typescript/help-message/format-arguments.d.ts.map +1 -1
- package/lib/typescript/help-message/format-cli.d.ts +5 -5
- package/lib/typescript/help-message/format-cli.d.ts.map +1 -1
- package/lib/typescript/help-message/format-options.d.ts +2 -2
- package/lib/typescript/help-message/format-options.d.ts.map +1 -1
- package/lib/typescript/help-message/format-subcommands.d.ts +2 -2
- package/lib/typescript/help-message/format-subcommands.d.ts.map +1 -1
- package/lib/typescript/help-message/styles.d.ts +14 -13
- package/lib/typescript/help-message/styles.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +10 -10
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/markdown/generate-markdown.d.ts +1 -1
- package/lib/typescript/markdown/generate-markdown.d.ts.map +1 -1
- package/lib/typescript/metadata/get-arguments-metadata.d.ts +1 -1
- package/lib/typescript/metadata/get-arguments-metadata.d.ts.map +1 -1
- package/lib/typescript/metadata/get-options-metadata.d.ts.map +1 -1
- package/lib/typescript/metadata/get-subcommands-metadata.d.ts.map +1 -1
- package/lib/typescript/parser/parse/parse-types.d.ts +5 -5
- package/lib/typescript/parser/parse/parse-types.d.ts.map +1 -1
- package/lib/typescript/parser/parse/parse.d.ts +2 -2
- package/lib/typescript/parser/parse/parse.d.ts.map +1 -1
- package/lib/typescript/parser/parse/parser-helpers.d.ts +7 -7
- package/lib/typescript/parser/parse/parser-helpers.d.ts.map +1 -1
- package/lib/typescript/parser/safe-parse.d.ts +2 -2
- package/lib/typescript/parser/safe-parse.d.ts.map +1 -1
- package/lib/typescript/parser/unsafe-parse.d.ts +2 -2
- package/lib/typescript/parser/unsafe-parse.d.ts.map +1 -1
- package/lib/typescript/parser/validate/validate-type.d.ts +4 -4
- package/lib/typescript/parser/validate/validate-type.d.ts.map +1 -1
- package/lib/typescript/parser/validate/validate.d.ts +4 -4
- package/lib/typescript/parser/validate/validate.d.ts.map +1 -1
- package/lib/typescript/types.d.ts +19 -15
- package/lib/typescript/types.d.ts.map +1 -1
- package/lib/typescript/{utils.d.ts → utilities.d.ts} +9 -9
- package/lib/typescript/utilities.d.ts.map +1 -0
- package/lib/typescript/{zod-utils.d.ts → zod-utilities.d.ts} +5 -4
- package/lib/typescript/zod-utilities.d.ts.map +1 -0
- package/package.json +17 -18
- package/src/autocomplete-scripts/bash-autocomplete-script.ts +10 -10
- package/src/autocomplete-scripts/powershell-autocomplete-script.ts +17 -15
- package/src/autocomplete-scripts/zsh-autocomplete-script.ts +8 -5
- package/src/help-message/format-arguments.ts +20 -13
- package/src/help-message/format-cli.ts +65 -45
- package/src/help-message/format-options.ts +19 -12
- package/src/help-message/format-subcommands.ts +9 -9
- package/src/help-message/styles.ts +18 -18
- package/src/index.ts +21 -21
- package/src/markdown/generate-markdown.ts +55 -55
- package/src/metadata/get-arguments-metadata.ts +10 -10
- package/src/metadata/get-options-metadata.ts +8 -6
- package/src/metadata/get-subcommands-metadata.ts +3 -1
- package/src/parser/parse/parse-types.ts +5 -5
- package/src/parser/parse/parse.ts +67 -64
- package/src/parser/parse/parser-helpers.ts +24 -25
- package/src/parser/safe-parse.ts +24 -18
- package/src/parser/unsafe-parse.ts +34 -34
- package/src/parser/validate/validate-type.ts +4 -4
- package/src/parser/validate/validate.ts +17 -17
- package/src/types.ts +21 -17
- package/src/{utils.ts → utilities.ts} +44 -28
- package/src/{zod-utils.ts → zod-utilities.ts} +56 -32
- package/lib/commonjs/autocomplete-scripts/bash-autocomplete-script.js.map +0 -1
- package/lib/commonjs/autocomplete-scripts/powershell-autocomplete-script.js.map +0 -1
- package/lib/commonjs/autocomplete-scripts/zsh-autocomplete-script.js.map +0 -1
- package/lib/commonjs/help-message/format-arguments.js +0 -1
- package/lib/commonjs/help-message/format-arguments.js.map +0 -1
- package/lib/commonjs/help-message/format-cli.js +0 -1
- package/lib/commonjs/help-message/format-cli.js.map +0 -1
- package/lib/commonjs/help-message/format-options.js +0 -1
- package/lib/commonjs/help-message/format-options.js.map +0 -1
- package/lib/commonjs/help-message/format-subcommands.js +0 -1
- package/lib/commonjs/help-message/format-subcommands.js.map +0 -1
- package/lib/commonjs/help-message/styles.js +0 -1
- package/lib/commonjs/help-message/styles.js.map +0 -1
- package/lib/commonjs/index.js +0 -1
- package/lib/commonjs/index.js.map +0 -1
- package/lib/commonjs/markdown/generate-markdown.js +0 -1
- package/lib/commonjs/markdown/generate-markdown.js.map +0 -1
- package/lib/commonjs/metadata/get-arguments-metadata.js +0 -1
- package/lib/commonjs/metadata/get-arguments-metadata.js.map +0 -1
- package/lib/commonjs/metadata/get-cli-metadata.js +0 -1
- package/lib/commonjs/metadata/get-options-metadata.js +0 -1
- package/lib/commonjs/metadata/get-options-metadata.js.map +0 -1
- package/lib/commonjs/metadata/get-subcommands-metadata.js +0 -1
- package/lib/commonjs/metadata/get-subcommands-metadata.js.map +0 -1
- package/lib/commonjs/parser/parse/parse.js +0 -1
- package/lib/commonjs/parser/parse/parse.js.map +0 -1
- package/lib/commonjs/parser/parse/parser-helpers.js +0 -1
- package/lib/commonjs/parser/parse/parser-helpers.js.map +0 -1
- package/lib/commonjs/parser/safe-parse.js +0 -1
- package/lib/commonjs/parser/safe-parse.js.map +0 -1
- package/lib/commonjs/parser/unsafe-parse.js +0 -1
- package/lib/commonjs/parser/unsafe-parse.js.map +0 -1
- package/lib/commonjs/parser/validate/validate-type.js.map +0 -1
- package/lib/commonjs/parser/validate/validate.js +0 -1
- package/lib/commonjs/parser/validate/validate.js.map +0 -1
- package/lib/commonjs/utils.js +0 -1
- package/lib/commonjs/utils.js.map +0 -1
- package/lib/commonjs/zod-utils.js +0 -1
- package/lib/commonjs/zod-utils.js.map +0 -1
- package/lib/module/autocomplete-scripts/bash-autocomplete-script.js.map +0 -1
- package/lib/module/autocomplete-scripts/powershell-autocomplete-script.js +0 -31
- package/lib/module/autocomplete-scripts/powershell-autocomplete-script.js.map +0 -1
- package/lib/module/autocomplete-scripts/zsh-autocomplete-script.js.map +0 -1
- package/lib/module/help-message/format-arguments.js +0 -1
- package/lib/module/help-message/format-arguments.js.map +0 -1
- package/lib/module/help-message/format-cli.js +0 -1
- package/lib/module/help-message/format-cli.js.map +0 -1
- package/lib/module/help-message/format-options.js +0 -1
- package/lib/module/help-message/format-options.js.map +0 -1
- package/lib/module/help-message/format-subcommands.js +0 -1
- package/lib/module/help-message/format-subcommands.js.map +0 -1
- package/lib/module/help-message/styles.js +0 -1
- package/lib/module/help-message/styles.js.map +0 -1
- package/lib/module/index.js +0 -1
- package/lib/module/index.js.map +0 -1
- package/lib/module/markdown/generate-markdown.js +0 -1
- package/lib/module/markdown/generate-markdown.js.map +0 -1
- package/lib/module/metadata/get-arguments-metadata.js +0 -1
- package/lib/module/metadata/get-arguments-metadata.js.map +0 -1
- package/lib/module/metadata/get-cli-metadata.js +0 -1
- package/lib/module/metadata/get-options-metadata.js +0 -1
- package/lib/module/metadata/get-options-metadata.js.map +0 -1
- package/lib/module/metadata/get-subcommands-metadata.js +0 -1
- package/lib/module/metadata/get-subcommands-metadata.js.map +0 -1
- package/lib/module/parser/parse/parse.js +0 -1
- package/lib/module/parser/parse/parse.js.map +0 -1
- package/lib/module/parser/parse/parser-helpers.js +0 -1
- package/lib/module/parser/parse/parser-helpers.js.map +0 -1
- package/lib/module/parser/safe-parse.js +0 -1
- package/lib/module/parser/safe-parse.js.map +0 -1
- package/lib/module/parser/unsafe-parse.js +0 -1
- package/lib/module/parser/unsafe-parse.js.map +0 -1
- package/lib/module/parser/validate/validate-type.js.map +0 -1
- package/lib/module/parser/validate/validate.js +0 -1
- package/lib/module/parser/validate/validate.js.map +0 -1
- package/lib/module/utils.js +0 -1
- package/lib/module/utils.js.map +0 -1
- package/lib/module/zod-utils.js +0 -1
- package/lib/module/zod-utils.js.map +0 -1
- package/lib/typescript/utils.d.ts.map +0 -1
- package/lib/typescript/zod-utils.d.ts.map +0 -1
- /package/lib/commonjs/parser/validate/{validate-type.js → validate-type.cjs} +0 -0
- /package/lib/module/parser/validate/{validate-type.js → validate-type.mjs} +0 -0
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
import { getCliMetadata } from "../metadata/get-cli-metadata.js";
|
|
2
|
-
import { concat, indent, ln, subcommandPlaceholder } from "../
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
import type { Cli,
|
|
9
|
-
|
|
10
|
-
export function
|
|
11
|
-
|
|
2
|
+
import { concat, indent, ln, subcommandPlaceholder } from "../utilities.js";
|
|
3
|
+
import { formatHelpMessageArguments } from "./format-arguments.js";
|
|
4
|
+
import { formatHelpMessageOptions } from "./format-options.js";
|
|
5
|
+
import { formatHelpMessageCommands } from "./format-subcommands.js";
|
|
6
|
+
import { helpMessageStyles } from "./styles.js";
|
|
7
|
+
|
|
8
|
+
import type { Cli, HelpMessageStyle, Subcommand } from "../types.js";
|
|
9
|
+
|
|
10
|
+
export function formatCliHelpMessage(
|
|
11
|
+
parameters: readonly [Cli, ...Subcommand[]],
|
|
12
|
+
style?: Partial<HelpMessageStyle>,
|
|
13
|
+
): string {
|
|
14
|
+
const c = helpMessageStyles.default;
|
|
12
15
|
if (style) Object.assign(c, style);
|
|
13
16
|
|
|
14
|
-
const metadata = getCliMetadata(
|
|
17
|
+
const metadata = getCliMetadata(parameters);
|
|
15
18
|
|
|
16
19
|
const formatTitle = (title: string) => c.title(` ${title.toUpperCase()}`);
|
|
17
20
|
|
|
18
|
-
let
|
|
21
|
+
let message = "";
|
|
19
22
|
|
|
20
23
|
// CLI usage
|
|
21
24
|
const usage =
|
|
@@ -23,18 +26,18 @@ export function formatCliHelpMsg(params: readonly [Cli, ...Subcommand[]], style?
|
|
|
23
26
|
concat(
|
|
24
27
|
c.punctuation("$"),
|
|
25
28
|
c.description(metadata.name),
|
|
26
|
-
metadata.subcommands.length ? c.command("[command]") : "",
|
|
27
|
-
metadata.options.length ? c.option("[options]") : "",
|
|
28
|
-
metadata.arguments.length ? c.argument("<arguments>") : "",
|
|
29
|
+
metadata.subcommands.length > 0 ? c.command("[command]") : "",
|
|
30
|
+
metadata.options.length > 0 ? c.option("[options]") : "",
|
|
31
|
+
metadata.arguments.length > 0 ? c.argument("<arguments>") : "",
|
|
29
32
|
metadata.allowPositional ? c.argument("<positionals>") : "",
|
|
30
33
|
);
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
message += formatTitle("Usage") + ln(1);
|
|
35
|
+
message += indent(2) + usage + ln(2);
|
|
33
36
|
|
|
34
37
|
// CLI description
|
|
35
38
|
if (metadata.description) {
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
message += formatTitle("Description") + ln(1);
|
|
40
|
+
message += indent(2) + c.description(metadata.description).replace(/\n+/g, "\n" + indent(2)) + ln(2);
|
|
38
41
|
}
|
|
39
42
|
|
|
40
43
|
let longest = 0;
|
|
@@ -42,11 +45,14 @@ export function formatCliHelpMsg(params: readonly [Cli, ...Subcommand[]], style?
|
|
|
42
45
|
// Prepare CLI options
|
|
43
46
|
const optionsMetadata = metadata.options;
|
|
44
47
|
|
|
45
|
-
|
|
46
|
-
|
|
48
|
+
let longestOptionTitle = 0;
|
|
49
|
+
for (const metadata of optionsMetadata) {
|
|
50
|
+
const names = [...metadata.aliasesAsArgs, metadata.nameAsArg].join(", ");
|
|
47
51
|
const optLength = names.length + metadata.placeholder.length;
|
|
48
|
-
|
|
49
|
-
|
|
52
|
+
if (optLength > longestOptionTitle) {
|
|
53
|
+
longestOptionTitle = optLength;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
50
56
|
|
|
51
57
|
if (longestOptionTitle > longest) {
|
|
52
58
|
longest = longestOptionTitle;
|
|
@@ -55,46 +61,60 @@ export function formatCliHelpMsg(params: readonly [Cli, ...Subcommand[]], style?
|
|
|
55
61
|
// Prepare CLI commands
|
|
56
62
|
const subcommandsMetadata = metadata.subcommands;
|
|
57
63
|
|
|
58
|
-
|
|
59
|
-
|
|
64
|
+
let longestSubcommandTitle = 0;
|
|
65
|
+
for (const metadata of subcommandsMetadata) {
|
|
66
|
+
const names = [...metadata.aliases, metadata.name].join(", ");
|
|
60
67
|
const placeholder = subcommandPlaceholder(metadata);
|
|
61
68
|
const optLength = names.length + placeholder.length;
|
|
62
|
-
|
|
63
|
-
|
|
69
|
+
if (optLength > longestSubcommandTitle) {
|
|
70
|
+
longestSubcommandTitle = optLength;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
64
73
|
|
|
65
74
|
if (longestSubcommandTitle > longest) {
|
|
66
75
|
longest = longestSubcommandTitle;
|
|
67
76
|
}
|
|
68
77
|
|
|
69
78
|
// Prepare CLI arguments
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
79
|
+
const argumentsMetadata = metadata.arguments;
|
|
80
|
+
|
|
81
|
+
let longestArgumentTitle = 0;
|
|
82
|
+
for (const argument of argumentsMetadata) {
|
|
83
|
+
if (argument.name.length > longestArgumentTitle) {
|
|
84
|
+
longestArgumentTitle = argument.name.length;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (longestArgumentTitle > longest) {
|
|
89
|
+
longest = longestArgumentTitle;
|
|
74
90
|
}
|
|
75
91
|
|
|
76
92
|
// CLI options
|
|
77
|
-
|
|
93
|
+
message += formatHelpMessageOptions(optionsMetadata, c, longest);
|
|
78
94
|
|
|
79
95
|
// CLI commands
|
|
80
|
-
|
|
96
|
+
message += formatHelpMessageCommands(subcommandsMetadata, c, longest);
|
|
81
97
|
|
|
82
98
|
// CLI arguments
|
|
83
|
-
|
|
99
|
+
message += formatHelpMessageArguments(argumentsMetadata, c, longest);
|
|
84
100
|
|
|
85
101
|
// CLI example
|
|
86
102
|
if (metadata.example) {
|
|
87
|
-
|
|
88
|
-
|
|
103
|
+
message += formatTitle("Example");
|
|
104
|
+
message += ln(1);
|
|
89
105
|
const normalizeExample = metadata.example.replace(/\n+/g, "\n" + indent(3));
|
|
90
|
-
|
|
106
|
+
message += concat(indent(2), c.example(normalizeExample), ln(2));
|
|
91
107
|
}
|
|
92
108
|
|
|
93
|
-
return
|
|
109
|
+
return message;
|
|
94
110
|
}
|
|
95
111
|
|
|
96
|
-
export function
|
|
97
|
-
|
|
112
|
+
export function formatSubcommandHelpMessage(
|
|
113
|
+
subcommand: Subcommand,
|
|
114
|
+
printStyle?: Partial<HelpMessageStyle>,
|
|
115
|
+
cliName = "",
|
|
116
|
+
) {
|
|
117
|
+
const c = helpMessageStyles.default;
|
|
98
118
|
if (printStyle) Object.assign(c, printStyle);
|
|
99
119
|
|
|
100
120
|
const usage =
|
|
@@ -109,13 +129,13 @@ export function formatSubcommandHelpMsg(subcommand: Subcommand, printStyle?: Par
|
|
|
109
129
|
|
|
110
130
|
const asCli: Cli = { cliName, usage, ...subcommand };
|
|
111
131
|
|
|
112
|
-
return
|
|
132
|
+
return formatCliHelpMessage([asCli], c);
|
|
113
133
|
}
|
|
114
134
|
|
|
115
|
-
export function printCliHelp(
|
|
116
|
-
console.log(
|
|
135
|
+
export function printCliHelp(parameters: readonly [Cli, ...Subcommand[]], style?: Partial<HelpMessageStyle>) {
|
|
136
|
+
console.log(formatCliHelpMessage(parameters, style));
|
|
117
137
|
}
|
|
118
138
|
|
|
119
|
-
export function printSubcommandHelp(subcommand: Subcommand, style?: Partial<
|
|
120
|
-
console.log(
|
|
139
|
+
export function printSubcommandHelp(subcommand: Subcommand, style?: Partial<HelpMessageStyle>, cliName = "") {
|
|
140
|
+
console.log(formatSubcommandHelpMessage(subcommand, style, cliName));
|
|
121
141
|
}
|
|
@@ -1,27 +1,34 @@
|
|
|
1
|
-
import { concat, indent, insertAtEndOfFirstLine, ln } from "../
|
|
1
|
+
import { concat, indent, insertAtEndOfFirstLine, ln } from "../utilities.js";
|
|
2
2
|
|
|
3
3
|
import type { OptionMetadata } from "../metadata/metadata-types.js";
|
|
4
|
-
import type {
|
|
4
|
+
import type { HelpMessageStyle } from "../types.js";
|
|
5
5
|
|
|
6
|
-
export function
|
|
7
|
-
|
|
6
|
+
export function formatHelpMessageOptions(
|
|
7
|
+
optionsMetadata: OptionMetadata[],
|
|
8
|
+
c: HelpMessageStyle,
|
|
9
|
+
longest: number,
|
|
10
|
+
): string {
|
|
11
|
+
if (optionsMetadata.length === 0) return "";
|
|
8
12
|
|
|
9
|
-
let
|
|
13
|
+
let message = c.title(" OPTIONS") + ln(1);
|
|
10
14
|
|
|
11
15
|
for (const metadata of optionsMetadata) {
|
|
12
16
|
const names = metadata.aliasesAsArgs.concat([metadata.nameAsArg]);
|
|
13
17
|
const normalizeDesc = metadata.description.replace(/\n+/g, "\n" + indent(longest + 6) + c.punctuation("└"));
|
|
14
|
-
const
|
|
15
|
-
typeof metadata.defaultValue !== "undefined" ? `(default: ${metadata.defaultValueAsString})` : "";
|
|
18
|
+
const defaultString = metadata.defaultValue === undefined ? "" : `(default: ${metadata.defaultValueAsString})`;
|
|
16
19
|
|
|
17
20
|
const optLength = names.join(", ").length + metadata.placeholder.length;
|
|
18
21
|
const spacing = longest + 1 - optLength;
|
|
19
22
|
|
|
20
23
|
const coloredNames = names.map(name => c.option(name)).join(c.punctuation(", "));
|
|
21
24
|
|
|
22
|
-
const defaultOrOptional =
|
|
25
|
+
const defaultOrOptional = defaultString
|
|
26
|
+
? c.default(defaultString)
|
|
27
|
+
: metadata.optional
|
|
28
|
+
? c.optional("(optional)")
|
|
29
|
+
: "";
|
|
23
30
|
|
|
24
|
-
|
|
31
|
+
message += concat(
|
|
25
32
|
indent(2) + coloredNames,
|
|
26
33
|
c.placeholder(metadata.placeholder),
|
|
27
34
|
indent(spacing),
|
|
@@ -31,14 +38,14 @@ export function formatHelpMsgOptions(optionsMetadata: OptionMetadata[], c: HelpM
|
|
|
31
38
|
|
|
32
39
|
if (metadata.example) {
|
|
33
40
|
const normalizeExample = metadata.example.replace(/\n+/g, "\n" + indent(longest + 16));
|
|
34
|
-
|
|
41
|
+
message += concat(
|
|
35
42
|
indent(longest + 6) + c.punctuation("└") + c.exampleTitle("Example:"),
|
|
36
43
|
c.example(normalizeExample) + ln(1),
|
|
37
44
|
);
|
|
38
45
|
}
|
|
39
46
|
}
|
|
40
47
|
|
|
41
|
-
|
|
48
|
+
message += ln(1);
|
|
42
49
|
|
|
43
|
-
return
|
|
50
|
+
return message;
|
|
44
51
|
}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { concat, indent, ln, subcommandPlaceholder } from "../
|
|
1
|
+
import { concat, indent, ln, subcommandPlaceholder } from "../utilities.js";
|
|
2
2
|
|
|
3
3
|
import type { SubcommandMetadata } from "../metadata/metadata-types.js";
|
|
4
|
-
import type {
|
|
4
|
+
import type { HelpMessageStyle } from "../types.js";
|
|
5
5
|
|
|
6
|
-
export function
|
|
6
|
+
export function formatHelpMessageCommands(
|
|
7
7
|
subcommandsMetadata: SubcommandMetadata[],
|
|
8
|
-
c:
|
|
8
|
+
c: HelpMessageStyle,
|
|
9
9
|
longest: number,
|
|
10
10
|
): string {
|
|
11
|
-
if (
|
|
11
|
+
if (subcommandsMetadata.length === 0) return "";
|
|
12
12
|
|
|
13
|
-
let
|
|
13
|
+
let message = c.title(" COMMANDS") + ln(1);
|
|
14
14
|
|
|
15
15
|
for (const metadata of subcommandsMetadata) {
|
|
16
16
|
const names = metadata.aliases.concat([metadata.name]);
|
|
@@ -22,7 +22,7 @@ export function formatHelpMsgCommands(
|
|
|
22
22
|
|
|
23
23
|
const coloredNames = names.map(name => c.command(name)).join(c.punctuation(", "));
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
message += concat(
|
|
26
26
|
indent(2) + coloredNames,
|
|
27
27
|
c.placeholder(placeholder),
|
|
28
28
|
indent(spacing),
|
|
@@ -30,7 +30,7 @@ export function formatHelpMsgCommands(
|
|
|
30
30
|
);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
message += ln(1);
|
|
34
34
|
|
|
35
|
-
return
|
|
35
|
+
return message;
|
|
36
36
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import chalk from "chalk";
|
|
2
|
-
import type {
|
|
2
|
+
import type { HelpMessageStyle } from "../types.js";
|
|
3
3
|
|
|
4
|
-
export const
|
|
4
|
+
export const helpMessageStyles = Object.freeze({
|
|
5
5
|
default: {
|
|
6
6
|
title: chalk.bold.blue,
|
|
7
7
|
description: chalk.white,
|
|
@@ -55,17 +55,17 @@ export const helpMsgStyles = Object.freeze({
|
|
|
55
55
|
punctuation: chalk.hex("#4C566A"),
|
|
56
56
|
},
|
|
57
57
|
html: {
|
|
58
|
-
title: (...
|
|
59
|
-
description: (...
|
|
60
|
-
default: (...
|
|
61
|
-
optional: (...
|
|
62
|
-
exampleTitle: (...
|
|
63
|
-
example: (...
|
|
64
|
-
command: (...
|
|
65
|
-
option: (...
|
|
66
|
-
argument: (...
|
|
67
|
-
placeholder: (...
|
|
68
|
-
punctuation: (...
|
|
58
|
+
title: (...string) => `<span style="color: #89dceb; font-weight: bold;">${escapeHTML(string.join(" "))}</span>`,
|
|
59
|
+
description: (...string) => `<span style="color: #cdd6e8;">${escapeHTML(string.join(" "))}</span>`,
|
|
60
|
+
default: (...string) => `<span style="color: #6c7086; font-style: italic;">${escapeHTML(string.join(" "))}</span>`,
|
|
61
|
+
optional: (...string) => `<span style="color: #6c7086; font-style: italic;">${escapeHTML(string.join(" "))}</span>`,
|
|
62
|
+
exampleTitle: (...string) => `<span style="color: #f9e2af;">${escapeHTML(string.join(" "))}</span>`,
|
|
63
|
+
example: (...string) => `<span style="color: #6c7086;">${escapeHTML(string.join(" "))}</span>`,
|
|
64
|
+
command: (...string) => `<span style="color: #f9e2af;">${escapeHTML(string.join(" "))}</span>`,
|
|
65
|
+
option: (...string) => `<span style="color: #17b85d;">${escapeHTML(string.join(" "))}</span>`,
|
|
66
|
+
argument: (...string) => `<span style="color: #00ff00;">${escapeHTML(string.join(" "))}</span>`,
|
|
67
|
+
placeholder: (...string) => `<span style="color: #db9518;">${escapeHTML(string.join(" "))}</span>`,
|
|
68
|
+
punctuation: (...string) => `<span style="color: #6c7086;">${escapeHTML(string.join(" "))}</span>`,
|
|
69
69
|
},
|
|
70
70
|
gruvboxDark: {
|
|
71
71
|
title: chalk.bold.hex("#FABD2F"),
|
|
@@ -109,12 +109,12 @@ export const helpMsgStyles = Object.freeze({
|
|
|
109
109
|
get noColors() {
|
|
110
110
|
return new Proxy(this.default, {
|
|
111
111
|
get() {
|
|
112
|
-
return (...
|
|
112
|
+
return (...string: string[]) => string.join(" ");
|
|
113
113
|
},
|
|
114
|
-
});
|
|
114
|
+
}) as HelpMessageStyle;
|
|
115
115
|
},
|
|
116
|
-
}) satisfies Record<string,
|
|
116
|
+
}) satisfies Record<string, HelpMessageStyle>;
|
|
117
117
|
|
|
118
|
-
function escapeHTML(
|
|
119
|
-
return
|
|
118
|
+
function escapeHTML(string: string) {
|
|
119
|
+
return string.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
120
120
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
2
|
+
ActionsFunctions,
|
|
3
3
|
Argument,
|
|
4
4
|
CheckArgumentsOptional,
|
|
5
5
|
CheckDuplicatedArguments,
|
|
@@ -17,45 +17,45 @@ import type {
|
|
|
17
17
|
* - Insures no optional arguments are allowed when `allowPositional` is enabled
|
|
18
18
|
*/
|
|
19
19
|
type CheckCliSubcommandInput<T extends Cli | Subcommand> =
|
|
20
|
-
CheckDuplicatedOptions<T> extends infer
|
|
21
|
-
?
|
|
22
|
-
: CheckDuplicatedArguments<T> extends infer
|
|
23
|
-
?
|
|
24
|
-
: CheckArgumentsOptional<T> extends infer
|
|
25
|
-
?
|
|
20
|
+
CheckDuplicatedOptions<T> extends infer Error extends string
|
|
21
|
+
? Error
|
|
22
|
+
: CheckDuplicatedArguments<T> extends infer Error extends string
|
|
23
|
+
? Error
|
|
24
|
+
: CheckArgumentsOptional<T> extends infer Error extends string
|
|
25
|
+
? Error
|
|
26
26
|
: T;
|
|
27
27
|
|
|
28
28
|
export function createCli<const T extends Cli>(input: CheckCliSubcommandInput<T>) {
|
|
29
|
-
const setAction = (action: (
|
|
29
|
+
const setAction = (action: (data: any) => any) => {
|
|
30
30
|
if (typeof input === "string") return;
|
|
31
31
|
input.action = action;
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
-
const setPreValidationHook = (hook: (
|
|
34
|
+
const setPreValidationHook = (hook: (context: any) => any) => {
|
|
35
35
|
if (typeof input === "string") return;
|
|
36
36
|
input.preValidation = hook;
|
|
37
37
|
};
|
|
38
38
|
|
|
39
|
-
return Object.assign(input, { setAction, setPreValidationHook }) as Prettify<typeof input &
|
|
39
|
+
return Object.assign(input, { setAction, setPreValidationHook }) as Prettify<typeof input & ActionsFunctions<T>>;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
export function createSubcommand<const T extends Subcommand>(input: CheckCliSubcommandInput<T>) {
|
|
43
|
-
const setAction = (action: (
|
|
43
|
+
const setAction = (action: (data: any) => any) => {
|
|
44
44
|
if (typeof input === "string") return;
|
|
45
45
|
input.action = action as T["action"];
|
|
46
46
|
};
|
|
47
47
|
|
|
48
|
-
const setPreValidationHook = (hook: (
|
|
48
|
+
const setPreValidationHook = (hook: (context: any) => any) => {
|
|
49
49
|
if (typeof input === "string") return;
|
|
50
50
|
input.preValidation = hook;
|
|
51
51
|
};
|
|
52
52
|
|
|
53
|
-
return Object.assign(input, { setAction, setPreValidationHook }) as Prettify<typeof input &
|
|
53
|
+
return Object.assign(input, { setAction, setPreValidationHook }) as Prettify<typeof input & ActionsFunctions<T>>;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
/** - Insures that there are no duplicated options */
|
|
57
57
|
type CheckOptionsInput<T extends Option[]> =
|
|
58
|
-
CheckDuplicatedOptions<{ options: T }> extends infer
|
|
58
|
+
CheckDuplicatedOptions<{ options: T }> extends infer Error extends string ? Error : T;
|
|
59
59
|
|
|
60
60
|
export function createOptions<const T extends [Option, ...Option[]]>(options: CheckOptionsInput<T>) {
|
|
61
61
|
return options;
|
|
@@ -63,24 +63,24 @@ export function createOptions<const T extends [Option, ...Option[]]>(options: Ch
|
|
|
63
63
|
|
|
64
64
|
/** - Insures that only the last argument is optional */
|
|
65
65
|
type CheckArgumentsInput<T extends Argument[]> =
|
|
66
|
-
CheckArgumentsOptional<{ arguments: T }> extends infer
|
|
66
|
+
CheckArgumentsOptional<{ arguments: T }> extends infer Error extends string ? Error : T;
|
|
67
67
|
|
|
68
|
-
export function createArguments<const T extends [Argument, ...Argument[]]>(
|
|
69
|
-
return
|
|
68
|
+
export function createArguments<const T extends [Argument, ...Argument[]]>(arguments_: CheckArgumentsInput<T>) {
|
|
69
|
+
return arguments_;
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
export {
|
|
73
|
-
formatCliHelpMsg,
|
|
74
|
-
formatSubcommandHelpMsg,
|
|
73
|
+
formatCliHelpMessage as formatCliHelpMsg,
|
|
74
|
+
formatSubcommandHelpMessage as formatSubcommandHelpMsg,
|
|
75
75
|
printCliHelp,
|
|
76
76
|
printSubcommandHelp,
|
|
77
77
|
} from "./help-message/format-cli.js";
|
|
78
|
-
export {
|
|
78
|
+
export { helpMessageStyles } from "./help-message/styles.js";
|
|
79
79
|
|
|
80
80
|
export { safeParse, safeParseAsync } from "./parser/safe-parse.js";
|
|
81
81
|
export { unsafeParse as parse, unsafeParseAsync as parseAsync } from "./parser/unsafe-parse.js";
|
|
82
82
|
|
|
83
|
-
export { isOptionalSchema, schemaDefaultValue, stringToArray, stringToSet } from "./zod-
|
|
83
|
+
export { isOptionalSchema, schemaDefaultValue, stringToArray, stringToSet } from "./zod-utilities.js";
|
|
84
84
|
|
|
85
85
|
export { generateBashAutocompleteScript } from "./autocomplete-scripts/bash-autocomplete-script.js";
|
|
86
86
|
export { generatePowerShellAutocompleteScript } from "./autocomplete-scripts/powershell-autocomplete-script.js";
|