Skip to content

Commit

Permalink
fix: disallow accepting aliases with --
Browse files Browse the repository at this point in the history
BREAKING CHANGE: no longer supports the edge-case --a where a is an alias
  • Loading branch information
privatenumber committed Jun 20, 2024
1 parent d328656 commit 4d4ec18
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/type-flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ export const typeFlag = <Schemas extends Flags>(

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,
Expand Down
17 changes: 17 additions & 0 deletions tests/specs/type-flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,23 @@ export default testSuite(({ describe }) => {
expect<string[]>(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<string | undefined>(parsed.flags.alias).toBe(undefined);

Check failure on line 422 in tests/specs/type-flag.ts

View workflow job for this annotation

GitHub Actions / Test

Argument of type 'boolean | undefined' is not assignable to parameter of type 'string | undefined'.
expect<(string | boolean)[]>(parsed.unknownFlags.a).toStrictEqual([true]);
expect(argv).toStrictEqual([]);
});
});

test('unknown flags', () => {
Expand Down

0 comments on commit 4d4ec18

Please sign in to comment.