Skip to content

Commit 2c30939

Browse files
authored
fix(W-16338640): Update logic for backwards compatibility for CLI commands (#139)
* WIP fix consumption of additional flags * Clean up code * Update copy for deprecation warning * Update deprecation message Remove portion of message to use in command so that command specific syntax is not shared amongst other commands
1 parent 5569ee6 commit 2c30939

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/command.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export abstract class Command extends Base {
5959
const nonExistentFlagsWithValues = {...parsed}
6060

6161
if (nonExistentFlags && nonExistentFlags.length > 0) {
62-
this.warn(`Using [${nonExistentFlags}] without a '--' (end of options) preceeding them is deprecated. Please use '--' preceeding the flag(s) meant to be passed-though.`)
62+
this.warn(`You’re using a deprecated syntax with the [${nonExistentFlags}] flag.\nAdd a '--' (end of options) separator before the flags you’re passing through.`)
6363
for (const flag of nonExistentFlags) {
6464
const key = flag.replace('--', '')
6565
delete parsed[key]
@@ -75,6 +75,23 @@ export abstract class Command extends Base {
7575
this.argv = unparser(parsed as unparser.Arguments)
7676
const result = await super.parse(options, argv)
7777
result.nonExistentFlags = unparser(nonExistentFlagsWithValues as unparser.Arguments)
78+
79+
for (let index = 0; index < result.nonExistentFlags.length; index++) {
80+
const positionalValue = result.nonExistentFlags[index]
81+
const doubleHyphenRegex = /^--/
82+
const positionalValueIsFlag = doubleHyphenRegex.test(positionalValue)
83+
if (positionalValueIsFlag) {
84+
const nextElement = result.nonExistentFlags[index + 1] ? result.nonExistentFlags[index + 1] : ''
85+
const nextElementIsFlag = doubleHyphenRegex.test(nextElement)
86+
// eslint-disable-next-line max-depth
87+
if (nextElement && !nextElementIsFlag) {
88+
result.argv.push(`${positionalValue}=${nextElement}`)
89+
} else if (!nextElement || nextElementIsFlag) {
90+
result.argv.push(`${positionalValue}=${true}`)
91+
}
92+
}
93+
}
94+
7895
return result
7996
}
8097
}

0 commit comments

Comments
 (0)