Skip to content

Commit

Permalink
fix(W-16338640): Update logic for backwards compatibility for CLI com…
Browse files Browse the repository at this point in the history
…mands (#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
  • Loading branch information
zwhitfield3 authored Aug 13, 2024
1 parent 5569ee6 commit 2c30939
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export abstract class Command extends Base {
const nonExistentFlagsWithValues = {...parsed}

if (nonExistentFlags && nonExistentFlags.length > 0) {
this.warn(`Using [${nonExistentFlags}] without a '--' (end of options) preceeding them is deprecated. Please use '--' preceeding the flag(s) meant to be passed-though.`)
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.`)
for (const flag of nonExistentFlags) {
const key = flag.replace('--', '')
delete parsed[key]
Expand All @@ -75,6 +75,23 @@ export abstract class Command extends Base {
this.argv = unparser(parsed as unparser.Arguments)
const result = await super.parse(options, argv)
result.nonExistentFlags = unparser(nonExistentFlagsWithValues as unparser.Arguments)

for (let index = 0; index < result.nonExistentFlags.length; index++) {
const positionalValue = result.nonExistentFlags[index]
const doubleHyphenRegex = /^--/
const positionalValueIsFlag = doubleHyphenRegex.test(positionalValue)
if (positionalValueIsFlag) {
const nextElement = result.nonExistentFlags[index + 1] ? result.nonExistentFlags[index + 1] : ''
const nextElementIsFlag = doubleHyphenRegex.test(nextElement)
// eslint-disable-next-line max-depth
if (nextElement && !nextElementIsFlag) {
result.argv.push(`${positionalValue}=${nextElement}`)
} else if (!nextElement || nextElementIsFlag) {
result.argv.push(`${positionalValue}=${true}`)
}
}
}

return result
}
}
Expand Down

0 comments on commit 2c30939

Please sign in to comment.