massarg 1.0.7-pre.1 → 2.0.0-pre.1
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 +126 -209
- package/command.d.ts +153 -0
- package/command.d.ts.map +1 -0
- package/command.js +317 -0
- package/command.js.map +1 -0
- package/error.d.ts +34 -0
- package/error.d.ts.map +1 -0
- package/error.js +37 -0
- package/error.js.map +1 -0
- package/example.d.ts +31 -0
- package/example.d.ts.map +1 -0
- package/example.js +41 -0
- package/example.js.map +1 -0
- package/help.d.ts +729 -0
- package/help.d.ts.map +1 -0
- package/help.js +253 -0
- package/help.js.map +1 -0
- package/index.d.ts +1 -70
- package/index.d.ts.map +1 -1
- package/index.js +13 -688
- package/index.js.map +1 -1
- package/massarg.d.ts +8 -0
- package/massarg.d.ts.map +1 -0
- package/massarg.js +23 -0
- package/massarg.js.map +1 -0
- package/option.d.ts +241 -0
- package/option.d.ts.map +1 -0
- package/option.js +305 -0
- package/option.js.map +1 -0
- package/package.json +22 -15
- package/style.d.ts +45 -0
- package/style.d.ts.map +1 -0
- package/style.js +52 -0
- package/style.js.map +1 -0
- package/utils.d.ts +33 -13
- package/utils.d.ts.map +1 -1
- package/utils.js +97 -110
- package/utils.js.map +1 -1
- package/assertions.d.ts +0 -8
- package/assertions.d.ts.map +0 -1
- package/assertions.js +0 -145
- package/assertions.js.map +0 -1
- package/errors.d.ts +0 -7
- package/errors.d.ts.map +0 -1
- package/errors.js +0 -33
- package/errors.js.map +0 -1
- package/types.d.ts +0 -224
- package/types.d.ts.map +0 -1
- package/types.js +0 -3
- package/types.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,70 +1,19 @@
|
|
|
1
|
-
#
|
|
1
|
+
# massarg
|
|
2
2
|
|
|
3
|
-
Massarg is a
|
|
3
|
+
Massarg is a beautiful, flexible, powerful, and simple-to-use command/argument parser for JS
|
|
4
4
|
applications, allowing you to create complex but easy applications that consume command-line
|
|
5
5
|
arguments and commands.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
- [Importing](#importing)
|
|
18
|
-
- [Using](#using)
|
|
19
|
-
- [Main command](#main-command)
|
|
20
|
-
- [Example](#example)
|
|
21
|
-
- [JS/TS](#jsts)
|
|
22
|
-
- [Shell](#shell)
|
|
23
|
-
- [Commands](#commands)
|
|
24
|
-
- [Options](#options)
|
|
25
|
-
- [Example](#example-1)
|
|
26
|
-
- [JS/TS](#jsts-1)
|
|
27
|
-
- [Shell](#shell-1)
|
|
28
|
-
- [Options](#options-1)
|
|
29
|
-
- [Options](#options-2)
|
|
30
|
-
- [Example](#example-2)
|
|
31
|
-
- [JS/TS](#jsts-2)
|
|
32
|
-
- [Shell](#shell-2)
|
|
33
|
-
- [Example Lines](#example-lines)
|
|
34
|
-
- [Options](#options-3)
|
|
35
|
-
- [Example](#example-3)
|
|
36
|
-
- [JS/TS](#jsts-3)
|
|
37
|
-
- [Help/Usage Command](#helpusage-command)
|
|
38
|
-
- [Options](#options-4)
|
|
39
|
-
- [Example](#example-4)
|
|
40
|
-
- [JS/TS](#jsts-4)
|
|
41
|
-
- [Shell output](#shell-output)
|
|
42
|
-
- [Contributing](#contributing)
|
|
43
|
-
<!-- - [Example](#example) -->
|
|
44
|
-
<!-- - [JS/TS](#jsts) -->
|
|
45
|
-
<!-- - [Shell](#shell) -->
|
|
46
|
-
- [Commands](#commands)
|
|
47
|
-
- [Options](#options)
|
|
48
|
-
- [Example](#example-1)
|
|
49
|
-
- [JS/TS](#jsts-1)
|
|
50
|
-
- [Shell](#shell-1)
|
|
51
|
-
- [Options](#options-1)
|
|
52
|
-
- [Options](#options-2)
|
|
53
|
-
- [Example](#example-2)
|
|
54
|
-
- [JS/TS](#jsts-2)
|
|
55
|
-
- [Shell](#shell-2)
|
|
56
|
-
- [Example Lines](#example-lines)
|
|
57
|
-
- [Options](#options-3)
|
|
58
|
-
- [Example](#example-3)
|
|
59
|
-
- [JS/TS](#jsts-3)
|
|
60
|
-
- [Help/Usage Command](#helpusage-command)
|
|
61
|
-
- [Options](#options-4)
|
|
62
|
-
- [Example](#example-4)
|
|
63
|
-
- [JS/TS](#jsts-4)
|
|
64
|
-
- [Shell output](#shell-output)
|
|
65
|
-
- [Contributing](#contributing)
|
|
66
|
-
|
|
67
|
-
</details>
|
|
7
|
+
It allows you to both parse argument options and flags, as well as hierarchal subcommands, both of
|
|
8
|
+
which can be parsed into an automatic help command or flag that displays all the information easily,
|
|
9
|
+
with customizable styles, and content.
|
|
10
|
+
|
|
11
|
+
You should only focus on actually writing the functionality of your CLI, and not waste it on writing
|
|
12
|
+
a way to parse the chain of commands, flags or options.
|
|
13
|
+
|
|
14
|
+
And it should look good too, right?
|
|
15
|
+
|
|
16
|
+

|
|
68
17
|
|
|
69
18
|
## Features
|
|
70
19
|
|
|
@@ -87,23 +36,24 @@ Yes, there are a lot of arg parsers. But hear us out.
|
|
|
87
36
|
|
|
88
37
|
## Quick Start
|
|
89
38
|
|
|
90
|
-
###
|
|
39
|
+
### Install
|
|
91
40
|
|
|
92
41
|
```shell
|
|
42
|
+
# pnpm
|
|
43
|
+
pnpm install massarg
|
|
93
44
|
# npm
|
|
94
|
-
npm install
|
|
45
|
+
npm install massarg
|
|
95
46
|
# yarn
|
|
96
47
|
yarn add massarg
|
|
97
48
|
```
|
|
98
49
|
|
|
99
|
-
###
|
|
50
|
+
### Import
|
|
100
51
|
|
|
101
|
-
```
|
|
102
|
-
import massarg from
|
|
103
|
-
import { Massarg } from "massarg" // import class
|
|
52
|
+
```ts
|
|
53
|
+
import massarg from 'massarg'
|
|
104
54
|
```
|
|
105
55
|
|
|
106
|
-
###
|
|
56
|
+
### Usage
|
|
107
57
|
|
|
108
58
|
Call the default export function `massarg`, or create a new instance manually using `new Massarg()`,
|
|
109
59
|
and then you can start chaining commands. Use `.parse()` to do the final parsing and run the
|
|
@@ -112,30 +62,53 @@ commands and options.
|
|
|
112
62
|
Here is an example with some commonly used examples to get you started. Keep reading for a complete
|
|
113
63
|
documentation of every option.
|
|
114
64
|
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
|
|
65
|
+
```ts
|
|
66
|
+
const parser = massarg({
|
|
67
|
+
name: 'my-cli',
|
|
68
|
+
description: "Does really amazing stuff, you wouldn't believe!",
|
|
69
|
+
}) // or: new Massarg()
|
|
70
|
+
.main((options) => console.log('main command', options))
|
|
118
71
|
.command({
|
|
119
|
-
name:
|
|
120
|
-
description:
|
|
121
|
-
aliases: [
|
|
122
|
-
run: (options) => console.log(
|
|
72
|
+
name: 'foo',
|
|
73
|
+
description: 'a sub command',
|
|
74
|
+
aliases: ['f'],
|
|
75
|
+
run: (options) => console.log('foo command'),
|
|
123
76
|
})
|
|
77
|
+
.command(
|
|
78
|
+
massarg({
|
|
79
|
+
name: 'bar',
|
|
80
|
+
description: 'another sub command',
|
|
81
|
+
aliases: ['s'],
|
|
82
|
+
run: (options) => console.log('bar command', options),
|
|
83
|
+
}).option({
|
|
84
|
+
name: 'file',
|
|
85
|
+
description: 'Filename to use',
|
|
86
|
+
aliases: ['f'],
|
|
87
|
+
parse: (filename) => path.resolve(process.cwd(), filename),
|
|
88
|
+
}),
|
|
89
|
+
)
|
|
124
90
|
.option({
|
|
125
|
-
name:
|
|
126
|
-
description:
|
|
127
|
-
aliases: [
|
|
128
|
-
boolean: true,
|
|
91
|
+
name: 'my-string',
|
|
92
|
+
description: 'A string argument',
|
|
93
|
+
aliases: ['s'],
|
|
129
94
|
})
|
|
130
|
-
.
|
|
131
|
-
name:
|
|
132
|
-
description:
|
|
133
|
-
|
|
134
|
-
|
|
95
|
+
.flag({
|
|
96
|
+
name: 'flag',
|
|
97
|
+
description: 'a flag that will be related to any command (main or sub)',
|
|
98
|
+
aliases: ['f'],
|
|
99
|
+
})
|
|
100
|
+
.example({
|
|
101
|
+
description: 'Run the sub command',
|
|
102
|
+
input: 'my-bin --flag sub',
|
|
103
|
+
output: 'Sub command: flag is true',
|
|
135
104
|
})
|
|
136
105
|
.help({
|
|
137
|
-
|
|
138
|
-
|
|
106
|
+
bindCommand: true,
|
|
107
|
+
footerText: `Copyright © ${new Date().getFullYear()} Me, Myself and I`,
|
|
108
|
+
titleStyle: {
|
|
109
|
+
bold: true,
|
|
110
|
+
color: 'brightWhite',
|
|
111
|
+
},
|
|
139
112
|
})
|
|
140
113
|
```
|
|
141
114
|
|
|
@@ -143,13 +116,15 @@ massarg() // or: new Massarg()
|
|
|
143
116
|
|
|
144
117
|
The main command is the one that runs when you supply no other commands.
|
|
145
118
|
|
|
119
|
+
If no command is specified, and no main command is present, the help usage is automatically printed.
|
|
120
|
+
|
|
146
121
|
### Example
|
|
147
122
|
|
|
148
123
|
#### JS/TS
|
|
149
124
|
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
console.log(
|
|
125
|
+
```ts
|
|
126
|
+
parser.main((options) => {
|
|
127
|
+
console.log('Parsed options:', options)
|
|
153
128
|
// do stuff
|
|
154
129
|
})
|
|
155
130
|
```
|
|
@@ -161,38 +136,38 @@ $ ./mybin
|
|
|
161
136
|
# Main command runs without options
|
|
162
137
|
|
|
163
138
|
$ ./mybin --my-string "Some string"
|
|
164
|
-
# Main command runs with
|
|
139
|
+
# Main command runs with options { myString: "Some string" }
|
|
140
|
+
|
|
141
|
+
$ ./mybin foo
|
|
142
|
+
# Foo sub command run with options {}
|
|
165
143
|
```
|
|
166
144
|
|
|
167
145
|
## Commands
|
|
168
146
|
|
|
169
147
|
Commands are activated when their keyword is included in the args. The first command that matches
|
|
170
|
-
will be executed, skipping the rest. Options will
|
|
171
|
-
|
|
172
|
-
Any arguments that are not taken by options or commands, are automatically passed to
|
|
173
|
-
`options.extra`, which you can access when running a command or when using the return value from
|
|
174
|
-
`parseArgs()`.
|
|
148
|
+
will be executed, skipping the rest. Options before will be parsed on the main parser, while
|
|
149
|
+
anything after the command will be parsed for that subcommand only.
|
|
175
150
|
|
|
176
151
|
### Options
|
|
177
152
|
|
|
178
|
-
| Name
|
|
179
|
-
|
|
|
180
|
-
| `name
|
|
181
|
-
| `aliases`
|
|
182
|
-
| `description`
|
|
183
|
-
| `run
|
|
153
|
+
| Name | Type | Required | Example | Description |
|
|
154
|
+
| ------------- | ----------------------------------- | -------- | ------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
|
|
155
|
+
| `name` | `string` | ✅ | `"my-command"` | The name of the command, which will be used in the CLI to trigger it |
|
|
156
|
+
| `aliases` | `string[]` | | `["m", "mc"]` | Alternate names for the command, available for use in addition to `name` |
|
|
157
|
+
| `description` | `string` | | `"Description of the command"` | Description for the command, only displayed with `--help` or `printHelp()` |
|
|
158
|
+
| `run` | `function(options, parser) => void` | ✅ | `(options) => console.log("my-command", options)` | Main function that runs this command. The supplied argument is the options passed via the CLI and parsed by massarg. |
|
|
184
159
|
|
|
185
160
|
### Example
|
|
186
161
|
|
|
187
162
|
#### JS/TS
|
|
188
163
|
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
name:
|
|
192
|
-
description:
|
|
193
|
-
aliases: [
|
|
164
|
+
```ts
|
|
165
|
+
parser.command({
|
|
166
|
+
name: 'do-something',
|
|
167
|
+
description: 'This command does something',
|
|
168
|
+
aliases: ['do', 'd'],
|
|
194
169
|
run: (options) => {
|
|
195
|
-
console.log(
|
|
170
|
+
console.log('Parsed options:', options)
|
|
196
171
|
// do stuff
|
|
197
172
|
},
|
|
198
173
|
})
|
|
@@ -211,57 +186,35 @@ $ ./mybin my-command --my-string "Some string"
|
|
|
211
186
|
## Options
|
|
212
187
|
|
|
213
188
|
Options are variables you can accept via CLI and parse to use in your commands, e.g. `--my-bool`,
|
|
214
|
-
`--
|
|
189
|
+
`--my-string string`, `--my-number 1`.
|
|
215
190
|
|
|
216
|
-
|
|
217
|
-
`options.extra`, which you can access when running a command or when using the return value from
|
|
218
|
-
`parseArgs()`.
|
|
191
|
+
Aliases use the shorthand syntax, as such: `-s string`, `-n 1`.
|
|
219
192
|
|
|
220
193
|
### Options
|
|
221
194
|
|
|
222
|
-
| Name
|
|
223
|
-
|
|
|
224
|
-
| `name
|
|
225
|
-
| `aliases`
|
|
226
|
-
| `description`
|
|
227
|
-
| `parse`
|
|
228
|
-
| `isDefault`
|
|
229
|
-
| `
|
|
230
|
-
| `
|
|
231
|
-
| `required` (default: `false`) | `boolean` | | When an option is required, parsing will throw a `RequiredError` if it was not given a proper value. If it is attached to a specific (or several) commands, it will only throw if the relevant command was used. |
|
|
232
|
-
|
|
233
|
-
\* In boolean args, truthy values are considered true if they fit one of the following values: `1`,
|
|
234
|
-
`true`, `yes`, `on`. Anything else is considered falsy.
|
|
235
|
-
|
|
236
|
-
\*\* Boolean args will also have special negation flags that allow passing a `false` value by using
|
|
237
|
-
a variation of the name or alias as a flag and still without supplying a value. The following will
|
|
238
|
-
work for true, given `name: 'bool', aliases: ['b']`: `--bool`, `-b`, and the following will work for
|
|
239
|
-
`false`: `--no-bool`, `-!b`. `--bool` and `-b` may still be supplied a truthy/falsy value separately
|
|
240
|
-
in order to determine their value. Note it might cause unpredicted behavior when combining with
|
|
241
|
-
options that have default values that are passed directly afterwards.
|
|
195
|
+
| Name | Type | Required | Default | Example | Description |
|
|
196
|
+
| ------------- | --------------------------------- | -------- | ------------------ | ------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
197
|
+
| `name` | `string` | ✅ | | `"my-number"` | The name of the option, which will be used in the CLI to apply it |
|
|
198
|
+
| `aliases` | `string[]` | | | `["n"]` | Alternate names for the option, available for use in addition to `name` |
|
|
199
|
+
| `description` | `string` | | | `"Description of the command"` | Description for the command, only displayed with `--help` or `printHelp()` |
|
|
200
|
+
| `parse` | `function(value, options) => any` | | `(s) => String(s)` | `(value, options) => parseInt(value)` | Function that parses this option. The supplied arguments are the string value from the arg, and other options passed via the CLI and parsed by massarg before this one. Not all options will be available. |
|
|
201
|
+
| `isDefault` | `boolean` | | `false` | | When `true`, any args placed without name will be applied to this option. When more than one arg is supplied this way, only the last given will be used (unless the option is an array type). |
|
|
202
|
+
| `array` | `boolean` | | `false` | | When set to true, you will be able to take multiple values when using the same option more than once. They will all be parsed properly and put into an array. |
|
|
203
|
+
| `required` | `boolean` | | `false` | | When an option is required, parsing will throw a `RequiredError` if it was not given a proper value. If it is attached to a specific (or several) commands, it will only throw if the relevant command was used. |
|
|
242
204
|
|
|
243
205
|
### Example
|
|
244
206
|
|
|
245
207
|
#### JS/TS
|
|
246
208
|
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
})
|
|
257
|
-
.option({
|
|
258
|
-
name: "number",
|
|
259
|
-
aliases: ["n"],
|
|
260
|
-
description: "This is a number arg, if you include this option, you must supply it with a numeric value.",
|
|
261
|
-
defaultValue: 0,
|
|
262
|
-
commands: "do",
|
|
263
|
-
parse: (v) => parseInt(v),
|
|
264
|
-
})
|
|
209
|
+
```ts
|
|
210
|
+
parser.option({
|
|
211
|
+
name: 'number',
|
|
212
|
+
aliases: ['n'],
|
|
213
|
+
description:
|
|
214
|
+
'This is a number arg, if you include this option, you must supply it with a numeric value.',
|
|
215
|
+
defaultValue: 0,
|
|
216
|
+
parse: (v) => parseInt(v),
|
|
217
|
+
})
|
|
265
218
|
```
|
|
266
219
|
|
|
267
220
|
#### Shell
|
|
@@ -284,21 +237,21 @@ atop as titles, if specified.
|
|
|
284
237
|
|
|
285
238
|
### Options
|
|
286
239
|
|
|
287
|
-
| Name
|
|
288
|
-
|
|
|
289
|
-
| `input
|
|
290
|
-
| `output`
|
|
291
|
-
| `description`
|
|
240
|
+
| Name | Type | Required | Default | Example | Description |
|
|
241
|
+
| ------------- | -------- | -------- | ------- | ----------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
242
|
+
| `input` | `string` | ✅ | | `"my-cmd --number 10"` | The input line, an example of user input that will be displayed as "shell" commands. The prefix is customizable through the `help()` options. |
|
|
243
|
+
| `output` | `string` | ✅ | | `"you entered my-cmd with the number 10, which is larger than 5"` | The output line, an example of the command's output that will be displayed as "shell" output. The prefix is customizable through the `help()` options. |
|
|
244
|
+
| `description` | `string` | | | `"Run the my-cmd command with a number parameter"` | An explanation of the input/output that will be display as a title above the input if specified. |
|
|
292
245
|
|
|
293
246
|
### Example
|
|
294
247
|
|
|
295
248
|
#### JS/TS
|
|
296
249
|
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
input:
|
|
300
|
-
output:
|
|
301
|
-
description:
|
|
250
|
+
```ts
|
|
251
|
+
parser.example({
|
|
252
|
+
input: 'my-cmd --number 10',
|
|
253
|
+
output: 'you entered my-cmd with the number 10, which is larger than 5',
|
|
254
|
+
description: 'Run the my-cmd command with a number parameter',
|
|
302
255
|
})
|
|
303
256
|
```
|
|
304
257
|
|
|
@@ -331,57 +284,21 @@ you may override their defaults to modify the behavior.
|
|
|
331
284
|
|
|
332
285
|
#### JS/TS
|
|
333
286
|
|
|
334
|
-
```
|
|
335
|
-
|
|
287
|
+
```ts
|
|
288
|
+
parser.help({
|
|
336
289
|
printWidth: 80,
|
|
337
|
-
binName:
|
|
338
|
-
normalColors:
|
|
339
|
-
highlightColors:
|
|
340
|
-
titleColors:
|
|
341
|
-
subtitleColors: [
|
|
342
|
-
header:
|
|
343
|
-
footer:
|
|
344
|
-
commandNameSeparator:
|
|
345
|
-
optionNameSeparator:
|
|
290
|
+
binName: 'my-app',
|
|
291
|
+
normalColors: 'dim',
|
|
292
|
+
highlightColors: 'yellow',
|
|
293
|
+
titleColors: 'white',
|
|
294
|
+
subtitleColors: ['bold', 'dim'],
|
|
295
|
+
header: 'Header text',
|
|
296
|
+
footer: 'Footer text',
|
|
297
|
+
commandNameSeparator: ' | ',
|
|
298
|
+
optionNameSeparator: '|',
|
|
346
299
|
useGlobalColumns: true,
|
|
347
|
-
usageExample:
|
|
300
|
+
usageExample: 'command [option]',
|
|
348
301
|
})
|
|
349
302
|
```
|
|
350
303
|
|
|
351
304
|
#### Shell output
|
|
352
|
-
|
|
353
|
-

|
|
354
|
-
|
|
355
|
-
## Contributing
|
|
356
|
-
|
|
357
|
-
I am developing this package on my free time, so any support, whether code, issues, or just stars
|
|
358
|
-
is very helpful to sustaining its life. If you would like to donate a bit to help keep the project
|
|
359
|
-
alive, I would be very thankful!
|
|
360
|
-
|
|
361
|
-
<a href='https://ko-fi.com/casraf' target='_blank'>
|
|
362
|
-
<img height='36' style='border:0px;height:36px;'
|
|
363
|
-
src='https://cdn.ko-fi.com/cdn/kofi1.png?v=3'
|
|
364
|
-
alt='Buy Me a Coffee at ko-fi.com' />
|
|
365
|
-
</a>
|
|
366
|
-
|
|
367
|
-
I welcome any issues or pull requests on GitHub. If you find a bug, or would like a new feature,
|
|
368
|
-
don't hesitate to open an appropriate issue and I will do my best to reply promptly.
|
|
369
|
-
|
|
370
|
-
If you are a developer and want to contribute code, here are some starting tips:
|
|
371
|
-
|
|
372
|
-
1. Fork this repository
|
|
373
|
-
2. Run `yarn install`
|
|
374
|
-
3. Run `yarn develop` to start file watch mode
|
|
375
|
-
4. Make any changes you would like
|
|
376
|
-
5. Create tests for your changes
|
|
377
|
-
6. Update the relevant documentation (readme, code comments, type comments)
|
|
378
|
-
7. Create a PR on upstream
|
|
379
|
-
|
|
380
|
-
Some tips on getting around the code:
|
|
381
|
-
|
|
382
|
-
- Use `yarn dev` for development - it runs TypeScript compile in watch mode, allowing you to make
|
|
383
|
-
changes and immediately be able to try them using `yarn cmd`.
|
|
384
|
-
- Use `yarn build` to build the output
|
|
385
|
-
- Use `yarn test` to run tests
|
|
386
|
-
- While `yarn develop` is running, you may run `yarn sample` (try running with `-h` flag) to see
|
|
387
|
-
a sample script using massarg, that logs the parsed options with the correct parsers and runners.
|
package/command.d.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { HelpConfig } from './help';
|
|
3
|
+
import { MassargOption, MassargFlag, OptionConfig, TypedOptionConfig } from './option';
|
|
4
|
+
import { DeepRequired } from './utils';
|
|
5
|
+
import { MassargExample, ExampleConfig } from './example';
|
|
6
|
+
export declare const CommandConfig: <RunArgs extends z.ZodType<any, z.ZodTypeDef, any>>(args: RunArgs) => z.ZodObject<{
|
|
7
|
+
/** Command name */
|
|
8
|
+
name: z.ZodString;
|
|
9
|
+
/** Command description, displayed in the help output */
|
|
10
|
+
description: z.ZodString;
|
|
11
|
+
/** Command aliases */
|
|
12
|
+
aliases: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
13
|
+
/**
|
|
14
|
+
* Function used when invoking this command. It receives the parsed options and the primary
|
|
15
|
+
* instance of Massarg used to invoke this command (the top-level instance)
|
|
16
|
+
*/
|
|
17
|
+
run: z.ZodType<Runner<z.TypeOf<RunArgs>>, z.ZodTypeDef, Runner<z.TypeOf<RunArgs>>>;
|
|
18
|
+
}, "strip", z.ZodTypeAny, {
|
|
19
|
+
description: string;
|
|
20
|
+
name: string;
|
|
21
|
+
run: Runner<z.TypeOf<RunArgs>>;
|
|
22
|
+
aliases?: string[] | undefined;
|
|
23
|
+
}, {
|
|
24
|
+
description: string;
|
|
25
|
+
name: string;
|
|
26
|
+
run: Runner<z.TypeOf<RunArgs>>;
|
|
27
|
+
aliases?: string[] | undefined;
|
|
28
|
+
}>;
|
|
29
|
+
export type CommandConfig<T = unknown> = z.infer<ReturnType<typeof CommandConfig<z.ZodType<T>>>>;
|
|
30
|
+
export type ArgsObject = Record<string, unknown>;
|
|
31
|
+
export type Runner<Args extends ArgsObject> = <A extends ArgsObject = Args>(options: A, instance: MassargCommand<A>) => Promise<void> | void;
|
|
32
|
+
/**
|
|
33
|
+
* A command is a named function that can be invoked with a set of options.
|
|
34
|
+
*
|
|
35
|
+
* Commands can have sub-commands, which can have their own sub-commands, and so on.
|
|
36
|
+
*
|
|
37
|
+
* Options are not inherited by sub-commands, but their parsed values are passed down when
|
|
38
|
+
* invoking a sub-command. This works recursively.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* massarg(options).command({
|
|
43
|
+
* name: 'foo',
|
|
44
|
+
* description: 'foo command',
|
|
45
|
+
* run: (options, instance) => {
|
|
46
|
+
* console.log(options, instance)
|
|
47
|
+
* },
|
|
48
|
+
* })
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare class MassargCommand<Args extends ArgsObject = ArgsObject> {
|
|
52
|
+
name: string;
|
|
53
|
+
description: string;
|
|
54
|
+
aliases: string[];
|
|
55
|
+
private _run?;
|
|
56
|
+
commands: MassargCommand<any>[];
|
|
57
|
+
options: MassargOption[];
|
|
58
|
+
examples: MassargExample[];
|
|
59
|
+
args: Partial<Args>;
|
|
60
|
+
private _helpConfig;
|
|
61
|
+
parent?: MassargCommand<any>;
|
|
62
|
+
constructor(options: CommandConfig<Args>, parent?: MassargCommand<any>);
|
|
63
|
+
get helpConfig(): DeepRequired<HelpConfig>;
|
|
64
|
+
/**
|
|
65
|
+
* Add a sub-command to this command.
|
|
66
|
+
*
|
|
67
|
+
* The sub-command will inherit all help configuration from the parent commands,
|
|
68
|
+
* all the way up to the top-level command.
|
|
69
|
+
*
|
|
70
|
+
* While options are not inherited, they will be passed from any parent commands
|
|
71
|
+
* to the sub-command when invoked.
|
|
72
|
+
*/
|
|
73
|
+
command<A extends ArgsObject = Args>(config: CommandConfig<A>): MassargCommand<Args>;
|
|
74
|
+
command<A extends ArgsObject = Args>(config: MassargCommand<A>): MassargCommand<Args>;
|
|
75
|
+
/**
|
|
76
|
+
* Adds a flag to this command.
|
|
77
|
+
*
|
|
78
|
+
* A flag is an option that is either present or not. It can be used to toggle
|
|
79
|
+
* a boolean value, or to indicate that a command should be run in a different
|
|
80
|
+
* mode.
|
|
81
|
+
*
|
|
82
|
+
* A flag can be negated by prefixing it with `no-`. For example, `--no-verbose`,
|
|
83
|
+
* or by prefixing the alias with `^` instead of `-`. This is configurable via the command's
|
|
84
|
+
* configuration.
|
|
85
|
+
*/
|
|
86
|
+
flag(config: Omit<OptionConfig<boolean>, 'parse' | 'isDefault'>): MassargCommand<Args>;
|
|
87
|
+
flag(config: MassargFlag): MassargCommand<Args>;
|
|
88
|
+
/**
|
|
89
|
+
* Adds an option to this command.
|
|
90
|
+
*
|
|
91
|
+
* An option is a named value that can be passed to a command. It can be
|
|
92
|
+
* required or optional, and can be of any type.
|
|
93
|
+
*
|
|
94
|
+
* You can specify a default value for an option, which will be used if the
|
|
95
|
+
* option is not passed to the command.
|
|
96
|
+
*
|
|
97
|
+
* You can also specify a parse function, which will be used to parse the
|
|
98
|
+
* value passed to the command. This is useful if you want to parse a string
|
|
99
|
+
* into a more complex type, or if you want to validate the value.
|
|
100
|
+
*/
|
|
101
|
+
option<T = string>(config: MassargOption<T>): MassargCommand<Args>;
|
|
102
|
+
option<T = string>(config: TypedOptionConfig<T>): MassargCommand<Args>;
|
|
103
|
+
/**
|
|
104
|
+
* Adds an example to this command.
|
|
105
|
+
*
|
|
106
|
+
* An example is a description of how to use the command, with an example input and output.
|
|
107
|
+
*
|
|
108
|
+
* At least one of `description`, `input` or `output` must be provided, but neither alone is
|
|
109
|
+
* required.
|
|
110
|
+
*/
|
|
111
|
+
example(config: ExampleConfig): MassargCommand<Args>;
|
|
112
|
+
/**
|
|
113
|
+
* Configure the help output for this (and all child) commands.
|
|
114
|
+
*
|
|
115
|
+
* You can automatically bind the help command to this command, and/or bind the help option
|
|
116
|
+
* to this command.
|
|
117
|
+
*
|
|
118
|
+
* If you don't opt-in to this behavior with `bindCommand` or `bindOption`, you can still
|
|
119
|
+
* access the help output via `this.helpString()` and `this.printHelp()`.
|
|
120
|
+
*/
|
|
121
|
+
help(config: HelpConfig): MassargCommand<Args>;
|
|
122
|
+
/**
|
|
123
|
+
* Configure the main function for this command. This command will run when no sub-commands
|
|
124
|
+
* are provided.
|
|
125
|
+
*
|
|
126
|
+
* If none is provided, help will be printed.
|
|
127
|
+
*/
|
|
128
|
+
main<A extends ArgsObject = Args>(run: Runner<A>): MassargCommand<Args>;
|
|
129
|
+
/**
|
|
130
|
+
* Parse the given arguments and run the command or sub-commands along with the given options
|
|
131
|
+
* and flags.
|
|
132
|
+
*
|
|
133
|
+
* To parse the arguments without running any commands and only get the output args,
|
|
134
|
+
* use `getArgs` instead.
|
|
135
|
+
*/
|
|
136
|
+
parse(argv: string[], args?: Partial<Args>, parent?: MassargCommand<Args>): Promise<void> | void;
|
|
137
|
+
private parseOption;
|
|
138
|
+
/** Parse the given arguments and return the output args. */
|
|
139
|
+
getArgs(argv: string[], __args?: Partial<Args>, parent?: MassargCommand<any>, parseCommands?: false): Promise<void> | void;
|
|
140
|
+
getArgs(argv: string[], __args?: Partial<Args>, parent?: MassargCommand<any>, parseCommands?: true): Args;
|
|
141
|
+
/**
|
|
142
|
+
* Generate the help output for this command, and return it as a string.
|
|
143
|
+
*/
|
|
144
|
+
helpString(): string;
|
|
145
|
+
/**
|
|
146
|
+
* Print the help output for this command.
|
|
147
|
+
*/
|
|
148
|
+
printHelp(): void;
|
|
149
|
+
}
|
|
150
|
+
export declare class MassargHelpCommand<T extends ArgsObject = ArgsObject> extends MassargCommand<T> {
|
|
151
|
+
constructor(config?: Partial<Omit<CommandConfig<T>, 'run'>>);
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=command.d.ts.map
|
package/command.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command.d.ts","sourceRoot":"","sources":["../src/command.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,OAAO,EAAqB,UAAU,EAAiB,MAAM,QAAQ,CAAA;AACrE,OAAO,EACL,aAAa,EACb,WAAW,EACX,YAAY,EACZ,iBAAiB,EAElB,MAAM,UAAU,CAAA;AACjB,OAAO,EAAE,YAAY,EAA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