generated from githuhsername/simple-template-advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.types.ts
112 lines (110 loc) · 3.42 KB
/
spec.types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { argstree } from './argstree.js';
import { Node, NodeData, Options } from './core.types.js';
/** The spec options. */
export interface SpecOptions extends Omit<Options, 'alias' | 'args'> {}
/** The spec object. */
export interface Spec {
/**
* The raw argument to parse. This is the `arg` value
* from the {@linkcode option} or {@linkcode command} call.
*/
readonly id: string | null;
/** Depth of spec. */
readonly depth: number;
/**
* Add an option or command.
* @param arg The option or command to match.
* @param options The spec options.
* @returns `this` for chaining.
*/
option(arg: string, options?: SpecOptions): this;
/**
* Add an option or command.
*
* This is an alias for {@linkcode option} with {@linkcode args} setup:
* ```javascript
* spec.option(arg, options).spec(spec => spec.args());
* ```
* @param arg The option or command to match.
* @param options The spec options.
* @returns `this` for chaining.
*/
command(arg: string, options?: SpecOptions): this;
/**
* Add an alias for the current option or command.
*
* Requires a call to {@linkcode option} or {@linkcode command} beforehand
* to create the option or command. Otherwise, an error is thrown.
* @param alias The alias or list of aliases for the current option or command.
* @param args The alias arguments.
* @returns `this` for chaining.
*/
alias(alias: string | string[], args?: string | string[]): this;
/**
* Setup the options for the current option or command.
*
* Requires a call to {@linkcode option} or {@linkcode command} beforehand
* to create the option or command. Otherwise, an error is thrown.
* @param setup The setup callback with a new {@linkcode Spec} object.
* @returns `this` for chaining.
*/
spec(setup: (spec: Spec) => void): this;
/**
* List of aliases mapped to {@linkcode Options.args}.
* @param alias The alias options.
* @returns `this` for chaining.
*/
aliases(alias: Required<Options>['alias']): this;
/**
* Add an empty object to {@linkcode Options.args}.
* @returns `this` for chaining.
*/
args(): this;
/**
* Add an {@linkcode Options.args} function.
* Additional calls will replace the existing {@linkcode handler}.
* @param handler The `args` function.
* @param data The node data.
* @returns `this` for chaining.
*/
args(
handler: (arg: string, data: NodeData) => Options | null | undefined
): this;
/**
* Get the parse options for this spec.
* @returns The parse options.
*/
options(): Options;
/**
* Parse arguments into a tree structure.
*
* This is an alias for {@linkcode argstree} call:
* ```javascript
* argstree(args, spec.options());
* ```
* @param args The arguments to parse.
* @returns The node object.
*/
parse(args: readonly string[]): Node;
/**
* Get the parent spec object. If `null`, then this is the root spec.
* @returns The parent spec object.
*/
parent(): Spec | null;
/**
* Get the children spec objects.
* @returns The children spec objects.
*/
children(): Spec[];
/**
* Get the ancestor spec objects starting from the root spec to the parent spec.
* @returns The descendant spec objects.
*/
ancestors(): Spec[];
/**
* Get the descendant spec objects.
* @returns The descendant spec objects.
*/
descendants(): Spec[];
}