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.
Files changed (70) hide show
  1. package/README.md +506 -14
  2. package/dist/clipboard.d.ts +10 -0
  3. package/dist/clipboard.js +79 -0
  4. package/dist/clipboard.js.map +1 -0
  5. package/dist/compile.d.ts +67 -0
  6. package/dist/compile.js +188 -0
  7. package/dist/compile.js.map +1 -0
  8. package/dist/descriptors.d.ts +322 -0
  9. package/dist/descriptors.js +362 -0
  10. package/dist/descriptors.js.map +1 -0
  11. package/dist/display.d.ts +43 -0
  12. package/dist/display.js +251 -0
  13. package/dist/display.js.map +1 -0
  14. package/dist/errors.d.ts +34 -17
  15. package/dist/errors.js +61 -75
  16. package/dist/errors.js.map +1 -1
  17. package/dist/index.d.ts +10 -6
  18. package/dist/index.js +8 -9
  19. package/dist/index.js.map +1 -1
  20. package/dist/options.d.ts +69 -41
  21. package/dist/options.js +198 -31
  22. package/dist/options.js.map +1 -1
  23. package/dist/parse.d.ts +70 -0
  24. package/dist/parse.js +551 -0
  25. package/dist/parse.js.map +1 -0
  26. package/dist/program.d.ts +125 -0
  27. package/dist/program.js +337 -0
  28. package/dist/program.js.map +1 -0
  29. package/dist/remember.d.ts +71 -0
  30. package/dist/remember.js +216 -0
  31. package/dist/remember.js.map +1 -0
  32. package/dist/styling.d.ts +38 -9
  33. package/dist/styling.js +155 -75
  34. package/dist/styling.js.map +1 -1
  35. package/dist/terminal.d.ts +205 -0
  36. package/dist/terminal.js +152 -0
  37. package/dist/terminal.js.map +1 -0
  38. package/dist/tools.d.ts +35 -31
  39. package/dist/tools.js +68 -140
  40. package/dist/tools.js.map +1 -1
  41. package/dist/tui.d.ts +96 -0
  42. package/dist/tui.js +397 -0
  43. package/dist/tui.js.map +1 -0
  44. package/dist/tui_layout.d.ts +151 -0
  45. package/dist/tui_layout.js +582 -0
  46. package/dist/tui_layout.js.map +1 -0
  47. package/dist/tui_model.d.ts +153 -0
  48. package/dist/tui_model.js +673 -0
  49. package/dist/tui_model.js.map +1 -0
  50. package/package.json +18 -14
  51. package/.eslintrc.js +0 -24
  52. package/.idea/codeStyles/Project.xml +0 -364
  53. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  54. package/.idea/compiler.xml +0 -7
  55. package/.idea/encodings.xml +0 -6
  56. package/.idea/inspectionProfiles/Project_Default.xml +0 -12
  57. package/.idea/jsLibraryMappings.xml +0 -6
  58. package/.idea/modules.xml +0 -8
  59. package/.idea/prettier.xml +0 -8
  60. package/.idea/talented-args.iml +0 -11
  61. package/.idea/vcs.xml +0 -6
  62. package/.prettierrc.yaml +0 -4
  63. package/dist/definitions.d.ts +0 -70
  64. package/dist/definitions.js +0 -38
  65. package/dist/definitions.js.map +0 -1
  66. package/dist/main.d.ts +0 -33
  67. package/dist/main.js +0 -313
  68. package/dist/main.js.map +0 -1
  69. package/jest.config.js +0 -4
  70. package/tsconfig.json +0 -18
@@ -0,0 +1,67 @@
1
+ import { IInputSpec, IPresence } from './descriptors';
2
+ import { INormalizedOptions } from './options';
3
+ /** Argument descriptor assigned to one result property. */
4
+ export interface IPlacement {
5
+ readonly input: IInputSpec;
6
+ /** Property path relative to the root or command result object. */
7
+ readonly path: readonly string[];
8
+ readonly presence: IPresence;
9
+ readonly defaultValue: unknown;
10
+ }
11
+ /** Argument placements, groups, and metadata in a root or command result object. */
12
+ export interface IScope {
13
+ readonly placements: readonly IPlacement[];
14
+ /** Group paths. Groups exist even when all their optional arguments are omitted. */
15
+ readonly groups: readonly (readonly string[])[];
16
+ /** Property paths for run metadata. */
17
+ readonly metas: readonly (readonly string[])[];
18
+ /** Distinct inputs in definition order. */
19
+ readonly inputs: readonly IInputSpec[];
20
+ }
21
+ /** Inputs available together: root arguments and the selected command's arguments. */
22
+ export interface IActiveScope {
23
+ /** Available inputs, with command inputs first. */
24
+ readonly inputs: readonly IInputSpec[];
25
+ /** Root inputs that are not also placed in the selected command. */
26
+ readonly commonInputs: readonly IInputSpec[];
27
+ /** Command inputs, including those also placed at the root. */
28
+ readonly commandInputs: readonly IInputSpec[];
29
+ readonly byName: ReadonlyMap<string, IInputSpec>;
30
+ readonly byAlias: ReadonlyMap<string, IInputSpec>;
31
+ /** Generated negated flag names without dashes, such as `no-color`. */
32
+ readonly negations: ReadonlyMap<string, IInputSpec>;
33
+ /** Input that accepts positional values, or `null` if none is defined. */
34
+ readonly positional: IInputSpec | null;
35
+ }
36
+ export interface ICompiledCommand {
37
+ /** Result property containing this command's arguments. */
38
+ readonly key: string;
39
+ /** Command name typed on the command line. */
40
+ readonly token: string;
41
+ readonly summary: string;
42
+ readonly details: string;
43
+ readonly scope: IScope;
44
+ readonly active: IActiveScope;
45
+ }
46
+ export interface ICompiledSchema {
47
+ readonly root: IScope;
48
+ readonly rootActive: IActiveScope;
49
+ readonly commands: readonly ICompiledCommand[];
50
+ readonly commandsByToken: ReadonlyMap<string, ICompiledCommand>;
51
+ /** Whether the program can return root arguments without selecting a command. */
52
+ readonly rootRunnable: boolean;
53
+ }
54
+ export declare function activeScopeOf(schema: ICompiledSchema, command: ICompiledCommand | null): IActiveScope;
55
+ /**
56
+ * Compile the definition for parsing, help, and interactive mode.
57
+ * Throws `DefinitionError` for invalid definitions or conflicting inputs.
58
+ */
59
+ export declare function compileSchema(shape: unknown, options: INormalizedOptions): ICompiledSchema;
60
+ /** Combined requirement and display default for an input in the selected command scope. */
61
+ export interface IInputPolicy {
62
+ /** At least one active result property requires this input. */
63
+ readonly required: boolean;
64
+ readonly hasDefault: boolean;
65
+ readonly defaultValue: unknown;
66
+ }
67
+ export declare function inputPolicy(schema: ICompiledSchema, command: ICompiledCommand | null, input: IInputSpec): IInputPolicy;
@@ -0,0 +1,188 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.activeScopeOf = activeScopeOf;
4
+ exports.compileSchema = compileSchema;
5
+ exports.inputPolicy = inputPolicy;
6
+ const descriptors_1 = require("./descriptors");
7
+ const errors_1 = require("./errors");
8
+ const tools_1 = require("./tools");
9
+ function activeScopeOf(schema, command) {
10
+ return command ? command.active : schema.rootActive;
11
+ }
12
+ // *********************************************************************************************************************
13
+ /**
14
+ * Whether a name can be used for a result property.
15
+ * Reject inherited names such as `toString`, so reading an omitted argument cannot
16
+ * return a property from `Object.prototype`. Also reject `prototype`.
17
+ */
18
+ function isUsableKey(key) {
19
+ return !(key in Object.prototype) && key !== 'prototype';
20
+ }
21
+ function describeNode(node) {
22
+ if (node === null) {
23
+ return 'null';
24
+ }
25
+ if (Array.isArray(node)) {
26
+ return 'an array';
27
+ }
28
+ if (typeof node === 'function') {
29
+ return 'a function';
30
+ }
31
+ if (typeof node === 'object') {
32
+ return 'an object that is not a plain one';
33
+ }
34
+ return `${typeof node} ${JSON.stringify(node) ?? ''}`.trim();
35
+ }
36
+ function walkShape(shape, prefix, where, scope, commands, ancestors) {
37
+ if (Object.getOwnPropertySymbols(shape).length) {
38
+ throw new errors_1.DefinitionError(`${prefix.length ? `"${prefix.join('.')}"` : 'the definition'}${where} has a symbol for a property name. ` + `Args can only be placed under regular names`);
39
+ }
40
+ for (const [key, node] of Object.entries(shape)) {
41
+ const path = [...prefix, key];
42
+ const pathLabel = `"${path.join('.')}"${where}`;
43
+ if (!isUsableKey(key)) {
44
+ throw new errors_1.DefinitionError(`${pathLabel} can't be used as a property name, every object already has a "${key}" of its own`);
45
+ }
46
+ const argState = (0, descriptors_1.getArgState)(node);
47
+ if (argState) {
48
+ scope.placements.push({
49
+ input: argState.input,
50
+ path,
51
+ presence: argState.presence,
52
+ defaultValue: argState.defaultValue,
53
+ });
54
+ continue;
55
+ }
56
+ if ((0, descriptors_1.isMeta)(node)) {
57
+ scope.metas.push(path);
58
+ continue;
59
+ }
60
+ const commandState = (0, descriptors_1.getCommandState)(node);
61
+ if (commandState) {
62
+ if (!commands || prefix.length) {
63
+ throw new errors_1.DefinitionError(`command "${commandState.token}" is defined at ${pathLabel}. Commands can only be placed at the top level`);
64
+ }
65
+ commands.push({ key, ...commandState });
66
+ continue;
67
+ }
68
+ if ((0, tools_1.isPlainObject)(node)) {
69
+ if (ancestors.includes(node)) {
70
+ throw new errors_1.DefinitionError(`${pathLabel} contains itself`);
71
+ }
72
+ scope.groups.push(path);
73
+ walkShape(node, path, where, scope, null, [...ancestors, node]);
74
+ continue;
75
+ }
76
+ throw new errors_1.DefinitionError(`${pathLabel} must be an arg, a command or a group of args. Got ${describeNode(node)}`);
77
+ }
78
+ }
79
+ function finishScope(draft) {
80
+ const inputs = [];
81
+ for (const placement of draft.placements) {
82
+ if (!inputs.includes(placement.input)) {
83
+ inputs.push(placement.input);
84
+ }
85
+ }
86
+ return { placements: draft.placements, groups: draft.groups, metas: draft.metas, inputs };
87
+ }
88
+ /** Combine root and command inputs, checking for conflicting option names and positional arguments. */
89
+ function buildActiveScope(root, command, where) {
90
+ const commandInputs = command ? command.inputs : [];
91
+ const commonInputs = root.inputs.filter(input => !commandInputs.includes(input));
92
+ const inputs = [...commandInputs, ...commonInputs];
93
+ const byName = new Map();
94
+ const byAlias = new Map();
95
+ for (const input of inputs) {
96
+ for (const [lookup, spellings, dashes] of [
97
+ [byName, input.names, '--'],
98
+ [byAlias, input.aliases, '-'],
99
+ ]) {
100
+ for (const spelling of spellings) {
101
+ if (lookup.has(spelling)) {
102
+ throw new errors_1.DefinitionError(`option "${dashes}${spelling}" is defined more than once ${where}. ` +
103
+ `To have the same option in several places, define it once and reuse the arg`);
104
+ }
105
+ lookup.set(spelling, input);
106
+ }
107
+ }
108
+ }
109
+ const negations = new Map();
110
+ for (const input of inputs) {
111
+ if (!input.negatable) {
112
+ continue;
113
+ }
114
+ for (const name of input.names) {
115
+ const negated = `no-${name}`;
116
+ // Explicit option names take precedence over generated negations, such as an option named no-defer.
117
+ if (!byName.has(negated) && !negations.has(negated) && negated !== 'no-interactive') {
118
+ negations.set(negated, input);
119
+ }
120
+ }
121
+ }
122
+ const positionals = inputs.filter(input => input.positional);
123
+ if (positionals.length > 1) {
124
+ throw new errors_1.DefinitionError(`there is more than one positional arg ${where}: ${(0, tools_1.quoteList)(positionals.map(descriptors_1.inputDisplayName))}. Only one arg can take the values given without an option name`);
125
+ }
126
+ return {
127
+ inputs,
128
+ commonInputs,
129
+ commandInputs,
130
+ byName,
131
+ byAlias,
132
+ negations,
133
+ positional: positionals[0] ?? null,
134
+ };
135
+ }
136
+ /**
137
+ * Compile the definition for parsing, help, and interactive mode.
138
+ * Throws `DefinitionError` for invalid definitions or conflicting inputs.
139
+ */
140
+ function compileSchema(shape, options) {
141
+ if (!(0, tools_1.isPlainObject)(shape)) {
142
+ throw new errors_1.DefinitionError(`the definition callback must return an object with args and commands. Got ${describeNode(shape)}`);
143
+ }
144
+ const rootDraft = { placements: [], groups: [], metas: [] };
145
+ const commandDrafts = [];
146
+ walkShape(shape, [], '', rootDraft, commandDrafts, [shape]);
147
+ const root = finishScope(rootDraft);
148
+ const rootActive = buildActiveScope(root, null, 'outside of commands');
149
+ const commands = [];
150
+ const commandsByToken = new Map();
151
+ for (const draft of commandDrafts) {
152
+ if (commandsByToken.has(draft.token)) {
153
+ // This also rejects a command descriptor reused under two result properties.
154
+ throw new errors_1.DefinitionError(`command "${draft.token}" is defined more than once`);
155
+ }
156
+ const where = ` of command "${draft.token}"`;
157
+ const scopeDraft = { placements: [], groups: [], metas: [] };
158
+ walkShape(draft.shape, [], where, scopeDraft, null, [draft.shape]);
159
+ const scope = finishScope(scopeDraft);
160
+ const command = {
161
+ key: draft.key,
162
+ token: draft.token,
163
+ summary: draft.summary,
164
+ details: draft.details,
165
+ scope,
166
+ active: buildActiveScope(root, scope, `for command "${draft.token}"`),
167
+ };
168
+ commands.push(command);
169
+ commandsByToken.set(command.token, command);
170
+ }
171
+ // Programs with commands require a selection unless missingCommandPolicy is none.
172
+ const rootRunnable = !commands.length || options.missingCommandPolicy === 'none';
173
+ return { root, rootActive, commands, commandsByToken, rootRunnable };
174
+ }
175
+ function inputPolicy(schema, command, input) {
176
+ // Command placements come first so their defaults take precedence in help and the editor.
177
+ const placements = [
178
+ ...(command ? command.scope.placements : []),
179
+ ...schema.root.placements,
180
+ ].filter(placement => placement.input === input);
181
+ const defaulted = placements.find(placement => placement.presence === 'defaulted');
182
+ return {
183
+ required: placements.some(placement => placement.presence === 'required'),
184
+ hasDefault: defaulted !== undefined,
185
+ defaultValue: defaulted?.defaultValue,
186
+ };
187
+ }
188
+ //# sourceMappingURL=compile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile.js","sourceRoot":"","sources":["../src/compile.ts"],"names":[],"mappings":";;AAiFA,sCAKC;AAuLD,sCA6CC;AAaD,kCAgBC;AAvVD,+CAOuB;AACvB,qCAA2C;AAE3C,mCAAmD;AAuEnD,SAAgB,aAAa,CAC3B,MAAuB,EACvB,OAAgC;IAEhC,OAAO,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC;AACtD,CAAC;AAED,wHAAwH;AAExH;;;;GAIG;AACH,SAAS,WAAW,CAAC,GAAW;IAC9B,OAAO,CAAC,CAAC,GAAG,IAAI,MAAM,CAAC,SAAS,CAAC,IAAI,GAAG,KAAK,WAAW,CAAC;AAC3D,CAAC;AAED,SAAS,YAAY,CAAC,IAAa;IACjC,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;QAClB,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QACxB,OAAO,UAAU,CAAC;IACpB,CAAC;IACD,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE,CAAC;QAC/B,OAAO,YAAY,CAAC;IACtB,CAAC;IACD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,OAAO,mCAAmC,CAAC;IAC7C,CAAC;IACD,OAAO,GAAG,OAAO,IAAI,IAAI,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI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AK,EAAE,gBAAgB,KAAK,CAAC,KAAK,GAAG,CAAC;SACtE,CAAC;QACF,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACvB,eAAe,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAC9C,CAAC;IAED,kFAAkF;IAClF,MAAM,YAAY,GAAG,CAAC,QAAQ,CAAC,MAAM,IAAI,OAAO,CAAC,oBAAoB,KAAK,MAAM,CAAC;IAEjF,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,eAAe,EAAE,YAAY,EAAE,CAAC;AACvE,CAAC;AAaD,SAAgB,WAAW,CACzB,MAAuB,EACvB,OAAgC,EAChC,KAAiB;IAEjB,0FAA0F;IAC1F,MAAM,UAAU,GAAG;QACjB,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC;QAC5C,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU;KAC1B,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,SAAS,CAAC,KAAK,KAAK,KAAK,CAAC,CAAC;IACjD,MAAM,SAAS,GAAG,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,SAAS,CAAC,QAAQ,KAAK,WAAW,CAAC,CAAC;IACnF,OAAO;QACL,QAAQ,EAAE,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,SAAS,CAAC,QAAQ,KAAK,UAAU,CAAC;QACzE,UAAU,EAAE,SAAS,KAAK,SAAS;QACnC,YAAY,EAAE,SAAS,EAAE,YAAY;KACtC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,322 @@
1
+ import { ISimplify } from './tools';
2
+ /** An option name as typed on the command line, such as `-p` or `--port`. */
3
+ export type IOptionName = `-${string}`;
4
+ /**
5
+ * One or more names for the same option.
6
+ * Messages use the first long name, or the first short alias if there is no long name.
7
+ */
8
+ export type IOptionNames = IOptionName | readonly [IOptionName, ...IOptionName[]];
9
+ /**
10
+ * Convert a string from the command line into the value your program receives.
11
+ * The function must return synchronously. Throw an error to reject the value.
12
+ * The error message is shown to the user unless the argument is sensitive.
13
+ */
14
+ export type IConverter<TValue> = (value: string) => TValue;
15
+ export interface IMultipleOptions {
16
+ /**
17
+ * Accept several values after one option name, such as `--ids a b c`.
18
+ * Stops at the next option. Defaults to `false`, so each value needs its own option name:
19
+ * `--ids a --ids b`. Positional lists accept several bare values either way.
20
+ */
21
+ greedy?: boolean;
22
+ }
23
+ export interface ICommandDescription {
24
+ /** Short description shown in the command list. */
25
+ summary: string;
26
+ /** Longer description shown in command help instead of `summary`. */
27
+ details?: string;
28
+ }
29
+ export type IValueType = 'string' | 'number' | 'custom';
30
+ /**
31
+ * Input definition created by a `ta.arg()` or `ta.flag()` call.
32
+ * Multiple result properties can read the same input. Sharing uses object identity.
33
+ */
34
+ export interface IInputSpec {
35
+ readonly kind: 'value' | 'flag';
36
+ /** Long option names without `--`. */
37
+ readonly names: readonly string[];
38
+ /** Single character aliases without `-`. */
39
+ readonly aliases: readonly string[];
40
+ readonly description: string;
41
+ readonly convert: IConverter<unknown>;
42
+ readonly valueType: IValueType;
43
+ /** Label shown in help instead of the value type. */
44
+ readonly label: string | null;
45
+ readonly multiple: boolean;
46
+ readonly greedy: boolean;
47
+ /** Accept values without an option name. */
48
+ readonly positional: boolean;
49
+ /** Mask the value in displays and diagnostics, and exclude it from saved state. */
50
+ readonly sensitive: boolean;
51
+ /** Accept `--no-<name>` to set a flag to `false`. */
52
+ readonly negatable: boolean;
53
+ }
54
+ export type IPresence = 'optional' | 'required' | 'defaulted';
55
+ /**
56
+ * Input definition, requirement, and default for one argument descriptor.
57
+ * Requirements and defaults apply to the result property where the descriptor is placed.
58
+ */
59
+ export interface IArgState {
60
+ readonly input: IInputSpec;
61
+ readonly presence: IPresence;
62
+ readonly defaultValue: unknown;
63
+ }
64
+ export interface ICommandState {
65
+ readonly token: string;
66
+ readonly summary: string;
67
+ readonly details: string;
68
+ readonly shape: {
69
+ readonly [key: string]: unknown;
70
+ };
71
+ }
72
+ /**
73
+ * Read the state of a builder-created argument descriptor.
74
+ * Returns `undefined` for other values.
75
+ */
76
+ export declare function getArgState(value: unknown): IArgState | undefined;
77
+ export declare function getCommandState(value: unknown): ICommandState | undefined;
78
+ /** Name used in messages and help: the first long name, or the first short alias. */
79
+ export declare function inputDisplayName(input: IInputSpec): string;
80
+ /** All input names with their dashes, with short aliases listed first. */
81
+ export declare function inputSpellings(input: IInputSpec): string[];
82
+ /** Type-only marker carrying an argument's value type and whether its property is guaranteed. */
83
+ export declare const ARG_TYPES: unique symbol;
84
+ export interface IArgTypes<TValue, TPresent extends boolean> {
85
+ readonly value: TValue;
86
+ readonly present: TPresent;
87
+ }
88
+ /**
89
+ * Descriptor for a value in the parsed result.
90
+ * Create one with `ta.arg()` or `ta.flag()` and include it in the definition callback's result.
91
+ *
92
+ * Descriptors are immutable. Each method returns a new descriptor.
93
+ * For example, the same input can be optional at the top level and required inside a command.
94
+ */
95
+ export declare abstract class Arg<TValue, TPresent extends boolean = boolean> {
96
+ readonly [ARG_TYPES]: IArgTypes<TValue, TPresent>;
97
+ protected constructor(state: IArgState);
98
+ }
99
+ /** An option that accepts one value, such as `--port 8080`. */
100
+ export declare class ValueArg<TValue, TPresent extends boolean = false> extends Arg<TValue, TPresent> {
101
+ constructor(state: IArgState);
102
+ /**
103
+ * Require the user to supply a value for this result property.
104
+ * Removes any previous default.
105
+ */
106
+ required(): ValueArg<TValue, true>;
107
+ /**
108
+ * Use `value` at this result property when the user omits the option.
109
+ * Defaults bypass the converter. Replaces any previous requirement.
110
+ */
111
+ default(value: TValue): ValueArg<TValue, true>;
112
+ /** Show a custom value label in help, such as `--port TCP_PORT`. */
113
+ label(label: string): ValueArg<TValue, TPresent>;
114
+ /**
115
+ * Mask the value in the editor, help defaults, command previews, and errors.
116
+ * Your program still receives the value, and clipboard copies include it.
117
+ * Sensitive values are never saved by `remember`.
118
+ */
119
+ sensitive(): ValueArg<TValue, TPresent>;
120
+ /**
121
+ * Accept a value without its option name, such as `serve 5432` instead of `serve --port 5432`.
122
+ * Only one argument can accept positional values in each active command scope.
123
+ */
124
+ positional(): ValueArg<TValue, TPresent>;
125
+ /**
126
+ * Accept multiple values and return an array of converted values.
127
+ * Call this before `required()` or `default()`.
128
+ */
129
+ multiple(this: ValueArg<TValue, false>, options?: IMultipleOptions): ListArg<TValue, false>;
130
+ }
131
+ /** An option that accepts multiple values, such as `--tag a --tag b`. */
132
+ export declare class ListArg<TItem, TPresent extends boolean = false> extends Arg<TItem[], TPresent> {
133
+ constructor(state: IArgState);
134
+ /**
135
+ * Require the user to supply at least one value for this result property.
136
+ * Removes any previous default.
137
+ */
138
+ required(): ListArg<TItem, true>;
139
+ /**
140
+ * Use `value` at this result property when the user omits the option.
141
+ * Defaults bypass the converter. Replaces any previous requirement.
142
+ */
143
+ default(value: readonly TItem[]): ListArg<TItem, true>;
144
+ /** Show a custom value label in help, such as `--tag NAME`. */
145
+ label(label: string): ListArg<TItem, TPresent>;
146
+ /**
147
+ * Mask values in the editor, help defaults, command previews, and errors.
148
+ * Your program still receives the values, and clipboard copies include them.
149
+ * Sensitive values are never saved by `remember`.
150
+ */
151
+ sensitive(): ListArg<TItem, TPresent>;
152
+ /**
153
+ * Accept values without the option name, such as `seed users posts`.
154
+ * Only one argument can accept positional values in each active command scope.
155
+ */
156
+ positional(): ListArg<TItem, TPresent>;
157
+ }
158
+ /** A boolean flag that takes no value, such as `--verbose`. */
159
+ export declare class FlagArg<TPresent extends boolean = false> extends Arg<boolean, TPresent> {
160
+ constructor(state: IArgState);
161
+ /** Use `value` at this result property when the user omits the flag. */
162
+ default(value: boolean): FlagArg<true>;
163
+ /**
164
+ * Accept `--no-<name>` to set the flag to `false`.
165
+ * Requires a long option name. Applies to each long name unless that negated name
166
+ * already belongs to another option.
167
+ */
168
+ negatable(): FlagArg<TPresent>;
169
+ }
170
+ /** Type-only marker carrying a command's result shape and command line token. */
171
+ export declare const COMMAND_TYPES: unique symbol;
172
+ export interface ICommandTypes<TShape, TToken extends string> {
173
+ readonly shape: TShape;
174
+ readonly token: TToken;
175
+ }
176
+ /**
177
+ * Command descriptor created by `ta.command()`.
178
+ * When selected, its result property contains the command's parsed arguments.
179
+ * The property is absent when another command is selected.
180
+ */
181
+ export declare class Command<TShape extends IGroupShape = IGroupShape, TToken extends string = string> {
182
+ readonly [COMMAND_TYPES]: ICommandTypes<TShape, TToken>;
183
+ constructor(state: ICommandState);
184
+ }
185
+ /** Type-only marker that distinguishes metadata descriptors from plain groups. */
186
+ export declare const META_TYPES: unique symbol;
187
+ /**
188
+ * Descriptor for run metadata in the parsed result.
189
+ * Create one with `ta.meta()` and place it where you want `{ command, interactive }` to appear.
190
+ */
191
+ export declare class Meta {
192
+ readonly [META_TYPES]: true;
193
+ constructor();
194
+ }
195
+ export declare function isMeta(value: unknown): value is Meta;
196
+ /** Run metadata at a result property defined with `ta.meta()`. */
197
+ export interface IMeta<TCommandToken extends string = string> {
198
+ /** Selected command token, such as `serve`, or `null` when running without a command. */
199
+ command: TCommandToken | null;
200
+ /** `true` when arguments were assembled in interactive mode, otherwise `false`. */
201
+ interactive: boolean;
202
+ }
203
+ /**
204
+ * Arguments and metadata grouped under one result property.
205
+ * Groups are plain objects and exist even when all their optional arguments are omitted.
206
+ */
207
+ export interface IGroupShape {
208
+ readonly [key: string]: Arg<unknown, boolean> | Meta | IGroupShape;
209
+ /** Definition properties must use string keys. Symbol keys are not supported. */
210
+ readonly [key: symbol]: never;
211
+ }
212
+ /** Definition callback result containing arguments, metadata, groups, and top-level commands. */
213
+ export interface IShape {
214
+ readonly [key: string]: Arg<unknown, boolean> | Meta | Command<IGroupShape, string> | IGroupShape;
215
+ readonly [key: symbol]: never;
216
+ }
217
+ export type IEmptyShape = Record<never, never>;
218
+ type IIsGuaranteed<TNode> = TNode extends Arg<unknown, infer TPresent> ? TPresent : TNode extends Command<IGroupShape, string> ? false : true;
219
+ type IParsedNode<TNode, TCommandToken extends string> = TNode extends Arg<infer TValue, boolean> ? TValue : TNode extends Meta ? IMeta<TCommandToken> : TNode extends Command<infer TShape, string> ? IParsedScope<TShape, TCommandToken> : TNode extends IGroupShape ? IParsedScope<TNode, TCommandToken> : never;
220
+ /** Parsed type for one definition object, with the program's command tokens available to metadata. */
221
+ type IParsedScope<TShape, TCommandToken extends string> = ISimplify<{
222
+ -readonly [K in keyof TShape as IIsGuaranteed<TShape[K]> extends true ? K : never]: IParsedNode<TShape[K], TCommandToken>;
223
+ } & {
224
+ -readonly [K in keyof TShape as IIsGuaranteed<TShape[K]> extends true ? never : K]?: IParsedNode<TShape[K], TCommandToken>;
225
+ }>;
226
+ /**
227
+ * Parsed result type inferred from a definition.
228
+ * Within each active scope, required and defaulted arguments, groups, and metadata are guaranteed.
229
+ * Other arguments and command properties are optional.
230
+ */
231
+ export type IParsed<TShape> = IParsedScope<TShape, ICommandTokens<TShape>>;
232
+ /** Union of the command line tokens defined by a shape. */
233
+ export type ICommandTokens<TShape> = {
234
+ [K in keyof TShape]: TShape[K] extends Command<IGroupShape, infer TToken> ? TToken : never;
235
+ }[keyof TShape];
236
+ type IGuidedGroupNode<TValue> = Arg<TValue, boolean> | (TValue extends IMeta ? Meta : never) | (TValue extends readonly unknown[] ? never : TValue extends (...args: never[]) => unknown ? never : TValue extends object ? IGuidedGroup<TValue> & IGroupShape : never);
237
+ /** Check that a group defines every property of a guide type, including nested and optional properties. */
238
+ export type IGuidedGroup<TGuide> = {
239
+ readonly [K in keyof TGuide]-?: IGuidedGroupNode<Exclude<TGuide[K], undefined>>;
240
+ };
241
+ type IGuidedShapeNode<TValue> = IGuidedGroupNode<TValue> | (TValue extends readonly unknown[] ? never : TValue extends (...args: never[]) => unknown ? never : TValue extends object ? Command<IGuidedGroup<TValue> & IGroupShape, string> : never);
242
+ /**
243
+ * Check that the definition can produce every property of a guide type, including optional properties.
244
+ * Object properties can use a group, a command, or an argument whose converter returns the object.
245
+ * Additional definition properties are allowed.
246
+ *
247
+ * Use `satisfies` on the callback result:
248
+ * `talentedArgs(options, ta => ({ ... } satisfies TalentedArgsGuidedShape<IMyArgs>))`.
249
+ * The parsed result type is inferred from the definition, rather than replaced by the guide type.
250
+ */
251
+ export type IGuidedShape<TGuide> = {
252
+ readonly [K in keyof TGuide]-?: IGuidedShapeNode<Exclude<TGuide[K], undefined>>;
253
+ } & IShape;
254
+ /**
255
+ * Type error returned for a converter that produces a promise.
256
+ * It cannot be used as an argument descriptor because parsing requires synchronous conversion.
257
+ */
258
+ export interface IAsyncConverterNotSupported {
259
+ readonly 'converters must return the value right away, promises are not supported': never;
260
+ }
261
+ /** Argument descriptor for a converter's return type, rejecting promise types. */
262
+ export type IConvertedArg<TValue> = [TValue] extends [never] ? ValueArg<TValue> : [TValue] extends [PromiseLike<unknown>] ? IAsyncConverterNotSupported : ValueArg<TValue>;
263
+ /**
264
+ * Builder passed to the definition callback.
265
+ * Use it to define arguments, flags, commands, and run metadata.
266
+ */
267
+ export interface IBuilder {
268
+ /**
269
+ * Define an option that accepts a string value.
270
+ * For `--port 8080`, the parsed value is `"8080"`.
271
+ */
272
+ arg(names: IOptionNames, description: string): ValueArg<string>;
273
+ /**
274
+ * Define an option that converts its string value using `convert`.
275
+ * With `Number`, `--port 8080` produces the number `8080`.
276
+ * You can also provide a synchronous function for custom conversion.
277
+ * Throw an error from the converter to reject a value.
278
+ */
279
+ arg<TValue>(names: IOptionNames, description: string, convert: IConverter<TValue>): IConvertedArg<TValue>;
280
+ /**
281
+ * Define a boolean flag that is `true` when supplied.
282
+ * When omitted, its property is absent.
283
+ * Use `ta.flag(...).default(false)` to get `false` when the flag is omitted.
284
+ */
285
+ flag(names: IOptionNames, description: string): FlagArg;
286
+ /**
287
+ * Define a command with no arguments of its own.
288
+ */
289
+ command<TToken extends string>(token: TToken, description: string | ICommandDescription): Command<IEmptyShape, TToken>;
290
+ /**
291
+ * Define a command with arguments described by the `shape` object.
292
+ * Options defined only here are available only when this command is selected.
293
+ * Reuse an argument or flag from outside the command to share the same option.
294
+ *
295
+ * @example
296
+ * ```typescript
297
+ * ta.command('backup', 'Back up the database', {
298
+ * location: ta.arg(['-l', '--location'], 'Where to store the backup'),
299
+ * });
300
+ * ```
301
+ */
302
+ command<TToken extends string, TShape extends IGroupShape>(token: TToken, description: string | ICommandDescription, shape: TShape): Command<TShape, TToken>;
303
+ /**
304
+ * Add `{ command, interactive }` at this position in the parsed result.
305
+ *
306
+ * - `command`: the selected command's `token`, or `null` when no command is selected.
307
+ * - `interactive`: `true` when interactive mode was used, otherwise `false`.
308
+ */
309
+ meta(): Meta;
310
+ }
311
+ /** Option names reserved for built-in help and interactive mode. */
312
+ export declare const RESERVED_NAMES: readonly string[];
313
+ export declare const RESERVED_ALIASES: readonly string[];
314
+ /**
315
+ * Convert numeric text to a finite number.
316
+ * Accepts decimal, exponent, hexadecimal, octal, and binary forms.
317
+ * Rejects empty or whitespace-only strings, `NaN`, and infinity.
318
+ */
319
+ export declare function convertNumber(value: string): number;
320
+ /** Create the builder passed to the definition callback. */
321
+ export declare function createBuilder(): IBuilder;
322
+ export {};