Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support single-character flags #27

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])}`);
Expand Down Expand Up @@ -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`);
}
Expand Down
2 changes: 2 additions & 0 deletions test-d/type-flag.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const parsed = typeFlag({
default: false,
description: 'Some description',
},
a: String,
});

type ExpectedType = {
Expand All @@ -34,6 +35,7 @@ type ExpectedType = {
numberFlag: number | undefined;
numberFlagDefault: number;
extraOptions: boolean;
a: string | undefined;
};
unknownFlags: {
[flag: string]: (string | boolean)[];
Expand Down
54 changes: 45 additions & 9 deletions tests/specs/type-flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }, []);
Expand Down Expand Up @@ -70,6 +62,20 @@ export default testSuite(({ describe }) => {
}).toThrow(/* 'Empty alias' */);
});

test('Single-character alias', () => {
expect(() => {
typeFlag({
a: {
type: String,
alias: 'b',
},
}, []);
}).toThrow(

/* Flag alias "b" for flag "a" cannot be defined for a single-character flag */
);
});

test('Multi-character alias', () => {
expect(() => {
typeFlag({
Expand Down Expand Up @@ -418,11 +424,41 @@ export default testSuite(({ describe }) => {
},
argv,
);

expect<boolean | undefined>(parsed.flags.alias).toBe(undefined);
expect<(string | boolean)[]>(parsed.unknownFlags.a).toStrictEqual([true]);
expect(argv).toStrictEqual([]);
});

test('single-character alias', () => {
const argv = ['-x', '1', '-y', '2'];
const parsed = typeFlag(
{
x: Number,
y: Number,
},
argv,
);

expect<number | undefined>(parsed.flags.x).toBe(1);
expect<number | undefined>(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<boolean | undefined>(parsed.flags.a).toBe(true);
expect<string | undefined>(parsed.flags.message).toBe('hello');
});
});

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