5
5
* @license MIT
6
6
*/
7
7
8
- import { Args , parse } from "../../deps.ts "
8
+ import { ArgsParser } from "@cross/utils/args "
9
9
10
10
/**
11
11
* Parses command line arguments and returns a parsed object.
12
12
*
13
13
* @param args - An array of command line arguments.
14
14
* @returns - A parsed object containing the command line arguments.
15
15
*/
16
- function parseArguments ( args : string [ ] ) : Args {
17
- // All boolean arguments
18
- const booleanArgs = [
19
- "setup" ,
20
- "upgrade" ,
21
- "help" ,
22
- "autostart" ,
23
-
24
- "dry-run" ,
25
- ]
26
-
27
- // All string arguments
28
- const stringArgs = [
29
- "version" ,
30
- "config" ,
31
- "watch" ,
32
- "cmd" ,
33
- "worker" ,
34
- "cwd" ,
35
- "id" ,
36
-
37
- "cron" ,
38
- "terminate" ,
39
-
40
- "instances" ,
41
- "start-port" ,
42
- "common-port" ,
43
- "strategy" ,
44
- "stdout" ,
45
- "stderr" ,
46
-
47
- "unsafely-ignore-certificate-errors" ,
48
- ]
49
-
50
- // All collection arguments
51
- const collectArgs = [
52
- "env" ,
53
- ]
54
-
55
- // And a list of aliases
56
- const alias = {
57
- "version" : "v" ,
58
- "help" : "h" ,
59
- "id" : "I" ,
60
- "autostart" : "A" ,
61
- "config" : "c" ,
62
- "cmd" : "C" ,
63
- "worker" : "W" ,
64
- "watch" : "w" ,
65
- "cron" : "O" ,
66
- "terminate" : "T" ,
67
- "cwd" : "d" ,
68
- "update" : "upgrade" ,
69
- "env" : "e" ,
16
+ function parseArguments ( args : string [ ] ) : ArgsParser {
17
+ const aliases = {
18
+ "v" : "version" ,
19
+ "h" : "help" ,
20
+ "I" : "id" ,
21
+ "A" : "autostart" ,
22
+ "c" : "config" ,
23
+ "C" : "cmd" ,
24
+ "W" : "worker" ,
25
+ "w" : "watch" ,
26
+ "O" : "cron" ,
27
+ "T" : "terminate" ,
28
+ "d" : "cwd" ,
29
+ "upgrade" : "update" ,
30
+ "e" : "env" ,
70
31
}
71
32
72
- return parse ( args , { alias , boolean : booleanArgs , string : stringArgs , collect : collectArgs , stopEarly : false , "--" : true } )
33
+ return new ArgsParser ( args , { aliases } )
73
34
}
74
35
75
36
/**
@@ -78,7 +39,7 @@ function parseArguments(args: string[]): Args {
78
39
* @returns - The parsed and checked arguments.
79
40
* @throws - An error if any of the arguments are invalid.
80
41
*/
81
- function checkArguments ( args : Args ) : Args {
42
+ function checkArguments ( args : ArgsParser ) : ArgsParser {
82
43
const validBaseArguments = [
83
44
"init" ,
84
45
"append" ,
@@ -126,28 +87,28 @@ function checkArguments(args: Args): Args {
126
87
]
127
88
128
89
// Check that the base argument is either undefined or valid
129
- const baseArgument = args . _ [ 0 ]
90
+ const baseArgument = args . getRest ( )
130
91
if ( baseArgument !== undefined && ( typeof baseArgument !== "string" || ! validBaseArguments . includes ( baseArgument ) ) ) {
131
92
throw new Error ( `Invalid base argument: ${ baseArgument } ` )
132
93
}
133
94
134
- const hasDoubleDashCmd = args [ "--" ] && args [ "--" ] . length > 0
135
- const hasCmd = hasDoubleDashCmd || args . cmd || args . worker
95
+ const hasDoubleDashCmd = args . hasRest ( )
96
+ const hasCmd = hasDoubleDashCmd || args . get ( " cmd" ) || args . get ( " worker" )
136
97
const expectConfigOptions = baseArgument === "init" || baseArgument === "append" || ( baseArgument === "run" && hasCmd )
137
98
const expectInstallerOptions = baseArgument === "setup" || baseArgument === "upgrade" || baseArgument === "update"
138
99
139
100
// Only one type of command can be present at the same time
140
- if ( ( hasDoubleDashCmd && args . cmd ) || ( args . cmd && args . worker ) || ( hasDoubleDashCmd && args . worker ) ) {
101
+ if ( ( hasDoubleDashCmd && args . get ( " cmd" ) ) || ( args . get ( " cmd" ) && args . get ( " worker" ) ) || ( hasDoubleDashCmd && args . get ( " worker" ) ) ) {
141
102
throw new Error ( "'--cmd', '--worker' and '--' cannot be used at the same time." )
142
103
}
143
104
144
105
// Certain base arguments require --id
145
- if ( ! args . id && ( baseArgument === "init" || baseArgument === "append" || baseArgument === "remove" ) ) {
106
+ if ( ! args . get ( "id" ) && ( baseArgument === "init" || baseArgument === "append" || baseArgument === "remove" ) ) {
146
107
throw new Error ( "Arguments 'init', 'append', and 'remove' require '--id'" )
147
108
}
148
109
149
110
// Init and append require a command
150
- if ( ( args . init || args . append ) && ! hasCmd ) {
111
+ if ( ( args . get ( " init" ) || args . get ( " append" ) ) && ! hasCmd ) {
151
112
throw new Error ( "Arguments 'init' and 'append' requires '--cmd' or '--worker'" )
152
113
}
153
114
@@ -158,27 +119,27 @@ function checkArguments(args: Args): Args {
158
119
159
120
// All arguments in processOptions require that init, append, cmd och worker is used
160
121
for ( const opt of processOptions ) {
161
- if ( args [ opt ] && ! expectConfigOptions ) {
122
+ if ( args . get ( opt ) && ! expectConfigOptions ) {
162
123
throw new Error ( `Argument '--${ opt } ' requires 'init', 'append', '--cmd' or '--worker'` )
163
124
}
164
125
}
165
126
166
127
// All arguments in installerOptions require that setup or upgrade (or update) is used
167
128
for ( const opt of installerOptions ) {
168
- if ( args [ opt ] && ! expectInstallerOptions ) {
129
+ if ( args . get ( opt ) && ! expectInstallerOptions ) {
169
130
throw new Error ( `Argument '--${ opt } ' requires 'setup' or 'upgrade'` )
170
131
}
171
132
}
172
133
173
134
// All arguments in numericArguments must be numeric
174
135
for ( const opt of numericArguments ) {
175
- if ( args [ opt ] && isNaN ( Number ( args [ opt ] ) ) ) {
136
+ if ( args . get ( opt ) && isNaN ( Number ( args . get ( opt ) ) ) ) {
176
137
throw new Error ( `Argument '--${ opt } ' must be a numeric value` )
177
138
}
178
139
}
179
140
180
141
// --env flag can only be used with 'service install' base argument
181
- if ( args . env && ( baseArgument !== "install" ) ) {
142
+ if ( args . get ( " env" ) && ( baseArgument !== "install" ) ) {
182
143
throw new Error ( "Argument '--env' can only be used with 'service install' base argument" )
183
144
}
184
145
0 commit comments