@cloud-copilot/cli 0.1.39 → 0.2.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 +86 -207
- package/dist/cjs/arguments/argument.d.ts +60 -0
- package/dist/cjs/arguments/argument.d.ts.map +1 -0
- package/dist/cjs/arguments/argument.js +3 -0
- package/dist/cjs/arguments/argument.js.map +1 -0
- package/dist/cjs/arguments/arrayValueArgument.d.ts +15 -0
- package/dist/cjs/arguments/arrayValueArgument.d.ts.map +1 -0
- package/dist/cjs/arguments/arrayValueArgument.js +38 -0
- package/dist/cjs/arguments/arrayValueArgument.js.map +1 -0
- package/dist/cjs/arguments/booleanArgument.d.ts +11 -0
- package/dist/cjs/arguments/booleanArgument.d.ts.map +1 -0
- package/dist/cjs/arguments/booleanArgument.js +25 -0
- package/dist/cjs/arguments/booleanArgument.js.map +1 -0
- package/dist/cjs/arguments/enumArgument.d.ts +15 -0
- package/dist/cjs/arguments/enumArgument.d.ts.map +1 -0
- package/dist/cjs/arguments/enumArgument.js +35 -0
- package/dist/cjs/arguments/enumArgument.js.map +1 -0
- package/dist/cjs/arguments/enumArrayArgument.d.ts +15 -0
- package/dist/cjs/arguments/enumArrayArgument.d.ts.map +1 -0
- package/dist/cjs/arguments/enumArrayArgument.js +42 -0
- package/dist/cjs/arguments/enumArrayArgument.js.map +1 -0
- package/dist/cjs/arguments/mapArgument.d.ts +18 -0
- package/dist/cjs/arguments/mapArgument.d.ts.map +1 -0
- package/dist/cjs/arguments/mapArgument.js +35 -0
- package/dist/cjs/arguments/mapArgument.js.map +1 -0
- package/dist/cjs/arguments/numberArguments.d.ts +17 -0
- package/dist/cjs/arguments/numberArguments.d.ts.map +1 -0
- package/dist/cjs/arguments/numberArguments.js +15 -0
- package/dist/cjs/arguments/numberArguments.js.map +1 -0
- package/dist/cjs/arguments/singleValueArgument.d.ts +15 -0
- package/dist/cjs/arguments/singleValueArgument.d.ts.map +1 -0
- package/dist/cjs/arguments/singleValueArgument.js +35 -0
- package/dist/cjs/arguments/singleValueArgument.js.map +1 -0
- package/dist/cjs/arguments/stringArguments.d.ts +17 -0
- package/dist/cjs/arguments/stringArguments.d.ts.map +1 -0
- package/dist/cjs/arguments/stringArguments.js +14 -0
- package/dist/cjs/arguments/stringArguments.js.map +1 -0
- package/dist/cjs/cli.d.ts +37 -89
- package/dist/cjs/cli.d.ts.map +1 -1
- package/dist/cjs/cli.js +171 -293
- package/dist/cjs/cli.js.map +1 -1
- package/dist/cjs/index.d.ts +11 -2
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +20 -3
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/readRelative.d.ts +25 -7
- package/dist/cjs/readRelative.d.ts.map +1 -1
- package/dist/cjs/readRelative.js +63 -20
- package/dist/cjs/readRelative.js.map +1 -1
- package/dist/esm/arguments/argument.d.ts +60 -0
- package/dist/esm/arguments/argument.d.ts.map +1 -0
- package/dist/esm/arguments/argument.js +2 -0
- package/dist/esm/arguments/argument.js.map +1 -0
- package/dist/esm/arguments/arrayValueArgument.d.ts +15 -0
- package/dist/esm/arguments/arrayValueArgument.d.ts.map +1 -0
- package/dist/esm/arguments/arrayValueArgument.js +35 -0
- package/dist/esm/arguments/arrayValueArgument.js.map +1 -0
- package/dist/esm/arguments/booleanArgument.d.ts +11 -0
- package/dist/esm/arguments/booleanArgument.d.ts.map +1 -0
- package/dist/esm/arguments/booleanArgument.js +22 -0
- package/dist/esm/arguments/booleanArgument.js.map +1 -0
- package/dist/esm/arguments/enumArgument.d.ts +15 -0
- package/dist/esm/arguments/enumArgument.d.ts.map +1 -0
- package/dist/esm/arguments/enumArgument.js +32 -0
- package/dist/esm/arguments/enumArgument.js.map +1 -0
- package/dist/esm/arguments/enumArrayArgument.d.ts +15 -0
- package/dist/esm/arguments/enumArrayArgument.d.ts.map +1 -0
- package/dist/esm/arguments/enumArrayArgument.js +39 -0
- package/dist/esm/arguments/enumArrayArgument.js.map +1 -0
- package/dist/esm/arguments/mapArgument.d.ts +18 -0
- package/dist/esm/arguments/mapArgument.d.ts.map +1 -0
- package/dist/esm/arguments/mapArgument.js +32 -0
- package/dist/esm/arguments/mapArgument.js.map +1 -0
- package/dist/esm/arguments/numberArguments.d.ts +17 -0
- package/dist/esm/arguments/numberArguments.d.ts.map +1 -0
- package/dist/esm/arguments/numberArguments.js +12 -0
- package/dist/esm/arguments/numberArguments.js.map +1 -0
- package/dist/esm/arguments/singleValueArgument.d.ts +15 -0
- package/dist/esm/arguments/singleValueArgument.d.ts.map +1 -0
- package/dist/esm/arguments/singleValueArgument.js +32 -0
- package/dist/esm/arguments/singleValueArgument.js.map +1 -0
- package/dist/esm/arguments/stringArguments.d.ts +17 -0
- package/dist/esm/arguments/stringArguments.d.ts.map +1 -0
- package/dist/esm/arguments/stringArguments.js +11 -0
- package/dist/esm/arguments/stringArguments.js.map +1 -0
- package/dist/esm/cli.d.ts +37 -89
- package/dist/esm/cli.d.ts.map +1 -1
- package/dist/esm/cli.js +171 -292
- package/dist/esm/cli.js.map +1 -1
- package/dist/esm/index.d.ts +11 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +10 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/readRelative.d.ts +25 -7
- package/dist/esm/readRelative.d.ts.map +1 -1
- package/dist/esm/readRelative.js +58 -19
- package/dist/esm/readRelative.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,59 +7,103 @@
|
|
|
7
7
|
Utilities for standardizing working CLIs in Typescript.
|
|
8
8
|
|
|
9
9
|
- Standardizes the way subcommands, arguments, and operands are parsed and validated
|
|
10
|
-
- Includes "
|
|
10
|
+
- Includes "prefix matching" of subcommands and arguments, so users can provide partial names if they match only one subcommand or argument
|
|
11
11
|
- Allows users to default arguments with environment variables
|
|
12
12
|
- Automatically generates a help text for the user
|
|
13
13
|
- Provides a type safe response of the parsed arguments
|
|
14
14
|
- Has zero dependencies
|
|
15
|
+
- Built-in argument types for string, number, boolean, enum, array, and map types
|
|
16
|
+
- Easy custom argument types to create your own argument types with validation
|
|
17
|
+
- Async argument validation and version checking
|
|
18
|
+
- Automatic version checking for updates to your CLI
|
|
15
19
|
|
|
16
20
|
I wrote this because I'm building several CLI applications and want to have a standard parsing of arguments that users can rely on across all of them.
|
|
17
21
|
|
|
18
22
|
I was inspired by https://github.com/lirantal/nodejs-cli-apps-best-practices and https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html to follow best practices for accepting arguments and environment variables while keeping my scope extremely limited and having no dependencies.
|
|
19
23
|
|
|
24
|
+
## Table of Contents
|
|
25
|
+
|
|
26
|
+
- [Example](#example)
|
|
27
|
+
- [Installation](#installation)
|
|
28
|
+
- [Documentation](#documentation)
|
|
29
|
+
- [Core Guides](#core-guides)
|
|
30
|
+
- [Examples](#examples)
|
|
31
|
+
|
|
32
|
+
## Documentation
|
|
33
|
+
|
|
34
|
+
### Core Guides
|
|
35
|
+
|
|
36
|
+
- **[Getting Started](docs/GettingStarted.md)** - Complete tutorial for new users
|
|
37
|
+
- **[Parsing Reference](docs/ParsingReference.md)** - Detailed parsing rules and mechanics
|
|
38
|
+
- **[Subcommands Guide](docs/SubcommandsGuide.md)** - Building CLIs with subcommands
|
|
39
|
+
- **[Custom Arguments](docs/CustomArguments.md)** - Creating custom argument types with validation
|
|
40
|
+
- **[Environment Variables](docs/EnvironmentVariables.md)** - Environment variable integration
|
|
41
|
+
- **[Version Handling](docs/VersionHandling.md)** - Version management and update checking
|
|
42
|
+
- **[API Reference](docs/APIReference.md)** - API documentation
|
|
43
|
+
|
|
44
|
+
### [Examples](src/demo/README.md)
|
|
45
|
+
|
|
46
|
+
- **[Basic CLI](src/demo/basic.ts)** - Minimal CLI setup with version handling
|
|
47
|
+
- **[Arguments Demo](src/demo/arguments.ts)** - All argument types including custom date arguments
|
|
48
|
+
- **[Subcommands Demo](src/demo/subcommands.ts)** - CLI with multiple subcommands and arguments
|
|
49
|
+
- **[Version Handling](src/demo/version.ts)** - Version checking and update notifications
|
|
50
|
+
- **[Reading from stdin](src/demo/stdin.ts)** - Processing input from stdin
|
|
51
|
+
|
|
20
52
|
## Example
|
|
21
53
|
|
|
22
54
|
```typescript
|
|
23
|
-
import {
|
|
24
|
-
|
|
25
|
-
|
|
55
|
+
import {
|
|
56
|
+
parseCliArguments,
|
|
57
|
+
stringArgument,
|
|
58
|
+
stringArrayArgument,
|
|
59
|
+
booleanArgument,
|
|
60
|
+
numberArgument,
|
|
61
|
+
enumArgument
|
|
62
|
+
} from '@cloud-copilot/cli'
|
|
63
|
+
|
|
64
|
+
const cli = await parseCliArguments(
|
|
26
65
|
'my-command',
|
|
27
66
|
// optional subcommands
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
validValues: ['typescript', 'javascript']
|
|
67
|
+
{
|
|
68
|
+
init: {
|
|
69
|
+
description: 'Initialize the project',
|
|
70
|
+
// Subcommand Specific Arguments
|
|
71
|
+
arguments: {
|
|
72
|
+
s3: booleanArgument({
|
|
73
|
+
description: 'Use S3 as the storage',
|
|
74
|
+
character: 's'
|
|
75
|
+
}),
|
|
76
|
+
language: enumArgument({
|
|
77
|
+
description: 'The language to use',
|
|
78
|
+
validValues: ['typescript', 'javascript']
|
|
79
|
+
})
|
|
42
80
|
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
values: 'single',
|
|
52
|
-
type: 'string'
|
|
81
|
+
},
|
|
82
|
+
download: {
|
|
83
|
+
description: 'Download a file',
|
|
84
|
+
// Subcommand Specific Arguments
|
|
85
|
+
arguments: {
|
|
86
|
+
url: stringArgument({
|
|
87
|
+
description: 'The URL to download from'
|
|
88
|
+
})
|
|
53
89
|
}
|
|
54
90
|
}
|
|
55
91
|
},
|
|
56
|
-
// Global Arguments
|
|
92
|
+
// Global Arguments that apply to all subcommands
|
|
57
93
|
{
|
|
58
|
-
regions: {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
94
|
+
regions: stringArrayArgument({
|
|
95
|
+
description: 'Regions to deploy to'
|
|
96
|
+
}),
|
|
97
|
+
account: stringArgument({
|
|
98
|
+
description: 'AWS account to deploy to'
|
|
99
|
+
}),
|
|
100
|
+
ssl: booleanArgument({
|
|
101
|
+
description: 'Enable SSL',
|
|
102
|
+
character: 's'
|
|
103
|
+
}),
|
|
104
|
+
port: numberArgument({
|
|
105
|
+
description: 'Port to use'
|
|
106
|
+
})
|
|
63
107
|
},
|
|
64
108
|
{}
|
|
65
109
|
)
|
|
@@ -68,18 +112,22 @@ const cli = parseArguments(
|
|
|
68
112
|
This will automatically pull in the arguments from the command line and validate them. If any arguments are not valid, it will throw an error with the message of what is wrong. If all arguments and subcommands are valid, it will return an object with a type safe response.
|
|
69
113
|
|
|
70
114
|
```typescript
|
|
71
|
-
cli.subcommand //
|
|
72
|
-
cli.args.regions // string[]
|
|
115
|
+
cli.subcommand // 'init' | 'download' | undefined
|
|
116
|
+
cli.args.regions // string[] | undefined
|
|
73
117
|
cli.args.account // string | undefined
|
|
74
118
|
cli.args.ssl // boolean
|
|
75
119
|
cli.args.port // number | undefined
|
|
76
|
-
cli.args.points // number[]
|
|
77
120
|
|
|
78
121
|
cli.anyValues // boolean, whether any values were provided on the CLI
|
|
79
122
|
|
|
80
123
|
if (cli.subcommand === 'init') {
|
|
81
|
-
// Type Checked command specific arguments
|
|
124
|
+
// Type Checked command specific arguments only in the return type if the subcommand is selected
|
|
82
125
|
cli.args.s3 // boolean
|
|
126
|
+
cli.args.language // 'typescript' | 'javascript' | undefined
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (cli.subcommand === 'download') {
|
|
130
|
+
cli.args.url // string | undefined
|
|
83
131
|
}
|
|
84
132
|
```
|
|
85
133
|
|
|
@@ -90,172 +138,3 @@ If the user provides `--help` a help message will be printed out with the availa
|
|
|
90
138
|
```bash
|
|
91
139
|
npm install @cloud-copilot/cli
|
|
92
140
|
```
|
|
93
|
-
|
|
94
|
-
## Parsing CLI Input
|
|
95
|
-
|
|
96
|
-
Arguments are parsed into three things:
|
|
97
|
-
|
|
98
|
-
```bash
|
|
99
|
-
my-command [subcommand] [arguments] [--] [operands]
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
- The subcommand which is the first argument and is optional based on your configuration
|
|
103
|
-
- The arguments that start with `--` (or optionally `-` for boolean flags) in the CLI
|
|
104
|
-
- The operands, which are the remaining values after the arguments. If provided, all arguments after the literal ` -- ` are considered operands.
|
|
105
|
-
|
|
106
|
-
### Parsing subcommands
|
|
107
|
-
|
|
108
|
-
If you provide a list of subcommands, the first argument will be parsed as a subcommand if it does not start with a `-`. If the subcommand is not provided, it will be `undefined`.
|
|
109
|
-
|
|
110
|
-
Partial matching will be used. If only one subcommand matches the string from the user input, the subcommand it matches will be returned. If more than one subcommand matches, an error will be thrown. If the string provided does not match any subcommand, an error will be thrown.
|
|
111
|
-
|
|
112
|
-
### Parsing Arguments
|
|
113
|
-
|
|
114
|
-
Arguments are parsed from the CLI input. They are generally parsed as `--name value` or `--name value1 value2 value3`. If the argument is a boolean flag, it can be provided as `--name` or `-n`. Arguments are case insensitive.
|
|
115
|
-
|
|
116
|
-
#### Boolean Arguments
|
|
117
|
-
|
|
118
|
-
Boolean values can be provided as `--name` or `-n`. If the argument is a boolean, the value will be `true` if the argument is provided and `false` if it is not. If an argument is passed to a boolean argument, an error will be thrown.
|
|
119
|
-
|
|
120
|
-
Multiple single character boolean flags can be combined into a single argument. For example, `-abc` is equivalent to `-a -b -c`.
|
|
121
|
-
|
|
122
|
-
#### String or Number Arguments
|
|
123
|
-
|
|
124
|
-
String or number arguments can be provided as `--name value` or `--name value1 value2 value3`. If the argument is a single value, it will be parsed as a single value. If the argument is multiple values, it will be parsed as an array of strings or numbers.
|
|
125
|
-
|
|
126
|
-
String or number arguments will throw an error if:
|
|
127
|
-
|
|
128
|
-
- The flag is specified and no values are provided
|
|
129
|
-
- The argument accepts only one value and multiple values are provided
|
|
130
|
-
- The argument is a number and a non-number value is provided
|
|
131
|
-
|
|
132
|
-
#### Enum Arguments
|
|
133
|
-
|
|
134
|
-
Enum arguments can be provided as `--name value1 value2`. The values must be one of the values provided in the `values` field of the argument configuration. The values are returned as a string or a string array.
|
|
135
|
-
|
|
136
|
-
Enum arguments will throw an error if:
|
|
137
|
-
|
|
138
|
-
- The flag is specified and no values are provided
|
|
139
|
-
- The argument accepts only one value and multiple values are provided
|
|
140
|
-
- Any of the arguments provided are not in the list of allowed values.
|
|
141
|
-
|
|
142
|
-
If the value is not one of the values provided, an error will be thrown.
|
|
143
|
-
|
|
144
|
-
### Operands
|
|
145
|
-
|
|
146
|
-
Operands are the remaining values after the arguments.
|
|
147
|
-
If there are no arguments, all strings after the subcommand (if any) are considered operands.
|
|
148
|
-
If the last argument is a boolean all strings after the boolean are considered operands.
|
|
149
|
-
If the last argument is a single value all the strings after the value for the argument are considered operands.
|
|
150
|
-
If the last argument is multiple values, use ` -- ` to separate the arguments from the operands.
|
|
151
|
-
|
|
152
|
-
### Environment Variables
|
|
153
|
-
|
|
154
|
-
If an `environmentPrefix` is specified in `additionalArgs`, the library will look for environment variables that start with the prefix and use them as defaults for the arguments. For example, if the prefix is `MY_APP`, the library will look for environment variables like `MY_APP_REGIONS`, `MY_APP_ACCOUNT`, etc. These variables will be validated just like the CLI arguments. Any values provided on the CLI will override the environment variables.
|
|
155
|
-
|
|
156
|
-
## Printing Help
|
|
157
|
-
|
|
158
|
-
If the user provides `--help` as an argument the library will print out a help message with the available subcommands and arguments. This can also be done manually with the function `printHelpContents` which accepts the same arguments as `parseCliArguments`.
|
|
159
|
-
|
|
160
|
-
```typescript
|
|
161
|
-
//Given this config
|
|
162
|
-
parseCliArguments(
|
|
163
|
-
'my-command',
|
|
164
|
-
// optional commands
|
|
165
|
-
[
|
|
166
|
-
{ name: 'init', description: 'Initialize my environment' },
|
|
167
|
-
{
|
|
168
|
-
name: 'execute',
|
|
169
|
-
description: 'Run the download'
|
|
170
|
-
}
|
|
171
|
-
],
|
|
172
|
-
{
|
|
173
|
-
expandAsterisk: {
|
|
174
|
-
character: 'e',
|
|
175
|
-
description: 'Expand a single wildcard asterisk (*) to all possible values',
|
|
176
|
-
values: 'none'
|
|
177
|
-
},
|
|
178
|
-
invalidActionBehavior: {
|
|
179
|
-
description: 'Behavior when an invalid action is encountered, defaults to remove',
|
|
180
|
-
validValues: ['remove', 'error', 'include'],
|
|
181
|
-
type: 'enum',
|
|
182
|
-
values: 'single'
|
|
183
|
-
},
|
|
184
|
-
readWaitMs: {
|
|
185
|
-
description: 'Milliseconds to wait between read operations',
|
|
186
|
-
type: 'number',
|
|
187
|
-
values: 'single'
|
|
188
|
-
}
|
|
189
|
-
},
|
|
190
|
-
{
|
|
191
|
-
operandsName: 'action'
|
|
192
|
-
}
|
|
193
|
-
)
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
````
|
|
197
|
-
|
|
198
|
-
```bash
|
|
199
|
-
$ my-command --help
|
|
200
|
-
Usage: my-command [command] [options] [flags] [--] [action1] [action2]
|
|
201
|
-
Commands:
|
|
202
|
-
init : Initialize my environment
|
|
203
|
-
execute: Run the download
|
|
204
|
-
Options:
|
|
205
|
-
--expand-asterisk: (-e) Expand a single wildcard asterisk (*) to all possible values.
|
|
206
|
-
--invalid-action-behavior: Behavior when an invalid action is encountered, defaults to remove. Must
|
|
207
|
-
be one of: remove, error, include.
|
|
208
|
-
--read-wait-ms: Milliseconds to wait between read operations. One number required
|
|
209
|
-
--help: Print this help message and exit
|
|
210
|
-
````
|
|
211
|
-
|
|
212
|
-
## API
|
|
213
|
-
|
|
214
|
-
`parseCliArguments` and `printHelpContents` are the two main functions for arguments and help text. They take the same arguments.
|
|
215
|
-
|
|
216
|
-
- `commandName` - The name of the command. This is used in the help text.
|
|
217
|
-
- `subcommands` - An object or sub commands that can be provided. The key is the name of the subcommand and the value is the configuration. Can be an empty object `{}`.
|
|
218
|
-
- `description` - The description of the subcommand. This is used in the help text.
|
|
219
|
-
- `options` - An object of argument definitions. The key is the name of the argument that will be returned in your results and the value is the configuration. Option objects are the same as described in `cliOptions`
|
|
220
|
-
- `cliOptions` - An object of argument definitions. The key is the name of the argument that will be returned in your results and the value is the configuration. One of
|
|
221
|
-
|
|
222
|
-
- Standard Argument:
|
|
223
|
-
|
|
224
|
-
- `values` - The number of values the argument accepts. Can be `single` or `multiple`.
|
|
225
|
-
- `type` - The type of the argument. Can be `string` or `number`.
|
|
226
|
-
- `description` - The description of the argument. This is used in the help text.
|
|
227
|
-
|
|
228
|
-
- Enum Argument:
|
|
229
|
-
|
|
230
|
-
- `values` - The number of values the argument accepts. Can be `single` or `multiple`.
|
|
231
|
-
- `type` - The type of the argument. Must be `enum`.
|
|
232
|
-
- `validValues` - An array of valid values for the argument.
|
|
233
|
-
- `description` - The description of the argument. This is used in the help text.
|
|
234
|
-
|
|
235
|
-
- Boolean Argument:
|
|
236
|
-
|
|
237
|
-
- `character` - The single character flag for the argument.
|
|
238
|
-
- `type` - The type of the argument. Must be `boolean`.
|
|
239
|
-
- `description` - The description of the argument. This is used in the help text.
|
|
240
|
-
|
|
241
|
-
- `additionalArgs` - An object of additional arguments. This can include:
|
|
242
|
-
|
|
243
|
-
- `version` - The version of the command. If this is provided the argument `--version` will print out the version and exit.
|
|
244
|
-
- `args` - Override the string array or arguments to parse, by default it will use `process.argv.slice(2)`.
|
|
245
|
-
- `env` - Override the environment variables to use, by default it will use `process.env`.
|
|
246
|
-
- `envPrefix` - A prefix to use for environment variables. If provided, the library will look for environment variables that start with the prefix and use them as defaults for the arguments. For example, if the prefix is `MY_APP`, the library will look for environment variables like `MY_APP_REGIONS`, `MY_APP_ACCOUNT`, etc. These variables will be validated just like the CLI arguments. Any values provided on the CLI will override the environment variables.
|
|
247
|
-
- `operandsName` - The name of the operands. This is used in the help text. By default, this is `operand`.
|
|
248
|
-
- `requireSubcommand` - If true, a subcommand is required. By default, this is false.
|
|
249
|
-
- `allowOperandsFromStdin` - If true, the help text will include a usage example of reading operands from stdin. By default, this is false.
|
|
250
|
-
|
|
251
|
-
## Reading from stdin
|
|
252
|
-
|
|
253
|
-
The function `readStdin` can be used to read from stdin. It returns a promise that resolves with the string read from stdin.
|
|
254
|
-
|
|
255
|
-
```typescript
|
|
256
|
-
import { readStdin } from '@cloud-copilot/cli'
|
|
257
|
-
|
|
258
|
-
const stdin = await readStdin(undefined)
|
|
259
|
-
```
|
|
260
|
-
|
|
261
|
-
It optionally takes a read timeout in milliseconds to wait to receive the first byte before it assumes there is no input. This is useful if for instance your CLI is being piped input from another process that may more than a few seconds, such as a `curl` command to an API.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export type ValidValue<T> = {
|
|
2
|
+
valid: true;
|
|
3
|
+
value: T;
|
|
4
|
+
};
|
|
5
|
+
export type InvalidValue = {
|
|
6
|
+
valid: false;
|
|
7
|
+
message: string;
|
|
8
|
+
};
|
|
9
|
+
export type ValidatedValues<T> = ValidValue<T> | InvalidValue;
|
|
10
|
+
export type Argument<ArgumentType> = {
|
|
11
|
+
/**
|
|
12
|
+
* A description of the argument, used in help text.
|
|
13
|
+
*/
|
|
14
|
+
description: string;
|
|
15
|
+
/**
|
|
16
|
+
* The default value of the argument, if any.
|
|
17
|
+
* If not provided, the argument will be undefined until a value is provided.
|
|
18
|
+
*/
|
|
19
|
+
defaultValue: ArgumentType;
|
|
20
|
+
/**
|
|
21
|
+
* Validate all values passed to the argument at once.
|
|
22
|
+
* @param values the array of strings passed to the argument
|
|
23
|
+
* @returns if the value is invalid, returns { valid: false, message: string } and message will be shown to the user.
|
|
24
|
+
* if the value is valid, returns { valid: true, values`: ArgumentType[] } where value is the parsed value.
|
|
25
|
+
*/
|
|
26
|
+
validateValues: (currentValue: ArgumentType, values: string[]) => Promise<ValidValue<ArgumentType> | InvalidValue> | ValidValue<ArgumentType> | InvalidValue;
|
|
27
|
+
/**
|
|
28
|
+
* Reduces the current value and a new values into a single value.
|
|
29
|
+
*
|
|
30
|
+
* @param current the current value of the argument, can be undefined if there is no default value
|
|
31
|
+
* @param newValues the new values being added as parsed by validateValues
|
|
32
|
+
* @returns the new value of the argument
|
|
33
|
+
*/
|
|
34
|
+
reduceValues: (currentValue: ArgumentType, newValues: NonNullable<ArgumentType>) => Promise<ArgumentType> | ArgumentType;
|
|
35
|
+
/**
|
|
36
|
+
* If this callback is provided, it will be called when the argument is present
|
|
37
|
+
*
|
|
38
|
+
* @param currentValue the current value of the argument
|
|
39
|
+
* @returns the value to set the argument to
|
|
40
|
+
*/
|
|
41
|
+
present?(currentValue: ArgumentType): Promise<ArgumentType> | ArgumentType;
|
|
42
|
+
/**
|
|
43
|
+
* If this function is provided, it will be called to determine if the argument
|
|
44
|
+
* can accept multiple values.
|
|
45
|
+
*
|
|
46
|
+
* If not provided, the argument will only accept a single value.
|
|
47
|
+
*
|
|
48
|
+
* @returns true if the argument can accept multiple values, false otherwise
|
|
49
|
+
*/
|
|
50
|
+
acceptMultipleValues?(): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* A single-character alias for the argument, e.g. 'f' for --foo
|
|
53
|
+
*
|
|
54
|
+
* This is only valid if the argument does not take a value (e.g. boolean flags).
|
|
55
|
+
*/
|
|
56
|
+
character?: string;
|
|
57
|
+
};
|
|
58
|
+
export type PerArgumentArgs = Pick<Argument<any>, 'description'>;
|
|
59
|
+
export type CustomArgument<ArgumentType> = Omit<Argument<ArgumentType>, 'character'>;
|
|
60
|
+
//# sourceMappingURL=argument.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"argument.d.ts","sourceRoot":"","sources":["../../../src/arguments/argument.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,CAAC,CAAA;CAAE,CAAA;AACrD,MAAM,MAAM,YAAY,GAAG;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAA;AAC5D,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,GAAG,YAAY,CAAA;AAE7D,MAAM,MAAM,QAAQ,CAAC,YAAY,IAAI;IACnC;;OAEG;IACH,WAAW,EAAE,MAAM,CAAA;IAEnB;;;OAGG;IACH,YAAY,EAAE,YAAY,CAAA;IAE1B;;;;;OAKG;IACH,cAAc,EAAE,CACd,YAAY,EAAE,YAAY,EAC1B,MAAM,EAAE,MAAM,EAAE,KACb,OAAO,CAAC,UAAU,CAAC,YAAY,CAAC,GAAG,YAAY,CAAC,GAAG,UAAU,CAAC,YAAY,CAAC,GAAG,YAAY,CAAA;IAE/F;;;;;;OAMG;IACH,YAAY,EAAE,CACZ,YAAY,EAAE,YAAY,EAC1B,SAAS,EAAE,WAAW,CAAC,YAAY,CAAC,KACjC,OAAO,CAAC,YAAY,CAAC,GAAG,YAAY,CAAA;IAEzC;;;;;OAKG;IACH,OAAO,CAAC,CAAC,YAAY,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,GAAG,YAAY,CAAA;IAE1E;;;;;;;OAOG;IACH,oBAAoB,CAAC,IAAI,OAAO,CAAA;IAEhC;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAED,MAAM,MAAM,eAAe,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,aAAa,CAAC,CAAA;AAChE,MAAM,MAAM,cAAc,CAAC,YAAY,IAAI,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,EAAE,WAAW,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"argument.js","sourceRoot":"","sources":["../../../src/arguments/argument.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Argument, PerArgumentArgs, ValidatedValues } from './argument.js';
|
|
2
|
+
type ArrayValueValidator<ValueType> = (rawValue: string) => Promise<ValidatedValues<ValueType>> | ValidatedValues<ValueType>;
|
|
3
|
+
/**
|
|
4
|
+
* Creates an array value argument factory for a specific type
|
|
5
|
+
*/
|
|
6
|
+
export declare function arrayValueArgument<ValueType>(validator: ArrayValueValidator<ValueType>, descriptionSuffix?: string): {
|
|
7
|
+
<const O extends {
|
|
8
|
+
defaultValue: ValueType[];
|
|
9
|
+
} & PerArgumentArgs>(options: O): Argument<ValueType[]>;
|
|
10
|
+
<const O extends {
|
|
11
|
+
defaultValue?: undefined;
|
|
12
|
+
} & PerArgumentArgs>(options: O): Argument<ValueType[] | undefined>;
|
|
13
|
+
};
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=arrayValueArgument.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"arrayValueArgument.d.ts","sourceRoot":"","sources":["../../../src/arguments/arrayValueArgument.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,eAAe,CAAA;AAE1E,KAAK,mBAAmB,CAAC,SAAS,IAAI,CACpC,QAAQ,EAAE,MAAM,KACb,OAAO,CAAC,eAAe,CAAC,SAAS,CAAC,CAAC,GAAG,eAAe,CAAC,SAAS,CAAC,CAAA;AAErE;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,SAAS,EAC1C,SAAS,EAAE,mBAAmB,CAAC,SAAS,CAAC,EACzC,iBAAiB,GAAE,MAAW;WAEA,CAAC,SAAS;QAAE,YAAY,EAAE,SAAS,EAAE,CAAA;KAAE,GAAG,eAAe,WAC5E,CAAC,GACT,QAAQ,CAAC,SAAS,EAAE,CAAC;WACM,CAAC,SAAS;QAAE,YAAY,CAAC,EAAE,SAAS,CAAA;KAAE,GAAG,eAAe,WAC3E,CAAC,GACT,QAAQ,CAAC,SAAS,EAAE,GAAG,SAAS,CAAC;EAsCrC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.arrayValueArgument = arrayValueArgument;
|
|
4
|
+
/**
|
|
5
|
+
* Creates an array value argument factory for a specific type
|
|
6
|
+
*/
|
|
7
|
+
function arrayValueArgument(validator, descriptionSuffix = '') {
|
|
8
|
+
function createArgument(options) {
|
|
9
|
+
return {
|
|
10
|
+
description: options.description + descriptionSuffix,
|
|
11
|
+
validateValues: async (current, values) => {
|
|
12
|
+
if (values.length === 0) {
|
|
13
|
+
return { valid: false, message: 'at least one value is required' };
|
|
14
|
+
}
|
|
15
|
+
const validatedValues = [];
|
|
16
|
+
for (const rawValue of values) {
|
|
17
|
+
const result = await validator(rawValue);
|
|
18
|
+
if (!result.valid) {
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
validatedValues.push(result.value);
|
|
22
|
+
}
|
|
23
|
+
return { valid: true, value: validatedValues };
|
|
24
|
+
},
|
|
25
|
+
reduceValues: async (current, newValues) => {
|
|
26
|
+
if (!current) {
|
|
27
|
+
return newValues;
|
|
28
|
+
}
|
|
29
|
+
current.push(...newValues);
|
|
30
|
+
return current;
|
|
31
|
+
},
|
|
32
|
+
defaultValue: options.defaultValue,
|
|
33
|
+
acceptMultipleValues: () => true
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return createArgument;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=arrayValueArgument.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"arrayValueArgument.js","sourceRoot":"","sources":["../../../src/arguments/arrayValueArgument.ts"],"names":[],"mappings":";;AASA,gDA+CC;AAlDD;;GAEG;AACH,SAAgB,kBAAkB,CAChC,SAAyC,EACzC,oBAA4B,EAAE;IAQ9B,SAAS,cAAc,CAErB,OAAU;QACV,OAAO;YACL,WAAW,EAAE,OAAO,CAAC,WAAW,GAAG,iBAAiB;YACpD,cAAc,EAAE,KAAK,EACnB,OAAgC,EAChC,MAAgB,EACuB,EAAE;gBACzC,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;oBACxB,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,gCAAgC,EAAE,CAAA;gBACpE,CAAC;gBAED,MAAM,eAAe,GAAgB,EAAE,CAAA;gBACvC,KAAK,MAAM,QAAQ,IAAI,MAAM,EAAE,CAAC;oBAC9B,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,QAAQ,CAAC,CAAA;oBACxC,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;wBAClB,OAAO,MAAM,CAAA;oBACf,CAAC;oBACD,eAAe,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;gBACpC,CAAC;gBAED,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,eAAe,EAAE,CAAA;YAChD,CAAC;YACD,YAAY,EAAE,KAAK,EAAE,OAAgC,EAAE,SAAsB,EAAE,EAAE;gBAC/E,IAAI,CAAC,OAAO,EAAE,CAAC;oBACb,OAAO,SAAS,CAAA;gBAClB,CAAC;gBACD,OAAO,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,CAAA;gBAC1B,OAAO,OAAO,CAAA;YAChB,CAAC;YACD,YAAY,EAAE,OAAO,CAAC,YAAY;YAClC,oBAAoB,EAAE,GAAG,EAAE,CAAC,IAAI;SACjC,CAAA;IACH,CAAC;IAED,OAAO,cAAc,CAAA;AACvB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Argument, PerArgumentArgs } from './argument.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a boolean argument that is false by default and true if present.
|
|
4
|
+
*
|
|
5
|
+
* @param options the description and the single character that can be used to set the value to true
|
|
6
|
+
* @returns a boolean argument
|
|
7
|
+
*/
|
|
8
|
+
export declare function booleanArgument<const O extends {
|
|
9
|
+
character: string;
|
|
10
|
+
} & PerArgumentArgs>(options: O): Argument<boolean>;
|
|
11
|
+
//# sourceMappingURL=booleanArgument.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"booleanArgument.d.ts","sourceRoot":"","sources":["../../../src/arguments/booleanArgument.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAmB,MAAM,eAAe,CAAA;AAE1E;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,CAAC,CAAC,SAAS;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG,eAAe,EACrF,OAAO,EAAE,CAAC,GACT,QAAQ,CAAC,OAAO,CAAC,CAcnB"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.booleanArgument = booleanArgument;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a boolean argument that is false by default and true if present.
|
|
6
|
+
*
|
|
7
|
+
* @param options the description and the single character that can be used to set the value to true
|
|
8
|
+
* @returns a boolean argument
|
|
9
|
+
*/
|
|
10
|
+
function booleanArgument(options) {
|
|
11
|
+
return {
|
|
12
|
+
description: options.description,
|
|
13
|
+
character: options.character,
|
|
14
|
+
validateValues: (currentValue, value) => {
|
|
15
|
+
if (value.length == 0) {
|
|
16
|
+
return { valid: true, value: true };
|
|
17
|
+
}
|
|
18
|
+
return { valid: false, message: `does not accept values but received ${value.join(', ')}` };
|
|
19
|
+
},
|
|
20
|
+
reduceValues: (current, newValue) => current,
|
|
21
|
+
present: (currentValue) => true,
|
|
22
|
+
defaultValue: false
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=booleanArgument.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"booleanArgument.js","sourceRoot":"","sources":["../../../src/arguments/booleanArgument.ts"],"names":[],"mappings":";;AAQA,0CAgBC;AAtBD;;;;;GAKG;AACH,SAAgB,eAAe,CAC7B,OAAU;IAEV,OAAO;QACL,WAAW,EAAE,OAAO,CAAC,WAAW;QAChC,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,cAAc,EAAE,CAAC,YAAqB,EAAE,KAAe,EAA4B,EAAE;YACnF,IAAI,KAAK,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;gBACtB,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAA;YACrC,CAAC;YACD,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,uCAAuC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,CAAA;QAC7F,CAAC;QACD,YAAY,EAAE,CAAC,OAAgB,EAAE,QAAiB,EAAE,EAAE,CAAC,OAAO;QAC9D,OAAO,EAAE,CAAC,YAAqB,EAAE,EAAE,CAAC,IAAI;QACxC,YAAY,EAAE,KAAK;KACpB,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Argument, PerArgumentArgs } from './argument.js';
|
|
2
|
+
type EnumType<T extends {
|
|
3
|
+
validValues: string[];
|
|
4
|
+
defaultValue?: string | undefined;
|
|
5
|
+
}> = T['defaultValue'] extends string ? T['validValues'][number] | T['defaultValue'] : T['validValues'][number];
|
|
6
|
+
export declare function enumArgument<const O extends {
|
|
7
|
+
defaultValue: string;
|
|
8
|
+
validValues: string[];
|
|
9
|
+
} & PerArgumentArgs>(options: O): Argument<EnumType<O>>;
|
|
10
|
+
export declare function enumArgument<const O extends {
|
|
11
|
+
defaultValue?: undefined;
|
|
12
|
+
validValues: string[];
|
|
13
|
+
} & PerArgumentArgs>(options: O): Argument<EnumType<O> | undefined>;
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=enumArgument.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enumArgument.d.ts","sourceRoot":"","sources":["../../../src/arguments/enumArgument.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAmB,MAAM,eAAe,CAAA;AAE1E,KAAK,QAAQ,CAAC,CAAC,SAAS;IAAE,WAAW,EAAE,MAAM,EAAE,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,IAClF,CAAC,CAAC,cAAc,CAAC,SAAS,MAAM,GAC5B,CAAC,CAAC,aAAa,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,cAAc,CAAC,GAC5C,CAAC,CAAC,aAAa,CAAC,CAAC,MAAM,CAAC,CAAA;AAE9B,wBAAgB,YAAY,CAC1B,KAAK,CAAC,CAAC,SAAS;IAAE,YAAY,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,EAAE,CAAA;CAAE,GAAG,eAAe,EACjF,OAAO,EAAE,CAAC,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAA;AACpC,wBAAgB,YAAY,CAC1B,KAAK,CAAC,CAAC,SAAS;IAAE,YAAY,CAAC,EAAE,SAAS,CAAC;IAAC,WAAW,EAAE,MAAM,EAAE,CAAA;CAAE,GAAG,eAAe,EACrF,OAAO,EAAE,CAAC,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAA"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.enumArgument = enumArgument;
|
|
4
|
+
function enumArgument(options) {
|
|
5
|
+
return {
|
|
6
|
+
description: options.description +
|
|
7
|
+
`. One value required, valid values are: ${options.validValues.join(', ')}`,
|
|
8
|
+
validateValues: (currentValue, values) => {
|
|
9
|
+
if (values.length == 0) {
|
|
10
|
+
return { valid: false, message: 'a value is required' };
|
|
11
|
+
}
|
|
12
|
+
if (currentValue != options.defaultValue) {
|
|
13
|
+
return { valid: false, message: 'expects a single values but was set multiple times' };
|
|
14
|
+
}
|
|
15
|
+
if (values.length > 1) {
|
|
16
|
+
return {
|
|
17
|
+
valid: false,
|
|
18
|
+
message: 'expects a single value but received ' + values.join(', ')
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
const value = values[0];
|
|
22
|
+
const match = options.validValues.find((v) => v.toLowerCase() === value.toLowerCase());
|
|
23
|
+
if (!match) {
|
|
24
|
+
return {
|
|
25
|
+
valid: false,
|
|
26
|
+
message: `${value} is not one of the allowed values: ${options.validValues.join(', ')}`
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return { valid: true, value: match };
|
|
30
|
+
},
|
|
31
|
+
reduceValues: (current, newValue) => newValue,
|
|
32
|
+
defaultValue: options.defaultValue
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=enumArgument.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enumArgument.js","sourceRoot":"","sources":["../../../src/arguments/enumArgument.ts"],"names":[],"mappings":";;AAaA,oCAqCC;AArCD,SAAgB,YAAY,CAE1B,OAAU;IACV,OAAO;QACL,WAAW,EACT,OAAO,CAAC,WAAW;YACnB,2CAA2C,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;QAC7E,cAAc,EAAE,CACd,YAAqC,EACrC,MAAgB,EACc,EAAE;YAChC,IAAI,MAAM,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;gBACvB,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,qBAAqB,EAAE,CAAA;YACzD,CAAC;YACD,IAAI,YAAY,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC;gBACzC,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,oDAAoD,EAAE,CAAA;YACxF,CAAC;YACD,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACtB,OAAO;oBACL,KAAK,EAAE,KAAK;oBACZ,OAAO,EAAE,sCAAsC,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC;iBACpE,CAAA;YACH,CAAC;YACD,MAAM,KAAK,GAAG,MAAM,CAAC,CAAC,CAAC,CAAA;YACvB,MAAM,KAAK,GAAG,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,KAAK,KAAK,CAAC,WAAW,EAAE,CAAC,CAAA;YACtF,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,OAAO;oBACL,KAAK,EAAE,KAAK;oBACZ,OAAO,EAAE,GAAG,KAAK,sCAAsC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;iBACxF,CAAA;YACH,CAAC;YAED,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,KAAoB,EAAE,CAAA;QACrD,CAAC;QACD,YAAY,EAAE,CAAC,OAAgC,EAAE,QAAqB,EAAE,EAAE,CAAC,QAAQ;QACnF,YAAY,EAAE,OAAO,CAAC,YAAY;KACnC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Argument, PerArgumentArgs } from './argument.js';
|
|
2
|
+
type EnumArrayType<T extends {
|
|
3
|
+
validValues: string[];
|
|
4
|
+
defaultValue?: string[] | undefined;
|
|
5
|
+
}> = T['defaultValue'] extends string[] ? T['validValues'][number] | T['defaultValue'][number] : T['validValues'][number];
|
|
6
|
+
export declare function enumArrayArgument<const O extends {
|
|
7
|
+
defaultValue: string[];
|
|
8
|
+
validValues: string[];
|
|
9
|
+
} & PerArgumentArgs>(options: O): Argument<EnumArrayType<O>[]>;
|
|
10
|
+
export declare function enumArrayArgument<const O extends {
|
|
11
|
+
defaultValue?: undefined;
|
|
12
|
+
validValues: string[];
|
|
13
|
+
} & PerArgumentArgs>(options: O): Argument<EnumArrayType<O>[] | undefined>;
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=enumArrayArgument.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enumArrayArgument.d.ts","sourceRoot":"","sources":["../../../src/arguments/enumArrayArgument.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAmB,MAAM,eAAe,CAAA;AAE1E,KAAK,aAAa,CAAC,CAAC,SAAS;IAAE,WAAW,EAAE,MAAM,EAAE,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,EAAE,GAAG,SAAS,CAAA;CAAE,IACzF,CAAC,CAAC,cAAc,CAAC,SAAS,MAAM,EAAE,GAC9B,CAAC,CAAC,aAAa,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,cAAc,CAAC,CAAC,MAAM,CAAC,GACpD,CAAC,CAAC,aAAa,CAAC,CAAC,MAAM,CAAC,CAAA;AAE9B,wBAAgB,iBAAiB,CAC/B,KAAK,CAAC,CAAC,SAAS;IAAE,YAAY,EAAE,MAAM,EAAE,CAAC;IAAC,WAAW,EAAE,MAAM,EAAE,CAAA;CAAE,GAAG,eAAe,EACnF,OAAO,EAAE,CAAC,GAAG,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;AAC3C,wBAAgB,iBAAiB,CAC/B,KAAK,CAAC,CAAC,SAAS;IAAE,YAAY,CAAC,EAAE,SAAS,CAAC;IAAC,WAAW,EAAE,MAAM,EAAE,CAAA;CAAE,GAAG,eAAe,EACrF,OAAO,EAAE,CAAC,GAAG,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,GAAG,SAAS,CAAC,CAAA"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.enumArrayArgument = enumArrayArgument;
|
|
4
|
+
function enumArrayArgument(options) {
|
|
5
|
+
return {
|
|
6
|
+
description: options.description +
|
|
7
|
+
`. One or more values required, valid values are: ${options.validValues.join(', ')}`,
|
|
8
|
+
validateValues: async (currentValue, values) => {
|
|
9
|
+
if (values.length == 0) {
|
|
10
|
+
return { valid: false, message: 'At least one value is required' };
|
|
11
|
+
}
|
|
12
|
+
const invalidValues = [];
|
|
13
|
+
const validValues = [];
|
|
14
|
+
for (const value of values) {
|
|
15
|
+
const match = options.validValues.find((v) => v.toLowerCase() === value.toLowerCase());
|
|
16
|
+
if (!match) {
|
|
17
|
+
invalidValues.push(value);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
validValues.push(match);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (invalidValues.length > 0) {
|
|
24
|
+
return {
|
|
25
|
+
valid: false,
|
|
26
|
+
message: `${invalidValues.map((v) => `${v}`).join(', ')} is not one of the allowed values: ${options.validValues.join(', ')}`
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return { valid: true, value: validValues };
|
|
30
|
+
},
|
|
31
|
+
reduceValues: async (current, newValues) => {
|
|
32
|
+
if (!current) {
|
|
33
|
+
return newValues;
|
|
34
|
+
}
|
|
35
|
+
current.push(...newValues);
|
|
36
|
+
return current;
|
|
37
|
+
},
|
|
38
|
+
defaultValue: options.defaultValue,
|
|
39
|
+
acceptMultipleValues: () => true
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=enumArrayArgument.js.map
|