Skip to content

Commit 373edad

Browse files
authored
Merge pull request #1491 from oclif/mdonnalley/allow-no-with-alias
feat(W-17318341): support allowNo with alias
2 parents 8df097a + d6de26f commit 373edad

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

src/parser/parse.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ const validateOptions = (flag: OptionFlag<any>, input: string): string => {
114114
return input
115115
}
116116

117+
const NEGATION = '--no-'
118+
117119
export class Parser<
118120
T extends ParserInput,
119121
TFlags extends OutputFlags<T['flags']>,
@@ -397,15 +399,19 @@ export class Parser<
397399
if (tokenLength) {
398400
// boolean
399401
if (fws.inputFlag.flag.type === 'boolean' && last(fws.tokens)?.input) {
402+
const doesNotContainNegation = (i: FlagWithStrategy) => {
403+
const possibleNegations = [i.inputFlag.name, ...(i.inputFlag.flag.aliases ?? [])].map(
404+
(n) => `${NEGATION}${n}`,
405+
)
406+
const input = last(i.tokens)?.input
407+
if (!input) return true
408+
return !possibleNegations.includes(input)
409+
}
410+
400411
return {
401412
...fws,
402413
valueFunction: async (i) =>
403-
parseFlagOrThrowError(
404-
last(i.tokens)?.input !== `--no-${i.inputFlag.name}`,
405-
i.inputFlag.flag,
406-
this.context,
407-
last(i.tokens),
408-
),
414+
parseFlagOrThrowError(doesNotContainNegation(i), i.inputFlag.flag, this.context, last(i.tokens)),
409415
}
410416
}
411417

@@ -652,9 +658,14 @@ export class Parser<
652658
return this.flagAliases[name].name
653659
}
654660

655-
if (arg.startsWith('--no-')) {
656-
const flag = this.booleanFlags[arg.slice(5)]
661+
if (arg.startsWith(NEGATION)) {
662+
const flag = this.booleanFlags[arg.slice(NEGATION.length)]
657663
if (flag && flag.allowNo) return flag.name
664+
665+
const flagAlias = this.flagAliases[arg.slice(NEGATION.length)]
666+
if (flagAlias && flagAlias.type === 'boolean' && flagAlias.allowNo) {
667+
return flagAlias.name
668+
}
658669
}
659670
}
660671

test/parser/parse.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,7 +1806,7 @@ See more help with --help`)
18061806
expect(out.flags.foo).to.equal(undefined)
18071807
})
18081808

1809-
it('is false', async () => {
1809+
it('is false when negation is used', async () => {
18101810
const out = await parse(['--no-foo'], {
18111811
flags: {
18121812
foo: Flags.boolean({allowNo: true}),
@@ -1815,14 +1815,23 @@ See more help with --help`)
18151815
expect(out.flags.foo).to.equal(false)
18161816
})
18171817

1818-
it('is true', async () => {
1818+
it('is true when negation is not used', async () => {
18191819
const out = await parse(['--foo'], {
18201820
flags: {
18211821
foo: Flags.boolean({allowNo: true}),
18221822
},
18231823
})
18241824
expect(out.flags.foo).to.equal(true)
18251825
})
1826+
1827+
it('is false with negation of alias', async () => {
1828+
const out = await parse(['--no-bar'], {
1829+
flags: {
1830+
foo: Flags.boolean({char: 'f', allowNo: true, aliases: ['bar']}),
1831+
},
1832+
})
1833+
expect(out.flags.foo).to.equal(false)
1834+
})
18261835
})
18271836

18281837
describe('fs flags', () => {

0 commit comments

Comments
 (0)