From 2145f4d11897953d6321366033cbd4af13cbe91c Mon Sep 17 00:00:00 2001 From: Misha Kaletsky Date: Tue, 18 Jun 2024 09:03:54 -0400 Subject: [PATCH 1/2] feat: support single-character flags --- src/utils.ts | 8 +++---- tests/specs/type-flag.ts | 50 +++++++++++++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index b780204..7bc1dcd 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -75,10 +75,6 @@ const validateFlagName = ( throw new Error(`${errorPrefix} cannot be empty`); } - if (flagName.length === 1) { - throw new Error(`${errorPrefix} must be longer than a character`); - } - const hasReservedCharacter = flagName.match(reservedCharactersPattern); if (hasReservedCharacter) { throw new Error(`${errorPrefix} cannot contain ${stringify(hasReservedCharacter?.[0])}`); @@ -136,6 +132,10 @@ export const createRegistry = ( const { alias } = schema; const errorPrefix = `Flag alias ${stringify(alias)} for flag ${stringify(flagName)}`; + if (flagName.length === 1) { + throw new Error(`${errorPrefix} cannot be defined for a single-character flag`); + } + if (alias.length === 0) { throw new Error(`${errorPrefix} cannot be empty`); } diff --git a/tests/specs/type-flag.ts b/tests/specs/type-flag.ts index 6534f64..d0259d9 100644 --- a/tests/specs/type-flag.ts +++ b/tests/specs/type-flag.ts @@ -13,14 +13,6 @@ export default testSuite(({ describe }) => { }).toThrow(/* 'Invalid flag name: empty' */); }); - test('Single character flag name', () => { - expect(() => { - typeFlag({ - i: String, - }, []); - }).toThrow(/* 'Invalid flag name: single characters are reserved for aliases' */); - }); - test('Reserved characters', () => { expect(() => { typeFlag({ 'flag a': String }, []); @@ -70,6 +62,17 @@ export default testSuite(({ describe }) => { }).toThrow(/* 'Empty alias' */); }); + test('Single-character alias', () => { + expect(() => { + typeFlag({ + a: { + type: String, + alias: 'a', + }, + }, []); + }).toThrow(/* must not be defined for a single-character flag */); + }); + test('Multi-character alias', () => { expect(() => { typeFlag({ @@ -406,6 +409,37 @@ export default testSuite(({ describe }) => { expect(parsed.flags.alias).toStrictEqual(['', '', 'value']); expect(argv).toStrictEqual([]); }); + + test('single-character alias', () => { + const argv = ['-x', '1', '-y', '2']; + const parsed = typeFlag( + { + x: Number, + y: Number, + }, + argv, + ); + + expect(parsed.flags.x).toBe(1); + expect(parsed.flags.y).toBe(2); + }); + + test('single-character alias grouping', () => { + const argv = ['-am', 'hello']; + const parsed = typeFlag( + { + a: Boolean, + message: { + type: String, + alias: 'm', + }, + }, + argv, + ); + + expect(parsed.flags.a).toBe(true); + expect(parsed.flags.message).toBe('hello'); + }); }); test('unknown flags', () => { From 23f0ac794547b363a5f895b29de01b0ff8fb5711 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Thu, 20 Jun 2024 14:22:04 +0900 Subject: [PATCH 2/2] wip --- test-d/type-flag.test-d.ts | 2 ++ tests/specs/type-flag.ts | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/test-d/type-flag.test-d.ts b/test-d/type-flag.test-d.ts index fa7d2ff..5b77e3b 100644 --- a/test-d/type-flag.test-d.ts +++ b/test-d/type-flag.test-d.ts @@ -23,6 +23,7 @@ const parsed = typeFlag({ default: false, description: 'Some description', }, + a: String, }); type ExpectedType = { @@ -34,6 +35,7 @@ type ExpectedType = { numberFlag: number | undefined; numberFlagDefault: number; extraOptions: boolean; + a: string | undefined; }; unknownFlags: { [flag: string]: (string | boolean)[]; diff --git a/tests/specs/type-flag.ts b/tests/specs/type-flag.ts index 6f5aff7..4cb1a71 100644 --- a/tests/specs/type-flag.ts +++ b/tests/specs/type-flag.ts @@ -67,10 +67,13 @@ export default testSuite(({ describe }) => { typeFlag({ a: { type: String, - alias: 'a', + alias: 'b', }, }, []); - }).toThrow(/* must not be defined for a single-character flag */); + }).toThrow( + + /* Flag alias "b" for flag "a" cannot be defined for a single-character flag */ + ); }); test('Multi-character alias', () => {