talented-args 0.2.5-beta → 0.3.1-beta
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 +506 -14
- package/dist/clipboard.d.ts +10 -0
- package/dist/clipboard.js +79 -0
- package/dist/clipboard.js.map +1 -0
- package/dist/compile.d.ts +67 -0
- package/dist/compile.js +188 -0
- package/dist/compile.js.map +1 -0
- package/dist/descriptors.d.ts +322 -0
- package/dist/descriptors.js +362 -0
- package/dist/descriptors.js.map +1 -0
- package/dist/display.d.ts +43 -0
- package/dist/display.js +251 -0
- package/dist/display.js.map +1 -0
- package/dist/errors.d.ts +34 -17
- package/dist/errors.js +61 -75
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +10 -6
- package/dist/index.js +8 -9
- package/dist/index.js.map +1 -1
- package/dist/options.d.ts +69 -41
- package/dist/options.js +198 -31
- package/dist/options.js.map +1 -1
- package/dist/parse.d.ts +70 -0
- package/dist/parse.js +551 -0
- package/dist/parse.js.map +1 -0
- package/dist/program.d.ts +125 -0
- package/dist/program.js +337 -0
- package/dist/program.js.map +1 -0
- package/dist/remember.d.ts +71 -0
- package/dist/remember.js +216 -0
- package/dist/remember.js.map +1 -0
- package/dist/styling.d.ts +38 -9
- package/dist/styling.js +155 -75
- package/dist/styling.js.map +1 -1
- package/dist/terminal.d.ts +205 -0
- package/dist/terminal.js +152 -0
- package/dist/terminal.js.map +1 -0
- package/dist/tools.d.ts +35 -31
- package/dist/tools.js +68 -140
- package/dist/tools.js.map +1 -1
- package/dist/tui.d.ts +96 -0
- package/dist/tui.js +397 -0
- package/dist/tui.js.map +1 -0
- package/dist/tui_layout.d.ts +151 -0
- package/dist/tui_layout.js +582 -0
- package/dist/tui_layout.js.map +1 -0
- package/dist/tui_model.d.ts +153 -0
- package/dist/tui_model.js +673 -0
- package/dist/tui_model.js.map +1 -0
- package/package.json +18 -14
- package/.eslintrc.js +0 -24
- package/.idea/codeStyles/Project.xml +0 -364
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/compiler.xml +0 -7
- package/.idea/encodings.xml +0 -6
- package/.idea/inspectionProfiles/Project_Default.xml +0 -12
- package/.idea/jsLibraryMappings.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/prettier.xml +0 -8
- package/.idea/talented-args.iml +0 -11
- package/.idea/vcs.xml +0 -6
- package/.prettierrc.yaml +0 -4
- package/dist/definitions.d.ts +0 -70
- package/dist/definitions.js +0 -38
- package/dist/definitions.js.map +0 -1
- package/dist/main.d.ts +0 -33
- package/dist/main.js +0 -313
- package/dist/main.js.map +0 -1
- package/jest.config.js +0 -4
- package/tsconfig.json +0 -18
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { IBuilder, ICommandTokens, IParsed, IShape } from './descriptors';
|
|
2
|
+
import { IOptions } from './options';
|
|
3
|
+
import { ITerminalInput, ITerminalOutput } from './terminal';
|
|
4
|
+
import { ITuiOptions } from './tui';
|
|
5
|
+
import { ITuiOutcome } from './tui_model';
|
|
6
|
+
/**
|
|
7
|
+
* Result of synchronous parsing: parsed arguments, a help request,
|
|
8
|
+
* a missing command, or an interactive mode request.
|
|
9
|
+
*/
|
|
10
|
+
export type IParseResult<TArgs, TCommandToken extends string = string> = {
|
|
11
|
+
/** Arguments are complete and valid. */
|
|
12
|
+
readonly kind: 'parsed';
|
|
13
|
+
readonly args: TArgs;
|
|
14
|
+
/** Selected command token, or `null` when running without a command. */
|
|
15
|
+
readonly command: TCommandToken | null;
|
|
16
|
+
} | {
|
|
17
|
+
/** Help was requested. */
|
|
18
|
+
readonly kind: 'help';
|
|
19
|
+
/** Command token to show help for, or `null` for program help. */
|
|
20
|
+
readonly command: TCommandToken | null;
|
|
21
|
+
} | {
|
|
22
|
+
/** No command was supplied, but the program requires one. */
|
|
23
|
+
readonly kind: 'needs-command';
|
|
24
|
+
} | {
|
|
25
|
+
/** Interactive mode was explicitly requested. */
|
|
26
|
+
readonly kind: 'interactive';
|
|
27
|
+
readonly command: TCommandToken | null;
|
|
28
|
+
};
|
|
29
|
+
export interface IHelpOptions {
|
|
30
|
+
/** Help layout width in terminal columns. Defaults to 80. */
|
|
31
|
+
width?: number | undefined;
|
|
32
|
+
/** Include terminal styling escape codes. Defaults to `false`. */
|
|
33
|
+
color?: boolean | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Executable shown in usage examples.
|
|
36
|
+
* Defaults to the `executable` option, then the application script's filename.
|
|
37
|
+
*/
|
|
38
|
+
executable?: string | undefined;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Process streams, environment, and actions used by `parseOrDie()`.
|
|
42
|
+
* Injectable so tests can exercise the flow without using the real process.
|
|
43
|
+
*/
|
|
44
|
+
export interface IRuntime {
|
|
45
|
+
readonly stdin: ITerminalInput;
|
|
46
|
+
readonly stdout: ITerminalOutput;
|
|
47
|
+
readonly stderr: ITerminalOutput;
|
|
48
|
+
readonly env: NodeJS.ProcessEnv;
|
|
49
|
+
/** Application script path, used to infer its executable name and locate package metadata. */
|
|
50
|
+
readonly mainFilename: string | undefined;
|
|
51
|
+
exit(code: number): never;
|
|
52
|
+
runTui(options: ITuiOptions): Promise<ITuiOutcome>;
|
|
53
|
+
/**
|
|
54
|
+
* Copy text through a system clipboard tool or a terminal escape sequence.
|
|
55
|
+
* Returns `true` only when a clipboard tool reports success.
|
|
56
|
+
*/
|
|
57
|
+
copyToClipboard(text: string, terminal: ITerminalOutput): Promise<boolean>;
|
|
58
|
+
}
|
|
59
|
+
/** Whether output should include terminal colors, based on the stream and environment. */
|
|
60
|
+
export declare function wantsColor(stream: ITerminalOutput, env: NodeJS.ProcessEnv): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Terminal output stream for interactive mode, preferring stdout over stderr.
|
|
63
|
+
* Returns `null` without terminal input, in CI, or when no suitable output terminal is available.
|
|
64
|
+
*/
|
|
65
|
+
export declare function interactiveStream(runtime: IRuntime): ITerminalOutput | null;
|
|
66
|
+
/**
|
|
67
|
+
* Program created with `talentedArgs()`.
|
|
68
|
+
* Parses arguments and provides help and an interactive editor.
|
|
69
|
+
*/
|
|
70
|
+
export declare class TalentedArgs<TShape extends IShape> {
|
|
71
|
+
private readonly runtime;
|
|
72
|
+
private readonly options;
|
|
73
|
+
private readonly schema;
|
|
74
|
+
private appInfo;
|
|
75
|
+
constructor(options: IOptions, define: (ta: IBuilder) => TShape, runtime?: IRuntime);
|
|
76
|
+
/**
|
|
77
|
+
* Parse argument tokens synchronously, excluding the Node executable and script paths.
|
|
78
|
+
* Returns parsed arguments or reports a help request, missing command, or interactive request.
|
|
79
|
+
* Throws `UsageError` for invalid input without printing, exiting, or opening the editor.
|
|
80
|
+
*/
|
|
81
|
+
parse(tokens: readonly string[]): IParseResult<IParsed<TShape>, ICommandTokens<TShape>>;
|
|
82
|
+
/**
|
|
83
|
+
* Parse `process.argv` and return the arguments, opening interactive mode when requested
|
|
84
|
+
* or when the missing command policy requires it. The first two array entries are skipped.
|
|
85
|
+
*
|
|
86
|
+
* Prints help and exits with code 0 when requested. Prints usage errors and exits with code 1.
|
|
87
|
+
* Cancelling interactive mode exits with code 130. Copying a command exits with code 0.
|
|
88
|
+
* Returns normally after successful parsing or submission from the editor.
|
|
89
|
+
*/
|
|
90
|
+
parseOrDie(argv?: readonly string[]): Promise<IParsed<TShape>>;
|
|
91
|
+
/**
|
|
92
|
+
* Return help text for the program, or for the supplied command token.
|
|
93
|
+
* Does not print or exit. Use `options` to set the width, styling, and displayed executable.
|
|
94
|
+
*/
|
|
95
|
+
help(command?: ICommandTokens<TShape> | null, options?: IHelpOptions): string;
|
|
96
|
+
private tokenOf;
|
|
97
|
+
/**
|
|
98
|
+
* Convert supplied values, check requirements, and build the result.
|
|
99
|
+
* Throws `UsageError` for missing or invalid values.
|
|
100
|
+
* @param interactive Whether the values were assembled in interactive mode.
|
|
101
|
+
*/
|
|
102
|
+
private resolve;
|
|
103
|
+
private executableName;
|
|
104
|
+
/** Resolve the configured state file or custom store. Returns `null` when remembering is disabled. */
|
|
105
|
+
private rememberStore;
|
|
106
|
+
private getAppInfo;
|
|
107
|
+
private displayContext;
|
|
108
|
+
/** Choose output styling and width for the stream. Nonterminal output uses plain text and a fallback width. */
|
|
109
|
+
private streamDisplay;
|
|
110
|
+
private renderHelpFor;
|
|
111
|
+
private renderIssuesFor;
|
|
112
|
+
}
|
|
113
|
+
/** Parsed argument type for a program: `type IArgs = TalentedParsedArgs<typeof program>`. */
|
|
114
|
+
export type IParsedArgs<TProgram> = TProgram extends TalentedArgs<infer TShape> ? IParsed<TShape> : never;
|
|
115
|
+
/**
|
|
116
|
+
* Define a command line program and infer its parsed result type from the callback's return value.
|
|
117
|
+
* @param options Display metadata, styles, and parsing behavior.
|
|
118
|
+
* @param define Callback receiving a builder. Return an object containing argument, flag,
|
|
119
|
+
* command, group, and metadata definitions in the structure your program should receive.
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* talentedArgs(options, ta => ({ port: ta.arg(['-p', '--port'], 'Port to use', Number) }));
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export declare function talentedArgs<TShape extends IShape>(options: IOptions, define: (ta: IBuilder) => TShape): TalentedArgs<TShape>;
|
package/dist/program.js
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.TalentedArgs = void 0;
|
|
7
|
+
exports.wantsColor = wantsColor;
|
|
8
|
+
exports.interactiveStream = interactiveStream;
|
|
9
|
+
exports.talentedArgs = talentedArgs;
|
|
10
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
11
|
+
const clipboard_1 = require("./clipboard");
|
|
12
|
+
const compile_1 = require("./compile");
|
|
13
|
+
const descriptors_1 = require("./descriptors");
|
|
14
|
+
const display_1 = require("./display");
|
|
15
|
+
const errors_1 = require("./errors");
|
|
16
|
+
const options_1 = require("./options");
|
|
17
|
+
const parse_1 = require("./parse");
|
|
18
|
+
const remember_1 = require("./remember");
|
|
19
|
+
const styling_1 = require("./styling");
|
|
20
|
+
const terminal_1 = require("./terminal");
|
|
21
|
+
const tools_1 = require("./tools");
|
|
22
|
+
const tui_1 = require("./tui");
|
|
23
|
+
function processRuntime() {
|
|
24
|
+
return {
|
|
25
|
+
stdin: process.stdin,
|
|
26
|
+
stdout: process.stdout,
|
|
27
|
+
stderr: process.stderr,
|
|
28
|
+
env: process.env,
|
|
29
|
+
mainFilename: require.main?.filename,
|
|
30
|
+
exit: code => process.exit(code),
|
|
31
|
+
runTui: tui_1.runTui,
|
|
32
|
+
copyToClipboard: (text, terminal) => (0, clipboard_1.copyToClipboard)(text, terminal, process.env),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function isSet(value) {
|
|
36
|
+
return value !== undefined && value !== '' && value !== '0' && value.toLowerCase() !== 'false';
|
|
37
|
+
}
|
|
38
|
+
/** Whether output should include terminal colors, based on the stream and environment. */
|
|
39
|
+
function wantsColor(stream, env) {
|
|
40
|
+
if (env['NO_COLOR'] !== undefined && env['NO_COLOR'] !== '') {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
if (isSet(env['FORCE_COLOR'])) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
if (isSet(env['CI']) || !env['TERM'] || env['TERM'] === 'dumb') {
|
|
47
|
+
// Disable colors in CI or when terminal capabilities are unknown.
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
return stream.isTTY === true;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Terminal output stream for interactive mode, preferring stdout over stderr.
|
|
54
|
+
* Returns `null` without terminal input, in CI, or when no suitable output terminal is available.
|
|
55
|
+
*/
|
|
56
|
+
function interactiveStream(runtime) {
|
|
57
|
+
const { stdin, stdout, stderr, env } = runtime;
|
|
58
|
+
if (stdin.isTTY !== true || isSet(env['CI']) || !env['TERM'] || env['TERM'] === 'dumb') {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
// Use stderr when stdout is redirected, keeping normal program output separate from the editor.
|
|
62
|
+
return stdout.isTTY === true ? stdout : stderr.isTTY === true ? stderr : null;
|
|
63
|
+
}
|
|
64
|
+
/** Message describing clipboard confirmation and masked values after a copy. */
|
|
65
|
+
function copyNote(copied, masked) {
|
|
66
|
+
const notes = copied ? ['Copied to the clipboard.'] : [];
|
|
67
|
+
const names = (0, tools_1.quoteList)(masked);
|
|
68
|
+
if (masked.length === 1) {
|
|
69
|
+
notes.push(copied
|
|
70
|
+
? `The value of ${names} is hidden above, the copy has it.`
|
|
71
|
+
: `The value of ${names} is hidden, put it back in before running the command.`);
|
|
72
|
+
}
|
|
73
|
+
else if (masked.length > 1) {
|
|
74
|
+
notes.push(copied
|
|
75
|
+
? `Values of ${names} are hidden above, the copy has them.`
|
|
76
|
+
: `Values of ${names} are hidden, put them back in before running the command.`);
|
|
77
|
+
}
|
|
78
|
+
return notes.join(' ');
|
|
79
|
+
}
|
|
80
|
+
function write(stream, text) {
|
|
81
|
+
return new Promise(resolve => {
|
|
82
|
+
stream.write(text, () => resolve());
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
// *********************************************************************************************************************
|
|
86
|
+
/**
|
|
87
|
+
* Program created with `talentedArgs()`.
|
|
88
|
+
* Parses arguments and provides help and an interactive editor.
|
|
89
|
+
*/
|
|
90
|
+
class TalentedArgs {
|
|
91
|
+
runtime;
|
|
92
|
+
options;
|
|
93
|
+
schema;
|
|
94
|
+
appInfo;
|
|
95
|
+
constructor(options, define, runtime = processRuntime()) {
|
|
96
|
+
this.runtime = runtime;
|
|
97
|
+
this.options = (0, options_1.normalizeAndValidateOptions)(options);
|
|
98
|
+
if (typeof define !== 'function') {
|
|
99
|
+
throw new errors_1.DefinitionError(`args are defined in a callback: talentedArgs(options, ta => ({ port: ta.arg(['--port'], 'Port to use') }))`);
|
|
100
|
+
}
|
|
101
|
+
this.schema = (0, compile_1.compileSchema)(define((0, descriptors_1.createBuilder)()), this.options);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Parse argument tokens synchronously, excluding the Node executable and script paths.
|
|
105
|
+
* Returns parsed arguments or reports a help request, missing command, or interactive request.
|
|
106
|
+
* Throws `UsageError` for invalid input without printing, exiting, or opening the editor.
|
|
107
|
+
*/
|
|
108
|
+
parse(tokens) {
|
|
109
|
+
const outcome = (0, parse_1.bindTokens)(this.schema, tokens);
|
|
110
|
+
if (outcome.kind === 'help') {
|
|
111
|
+
return { kind: 'help', command: this.tokenOf(outcome.command) };
|
|
112
|
+
}
|
|
113
|
+
const { binding } = outcome;
|
|
114
|
+
if (outcome.interactive === true) {
|
|
115
|
+
return { kind: 'interactive', command: this.tokenOf(binding.command) };
|
|
116
|
+
}
|
|
117
|
+
if (outcome.needsCommand) {
|
|
118
|
+
return { kind: 'needs-command' };
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
kind: 'parsed',
|
|
122
|
+
args: this.resolve(binding),
|
|
123
|
+
command: this.tokenOf(binding.command),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Parse `process.argv` and return the arguments, opening interactive mode when requested
|
|
128
|
+
* or when the missing command policy requires it. The first two array entries are skipped.
|
|
129
|
+
*
|
|
130
|
+
* Prints help and exits with code 0 when requested. Prints usage errors and exits with code 1.
|
|
131
|
+
* Cancelling interactive mode exits with code 130. Copying a command exits with code 0.
|
|
132
|
+
* Returns normally after successful parsing or submission from the editor.
|
|
133
|
+
*/
|
|
134
|
+
async parseOrDie(argv = process.argv) {
|
|
135
|
+
const { runtime, schema } = this;
|
|
136
|
+
const tokens = argv.slice(2);
|
|
137
|
+
const executable = this.executableName(argv[1]);
|
|
138
|
+
// Find a mentioned command to choose the help hint if parsing fails.
|
|
139
|
+
const mentioned = tokens
|
|
140
|
+
.map(token => schema.commandsByToken.get(token))
|
|
141
|
+
.find(command => command !== undefined) ?? null;
|
|
142
|
+
try {
|
|
143
|
+
const outcome = (0, parse_1.bindTokens)(schema, tokens);
|
|
144
|
+
if (outcome.kind === 'help') {
|
|
145
|
+
await write(runtime.stdout, this.renderHelpFor(runtime.stdout, executable, outcome.command) + '\n');
|
|
146
|
+
return runtime.exit(errors_1.EXIT_CODES.help);
|
|
147
|
+
}
|
|
148
|
+
let { binding } = outcome;
|
|
149
|
+
const asked = outcome.interactive === true;
|
|
150
|
+
const opensByItself = outcome.needsCommand &&
|
|
151
|
+
outcome.interactive !== false &&
|
|
152
|
+
this.options.missingCommandPolicy === 'tui';
|
|
153
|
+
if (asked || opensByItself) {
|
|
154
|
+
const stream = interactiveStream(runtime);
|
|
155
|
+
if (stream) {
|
|
156
|
+
const store = this.rememberStore();
|
|
157
|
+
const remembered = store ? (0, remember_1.sanitizeRemembered)(await store.load()) : null;
|
|
158
|
+
const result = await runtime.runTui({
|
|
159
|
+
schema,
|
|
160
|
+
seed: binding,
|
|
161
|
+
commandChosen: !outcome.needsCommand && binding.command !== null,
|
|
162
|
+
remembered,
|
|
163
|
+
appInfo: this.getAppInfo(),
|
|
164
|
+
executable,
|
|
165
|
+
styles: this.options.styles,
|
|
166
|
+
stdin: runtime.stdin,
|
|
167
|
+
stdout: stream,
|
|
168
|
+
env: runtime.env,
|
|
169
|
+
});
|
|
170
|
+
if (result.kind === 'cancel') {
|
|
171
|
+
return runtime.exit(errors_1.EXIT_CODES.cancelled);
|
|
172
|
+
}
|
|
173
|
+
const picked = result.command === null ? null : schema.commandsByToken.get(result.command);
|
|
174
|
+
if (picked === undefined) {
|
|
175
|
+
throw new TypeError(`interactive mode picked a command that does not exist`);
|
|
176
|
+
}
|
|
177
|
+
const ran = (0, parse_1.bindTarget)(schema, picked, result.tokens);
|
|
178
|
+
if (result.kind === 'copy') {
|
|
179
|
+
// Write the displayed command to stdout so it can be copied from the terminal or redirected to a file.
|
|
180
|
+
await write(runtime.stdout, result.shown + '\n');
|
|
181
|
+
const copied = await runtime.copyToClipboard(result.text, stream);
|
|
182
|
+
const note = copyNote(copied, result.masked);
|
|
183
|
+
if (note) {
|
|
184
|
+
await write(runtime.stderr, note + '\n');
|
|
185
|
+
}
|
|
186
|
+
// Save copied commands for the next interactive session, just like submitted commands.
|
|
187
|
+
if (store?.save) {
|
|
188
|
+
await store.save((0, remember_1.rememberRun)(schema, remembered ?? {}, ran));
|
|
189
|
+
}
|
|
190
|
+
return runtime.exit(errors_1.EXIT_CODES.copied);
|
|
191
|
+
}
|
|
192
|
+
const args = this.resolve(ran, true);
|
|
193
|
+
if (store?.save) {
|
|
194
|
+
await store.save((0, remember_1.rememberRun)(schema, remembered ?? {}, ran));
|
|
195
|
+
}
|
|
196
|
+
return args;
|
|
197
|
+
}
|
|
198
|
+
if (asked) {
|
|
199
|
+
throw new errors_1.UsageError([
|
|
200
|
+
{
|
|
201
|
+
code: 'not_interactive',
|
|
202
|
+
message: 'Interactive mode needs a terminal. It can not be used when input or output is redirected, or in CI',
|
|
203
|
+
option: '--interactive',
|
|
204
|
+
},
|
|
205
|
+
]);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (outcome.needsCommand) {
|
|
209
|
+
await write(runtime.stderr, this.renderHelpFor(runtime.stderr, executable, null) + '\n');
|
|
210
|
+
throw new errors_1.UsageError([
|
|
211
|
+
{ code: 'unknown_command', message: 'No command was given', option: null },
|
|
212
|
+
]);
|
|
213
|
+
}
|
|
214
|
+
binding = outcome.binding;
|
|
215
|
+
return this.resolve(binding);
|
|
216
|
+
}
|
|
217
|
+
catch (err) {
|
|
218
|
+
if (!(err instanceof errors_1.UsageError)) {
|
|
219
|
+
throw err;
|
|
220
|
+
}
|
|
221
|
+
await write(runtime.stderr, this.renderIssuesFor(err.issues, executable, mentioned));
|
|
222
|
+
return runtime.exit(errors_1.EXIT_CODES.usage);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Return help text for the program, or for the supplied command token.
|
|
227
|
+
* Does not print or exit. Use `options` to set the width, styling, and displayed executable.
|
|
228
|
+
*/
|
|
229
|
+
help(command = null, options = {}) {
|
|
230
|
+
let compiled = null;
|
|
231
|
+
if (command !== null) {
|
|
232
|
+
compiled = this.schema.commandsByToken.get(command) ?? null;
|
|
233
|
+
if (!compiled) {
|
|
234
|
+
throw new errors_1.DefinitionError(`there is no command "${command}" to show help for`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
let paint = styling_1.paintNothing;
|
|
238
|
+
if (options.color) {
|
|
239
|
+
const session = (0, terminal_1.openTerminal)(this.runtime.stdin, this.runtime.stdout, undefined, true);
|
|
240
|
+
paint = (0, terminal_1.createPaint)(session.terminal);
|
|
241
|
+
session.release();
|
|
242
|
+
}
|
|
243
|
+
return (0, display_1.renderHelp)(this.displayContext(paint, options.width ?? display_1.FALLBACK_WIDTH, options.executable ?? this.executableName(undefined)), compiled);
|
|
244
|
+
}
|
|
245
|
+
// *******************************************************************************************************************
|
|
246
|
+
tokenOf(command) {
|
|
247
|
+
// The compiled schema has the command tokens from TShape; TypeScript cannot infer that relationship.
|
|
248
|
+
return command ? command.token : null;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Convert supplied values, check requirements, and build the result.
|
|
252
|
+
* Throws `UsageError` for missing or invalid values.
|
|
253
|
+
* @param interactive Whether the values were assembled in interactive mode.
|
|
254
|
+
*/
|
|
255
|
+
resolve(binding, interactive = false) {
|
|
256
|
+
const resolved = (0, parse_1.resolveBinding)(this.schema, binding, interactive);
|
|
257
|
+
if (!resolved.ok) {
|
|
258
|
+
throw new errors_1.UsageError(resolved.issues);
|
|
259
|
+
}
|
|
260
|
+
// The runtime schema and IParsed<TShape> come from the same definition.
|
|
261
|
+
// TypeScript cannot prove that correspondence, so the result is asserted here.
|
|
262
|
+
return resolved.args;
|
|
263
|
+
}
|
|
264
|
+
executableName(scriptPath) {
|
|
265
|
+
return (this.options.executable ??
|
|
266
|
+
(node_path_1.default.basename(scriptPath ?? this.runtime.mainFilename ?? '') || 'program'));
|
|
267
|
+
}
|
|
268
|
+
/** Resolve the configured state file or custom store. Returns `null` when remembering is disabled. */
|
|
269
|
+
rememberStore() {
|
|
270
|
+
const { remember } = this.options;
|
|
271
|
+
if (remember === null || typeof remember === 'object') {
|
|
272
|
+
return remember;
|
|
273
|
+
}
|
|
274
|
+
if (typeof remember === 'string') {
|
|
275
|
+
return (0, remember_1.createFileStore)(node_path_1.default.resolve(remember));
|
|
276
|
+
}
|
|
277
|
+
// Prefer the package identifier over the display name when choosing a state directory.
|
|
278
|
+
const name = (0, options_1.getPackageName)(this.runtime.mainFilename) ?? this.options.name ?? '';
|
|
279
|
+
if (!(0, remember_1.fileNameOf)(name)) {
|
|
280
|
+
throw new errors_1.DefinitionError(`"remember: true" names the file after the program, and this one has no name. ` +
|
|
281
|
+
`Give it a "name", or give "remember" the path of a file`);
|
|
282
|
+
}
|
|
283
|
+
return (0, remember_1.createFileStore)((0, remember_1.defaultRememberPath)(name, this.runtime.env, process.platform));
|
|
284
|
+
}
|
|
285
|
+
getAppInfo() {
|
|
286
|
+
this.appInfo = this.appInfo ?? (0, options_1.getAppInfo)(this.options, this.runtime.mainFilename);
|
|
287
|
+
return this.appInfo;
|
|
288
|
+
}
|
|
289
|
+
displayContext(paint, width, executable) {
|
|
290
|
+
return {
|
|
291
|
+
schema: this.schema,
|
|
292
|
+
appInfo: this.getAppInfo(),
|
|
293
|
+
executable,
|
|
294
|
+
painter: (0, styling_1.createStyleSpecPainter)(this.options.styles, paint),
|
|
295
|
+
text: (0, terminal_1.createTextTools)(),
|
|
296
|
+
width,
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
/** Choose output styling and width for the stream. Nonterminal output uses plain text and a fallback width. */
|
|
300
|
+
streamDisplay(stream) {
|
|
301
|
+
if (!wantsColor(stream, this.runtime.env)) {
|
|
302
|
+
return {
|
|
303
|
+
paint: styling_1.paintNothing,
|
|
304
|
+
width: stream.isTTY ? stream.columns ?? display_1.FALLBACK_WIDTH : display_1.FALLBACK_WIDTH,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
const session = (0, terminal_1.openTerminal)(this.runtime.stdin, stream, undefined, true);
|
|
308
|
+
const paint = (0, terminal_1.createPaint)(session.terminal);
|
|
309
|
+
// Release the session after obtaining its color escape sequences.
|
|
310
|
+
session.release();
|
|
311
|
+
return { paint, width: stream.isTTY ? stream.columns ?? display_1.FALLBACK_WIDTH : display_1.FALLBACK_WIDTH };
|
|
312
|
+
}
|
|
313
|
+
renderHelpFor(stream, executable, command) {
|
|
314
|
+
const { paint, width } = this.streamDisplay(stream);
|
|
315
|
+
return (0, display_1.renderHelp)(this.displayContext(paint, width, executable), command);
|
|
316
|
+
}
|
|
317
|
+
renderIssuesFor(issues, executable, command) {
|
|
318
|
+
const { paint } = this.streamDisplay(this.runtime.stderr);
|
|
319
|
+
return (0, display_1.renderIssues)(issues, (0, styling_1.createStyleSpecPainter)(this.options.styles, paint), executable, command);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
exports.TalentedArgs = TalentedArgs;
|
|
323
|
+
// *********************************************************************************************************************
|
|
324
|
+
/**
|
|
325
|
+
* Define a command line program and infer its parsed result type from the callback's return value.
|
|
326
|
+
* @param options Display metadata, styles, and parsing behavior.
|
|
327
|
+
* @param define Callback receiving a builder. Return an object containing argument, flag,
|
|
328
|
+
* command, group, and metadata definitions in the structure your program should receive.
|
|
329
|
+
* @example
|
|
330
|
+
* ```typescript
|
|
331
|
+
* talentedArgs(options, ta => ({ port: ta.arg(['-p', '--port'], 'Port to use', Number) }));
|
|
332
|
+
* ```
|
|
333
|
+
*/
|
|
334
|
+
function talentedArgs(options, define) {
|
|
335
|
+
return new TalentedArgs(options, define);
|
|
336
|
+
}
|
|
337
|
+
//# sourceMappingURL=program.js.map
|
|
@@ -0,0 +1 @@
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AEO,aAAa,CACnB,MAAuB,EACvB,UAAkB,EAClB,OAAgC;QAEhC,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;QACpD,OAAO,IAAA,oBAAU,EAAC,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,KAAK,EAAE,UAAU,CAAC,EAAE,OAAO,CAAC,CAAC;IAC5E,CAAC;IAEO,eAAe,CACrB,MAA8B,EAC9B,UAAkB,EAClB,OAAgC;QAEhC,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAC1D,OAAO,IAAA,sBAAY,EACjB,MAAM,EACN,IAAA,gCAAsB,EAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,EAClD,UAAU,EACV,OAAO,CACR,CAAC;IACJ,CAAC;CACF;AAjSD,oCAiSC;AAOD,wHAAwH;AAExH;;;;;;;;;GASG;AACH,SAAgB,YAAY,CAC1B,OAAiB,EACjB,MAAgC;IAEhC,OAAO,IAAI,YAAY,CAAS,OAAO,EAAE,MAAM,CAAC,CAAC;AACnD,CAAC"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { ICompiledCommand, ICompiledSchema } from './compile';
|
|
2
|
+
import { IInputSpec } from './descriptors';
|
|
3
|
+
import { IBinding, IRawValue } from './parse';
|
|
4
|
+
/** Store and restore the command and option values submitted or copied from interactive mode. */
|
|
5
|
+
/**
|
|
6
|
+
* Saved interactive state: selected command and option values.
|
|
7
|
+
* Option keys use display names such as `--port`. Saved values are raw input values,
|
|
8
|
+
* so they pass through the current converters when restored.
|
|
9
|
+
*/
|
|
10
|
+
export interface IRemembered {
|
|
11
|
+
/** Last submitted or copied command token. `null` means no command was selected. */
|
|
12
|
+
command?: string | null | undefined;
|
|
13
|
+
/** Values of options shared by all commands. */
|
|
14
|
+
common?: IRememberedValues | undefined;
|
|
15
|
+
/** Saved option values for each command, keyed by command token. */
|
|
16
|
+
commands?: {
|
|
17
|
+
[command: string]: IRememberedValues;
|
|
18
|
+
} | undefined;
|
|
19
|
+
}
|
|
20
|
+
/** String or number for a value option, an array for a list, or a boolean for a flag. */
|
|
21
|
+
export type IRememberedValue = string | number | boolean | readonly (string | number)[];
|
|
22
|
+
export interface IRememberedValues {
|
|
23
|
+
[option: string]: IRememberedValue;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Custom storage for interactive state.
|
|
27
|
+
* Both methods can return promises. Omit `save()` to provide initial values without saving changes.
|
|
28
|
+
*/
|
|
29
|
+
export interface IRememberStore {
|
|
30
|
+
/** Load saved or initial values. Return `null` or `undefined` when no values are available. */
|
|
31
|
+
load(): IRemembered | null | undefined | Promise<IRemembered | null | undefined>;
|
|
32
|
+
/** Save the submitted or copied command and values. Optional for a store that only prefills fields. */
|
|
33
|
+
save?(remembered: IRemembered): void | Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
export declare function isRememberStore(value: unknown): value is IRememberStore;
|
|
36
|
+
/** Validate the structure of loaded state and discard unsupported fields or value types. */
|
|
37
|
+
export declare function sanitizeRemembered(loaded: unknown): IRemembered;
|
|
38
|
+
/** Saved values matched to the current commands and input definitions. */
|
|
39
|
+
export interface IRecalled {
|
|
40
|
+
/** Initial command, or `undefined` if the saved selection is no longer available. */
|
|
41
|
+
readonly command: ICompiledCommand | null | undefined;
|
|
42
|
+
/** Raw saved values for options shared by all commands. */
|
|
43
|
+
readonly common: ReadonlyMap<IInputSpec, readonly IRawValue[]>;
|
|
44
|
+
/** Raw saved values for each command's options. */
|
|
45
|
+
readonly commands: ReadonlyMap<ICompiledCommand, ReadonlyMap<IInputSpec, readonly IRawValue[]>>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Match saved state to the current definition. Ignore removed commands and options,
|
|
49
|
+
* and values with incompatible types or list lengths.
|
|
50
|
+
* Keep values that fail conversion so the editor can show their validation errors.
|
|
51
|
+
*/
|
|
52
|
+
export declare function recall(schema: ICompiledSchema, remembered: IRemembered): IRecalled;
|
|
53
|
+
/**
|
|
54
|
+
* Build saved state for a submitted or copied command.
|
|
55
|
+
* Replace common values and the selected command's values so cleared fields stay empty.
|
|
56
|
+
* Preserve other commands' values. Exclude sensitive inputs.
|
|
57
|
+
*/
|
|
58
|
+
export declare function rememberRun(schema: ICompiledSchema, previous: IRemembered, binding: IBinding): IRemembered;
|
|
59
|
+
/** Normalize a program name for a state directory: `@acme/My Tool` becomes `acme-my-tool`. */
|
|
60
|
+
export declare function fileNameOf(name: string): string;
|
|
61
|
+
/**
|
|
62
|
+
* Default path for interactive state.
|
|
63
|
+
* An absolute `XDG_STATE_HOME` overrides the base directory on any platform.
|
|
64
|
+
* Otherwise use `~/.local/state` on Linux, Application Support on macOS, or LocalAppData on Windows.
|
|
65
|
+
*/
|
|
66
|
+
export declare function defaultRememberPath(name: string, env: NodeJS.ProcessEnv, platform: NodeJS.Platform): string;
|
|
67
|
+
/**
|
|
68
|
+
* Store interactive state in a JSON file.
|
|
69
|
+
* Treat missing, unreadable, or invalid files as empty state. Ignore failed writes.
|
|
70
|
+
*/
|
|
71
|
+
export declare function createFileStore(path: string): IRememberStore;
|