@@ -42,14 +42,6 @@ export const parseFlags = (args: string[]): ConfigFlags => {
42
42
}
43
43
}
44
44
45
- // to find unknown / unrecognized arguments we filter `args`, including only
46
- // arguments whose normalized form is not found in `knownArgs`. `knownArgs`
47
- // is populated during the call to `parseArgs` above. For arguments like
48
- // `--foobar` the string `"--foobar"` will be added, while for more
49
- // complicated arguments like `--bizBoz=bop` or `--bizBoz bop` just the
50
- // string `"--bizBoz"` will be added.
51
- flags . unknownArgs = flags . args . filter ( ( arg : string ) => ! flags . knownArgs . includes ( parseEqualsArg ( arg ) [ 0 ] ) ) ;
52
-
53
45
return flags ;
54
46
} ;
55
47
@@ -131,13 +123,13 @@ const parseCLITerm = (flags: ConfigFlags, args: string[]) => {
131
123
else if ( arg . startsWith ( '-' ) && arg . includes ( '=' ) ) {
132
124
// we're dealing with an AliasEqualsArg, we have a special helper for that
133
125
const [ originalArg , value ] = parseEqualsArg ( arg ) ;
134
- setCLIArg ( flags , arg . split ( '=' ) [ 0 ] , normalizeFlagName ( originalArg ) , value ) ;
126
+ setCLIArg ( flags , desugarRawAlias ( originalArg ) , normalizeFlagName ( originalArg ) , value ) ;
135
127
}
136
128
137
129
// AliasArg → "-" AliasName ( " " CLIValue )? ;
138
130
else if ( CLI_FLAG_REGEX . test ( arg ) ) {
139
131
// this is a short alias, like `-c` for Config
140
- setCLIArg ( flags , arg , normalizeFlagName ( arg ) , parseCLIValue ( args ) ) ;
132
+ setCLIArg ( flags , desugarRawAlias ( arg ) , normalizeFlagName ( arg ) , parseCLIValue ( args ) ) ;
141
133
}
142
134
143
135
// NegativeDashArg → "--no-" ArgName ;
@@ -164,13 +156,14 @@ const parseCLITerm = (flags: ConfigFlags, args: string[]) => {
164
156
// SimpleArg → "--" ArgName ( " " CLIValue )? ;
165
157
else if ( arg . startsWith ( '--' ) && arg . length > '--' . length ) {
166
158
setCLIArg ( flags , arg , normalizeFlagName ( arg ) , parseCLIValue ( args ) ) ;
159
+ } else {
160
+ // if we get here then `arg` is not an argument in our list of supported
161
+ // arguments. This doesn't necessarily mean we want to report an error or
162
+ // anything though! Instead, with unknown / unrecognized arguments we want
163
+ // to stick them into the `unknownArgs` array, which is used when we pass
164
+ // CLI args to Jest, for instance.
165
+ flags . unknownArgs . push ( arg ) ;
167
166
}
168
-
169
- // if we get here it is not an argument in our list of supported arguments.
170
- // This doesn't necessarily mean we want to report an error or anything
171
- // though! Instead, with unknown / unrecognized arguments we stick them into
172
- // the `unknownArgs` array, which is used when we pass CLI args to Jest, for
173
- // instance. So we just return void here.
174
167
} ;
175
168
176
169
/**
@@ -219,7 +212,7 @@ const normalizeFlagName = (flagName: string): string => {
219
212
* @param value the raw value to be set onto the config flags object
220
213
*/
221
214
const setCLIArg = ( flags : ConfigFlags , rawArg : string , normalizedArg : string , value : CLIValueResult ) => {
222
- normalizedArg = dereferenceAlias ( normalizedArg ) ;
215
+ normalizedArg = desugarAlias ( normalizedArg ) ;
223
216
224
217
// We're setting a boolean!
225
218
if ( readOnlyArrayHasStringMember ( BOOLEAN_CLI_FLAGS , normalizedArg ) ) {
@@ -320,6 +313,14 @@ const setCLIArg = (flags: ConfigFlags, rawArg: string, normalizedArg: string, va
320
313
} else {
321
314
throwCLIParsingError ( rawArg , 'expected to receive a valid log level but received nothing' ) ;
322
315
}
316
+ } else {
317
+ // we haven't found this flag in any of our lists of arguments, so we
318
+ // should put it in our list of unknown arguments
319
+ flags . unknownArgs . push ( rawArg ) ;
320
+
321
+ if ( typeof value === 'string' ) {
322
+ flags . unknownArgs . push ( value ) ;
323
+ }
323
324
}
324
325
} ;
325
326
@@ -355,9 +356,10 @@ type CLIValueResult = string | typeof Empty;
355
356
* A little helper which tries to parse a CLI value (as opposed to a flag) off
356
357
* of the argument array.
357
358
*
358
- * We support a variety of different argument formats, but all of them start
359
- * with `-`, so we can check the first character to test whether the next token
360
- * in our array of CLI arguments is a flag name or a value.
359
+ * We support a variety of different argument formats for flags (as opposed to
360
+ * values), but all of them start with `-`, so we can check the first character
361
+ * to test whether the next token in our array of CLI arguments is a flag name
362
+ * or a value.
361
363
*
362
364
* @param args an array of CLI args
363
365
* @returns either a string result or an Empty sentinel
@@ -454,22 +456,35 @@ const throwNumberParsingError = (flag: string, value: string) => {
454
456
} ;
455
457
456
458
/**
457
- * A little helper to 'dereference' a flag alias, which if you squint a little
458
- * you can think of like a pointer to a full flag name. Thus 'c' is like a
459
- * pointer to 'config', so here we're doing something like `*c`. Of course, this
460
- * being JS, this is just a metaphor!
459
+ * A little helper to 'desugar' a flag alias, meaning expand it to its full
460
+ * name. For instance, the alias `"c"` will desugar to `"config"`.
461
461
*
462
- * If no 'dereference' is found for the possible alias we just return the
463
- * passed string unmodified.
462
+ * If no expansion is found for the possible alias we just return the passed
463
+ * string unmodified.
464
464
*
465
465
* @param maybeAlias a string which _could_ be an alias to a full flag name
466
466
* @returns the full aliased flag name, if found, or the passed string if not
467
467
*/
468
- const dereferenceAlias = ( maybeAlias : string ) : string => {
469
- const possibleDereference = CLI_FLAG_ALIASES [ maybeAlias ] ;
468
+ const desugarAlias = ( maybeAlias : string ) : string => {
469
+ const possiblyDesugared = CLI_FLAG_ALIASES [ maybeAlias ] ;
470
470
471
- if ( typeof possibleDereference === 'string' ) {
472
- return possibleDereference ;
471
+ if ( typeof possiblyDesugared === 'string' ) {
472
+ return possiblyDesugared ;
473
473
}
474
474
return maybeAlias ;
475
475
} ;
476
+
477
+ /**
478
+ * Desugar a 'raw' alias (with a leading dash) and return an equivalent,
479
+ * desugared argument.
480
+ *
481
+ * For instance, passing `"-c` will return `"--config"`.
482
+ *
483
+ * The reason we'd like to do this is not so much for our own code, but so that
484
+ * we can transform an alias like `"-u"` to `"--updateSnapshot"` in order to
485
+ * pass it along to Jest.
486
+ *
487
+ * @param rawAlias a CLI flag alias as found on the command line (like `"-c"`)
488
+ * @returns an equivalent full command (like `"--config"`)
489
+ */
490
+ const desugarRawAlias = ( rawAlias : string ) : string => '--' + desugarAlias ( normalizeFlagName ( rawAlias ) ) ;
0 commit comments