From 4d4ec180e76e28322c0d2086cc489359365b45cf Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Thu, 20 Jun 2024 14:13:07 +0900 Subject: [PATCH] fix: disallow accepting aliases with -- BREAKING CHANGE: no longer supports the edge-case --a where a is an alias --- src/type-flag.ts | 4 +++- tests/specs/type-flag.ts | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/type-flag.ts b/src/type-flag.ts index be9e339..915d532 100644 --- a/src/type-flag.ts +++ b/src/type-flag.ts @@ -56,7 +56,9 @@ export const typeFlag = ( argvIterator(argv, { onFlag(name, explicitValue, flagIndex) { - const isKnownFlag = hasOwn(flagRegistry, name); + const isAlias = flagIndex.length === 3; + const isValid = isAlias || name.length > 1; + const isKnownFlag = isValid && hasOwn(flagRegistry, name); if ( ignore?.( isKnownFlag ? KNOWN_FLAG : UNKNOWN_FLAG, diff --git a/tests/specs/type-flag.ts b/tests/specs/type-flag.ts index 6534f64..d5e51c8 100644 --- a/tests/specs/type-flag.ts +++ b/tests/specs/type-flag.ts @@ -406,6 +406,23 @@ export default testSuite(({ describe }) => { expect(parsed.flags.alias).toStrictEqual(['', '', 'value']); expect(argv).toStrictEqual([]); }); + + test('should not accept aliases with --', () => { + const argv = ['--a']; + const parsed = typeFlag( + { + alias: { + type: Boolean, + alias: 'a', + }, + }, + argv, + ); + + expect(parsed.flags.alias).toBe(undefined); + expect<(string | boolean)[]>(parsed.unknownFlags.a).toStrictEqual([true]); + expect(argv).toStrictEqual([]); + }); }); test('unknown flags', () => {