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: store end of flags in -- property #9

Merged
merged 3 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ $ node ./cli --some-string 'hello' --some-boolean --some-number 3
unknownFlags: {
[flagName: string]: (string | boolean)[]
}
_: string[]
_: string[] & {
'--': string[]
}
}
```

Expand Down Expand Up @@ -195,7 +197,7 @@ This outputs the following:
### Arguments
All argument values are stored in the `_` property.

Everything after `--` will not be parsed and be treated as arguments.
Everything after `--` (end-of-flags) is treated as arguments and will be stored in the `_['--']` property.

```sh
$ node ./cli --boolean value --string "hello world" "another value" -- --string "goodbye world"
Expand All @@ -204,7 +206,16 @@ $ node ./cli --boolean value --string "hello world" "another value" -- --string
This outputs the following:
```json5
{
_: ['value', 'another value', '--string', 'goodbye world']
_: [
'value',
'another value',
'--string',
'goodbye world',
'--': [
'--string',
'goodbye world'
]
]
// ...
}
```
Expand Down
11 changes: 8 additions & 3 deletions src/type-flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {

const isAliasPattern = /^-[\da-z]+/i;
const isFlagPattern = /^--[\w-]{2,}/;
const END_OF_FLAGS = '--';

/**
type-flag: typed argv parser
Expand Down Expand Up @@ -44,7 +45,9 @@ export function typeFlag<Schemas extends Flags>(
const parsed: TypeFlag<Schemas> = {
flags: createFlagsObject(schemas),
unknownFlags: {},
_: [],
_: Object.assign([], {
[END_OF_FLAGS]: [],
}),
};

let expectingValue: undefined | ((value?: string | boolean) => void);
Expand Down Expand Up @@ -98,8 +101,10 @@ export function typeFlag<Schemas extends Flags>(
for (let i = 0; i < argv.length; i += 1) {
const argvElement = argv[i];

if (argvElement === '--') {
parsed._.push(...argv.slice(i + 1));
if (argvElement === END_OF_FLAGS) {
const endOfFlags = argv.slice(i + 1);
parsed._[END_OF_FLAGS] = endOfFlags;
parsed._.push(...endOfFlags);
break;
}

Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,7 @@ export type TypeFlag<Schemas extends Flags> = {
unknownFlags: {
[flag: string]: (string | boolean)[];
};
_: string[];
_: string[] & {
'--': string[];
};
};
46 changes: 39 additions & 7 deletions tests/type-flag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('Parsing', () => {
const parsed = typeFlag({}, ['-invalidAlias']);

expect(parsed).toStrictEqual({
_: [],
_: Object.assign([], { '--': [] }),
flags: {},
unknownFlags: {
i: [true, true, true],
Expand All @@ -180,7 +180,14 @@ describe('Parsing', () => {

expect<string | undefined>(parsed.flags.flagA).toBe('');
expect<string | undefined>(parsed.flags.flagB).toBe(undefined);
expect<string[]>(parsed._).toStrictEqual(['--flagB']);
expect<string[]>(parsed._).toStrictEqual(
Object.assign(
['--flagB'],
{
'--': ['--flagB'],
},
),
);
});

test('strings, booleans, numbers', () => {
Expand All @@ -193,7 +200,12 @@ describe('Parsing', () => {
expect<string | undefined>(parsed.flags.string).toBe('');
expect<boolean | undefined>(parsed.flags.boolean).toBe(true);
expect<number | undefined>(parsed.flags.number).toBe(Number.NaN);
expect<string[]>(parsed._).toStrictEqual(['world']);
expect<string[]>(parsed._).toStrictEqual(
Object.assign(
['world'],
{ '--': [] },
),
);
});

test('convert kebab-case to camelCase', () => {
Expand Down Expand Up @@ -226,7 +238,12 @@ describe('Parsing', () => {
expect<string | undefined>(parsed.flags.string).toBe('');
expect<boolean | undefined>(parsed.flags.boolean).toBe(true);
expect<number | undefined>(parsed.flags.number).toBe(Number.NaN);
expect<string[]>(parsed._).toStrictEqual(['world']);
expect<string[]>(parsed._).toStrictEqual(
Object.assign(
['world'],
{ '--': [] },
),
);
});

test('flag: - to allow the use of = in values (or vice versa)', () => {
Expand Down Expand Up @@ -266,7 +283,12 @@ describe('Parsing', () => {
expect<string | undefined>(parsed.flags.string).toBe('');
expect<boolean | undefined>(parsed.flags.boolean).toBe(true);
expect<string[]>(Object.keys(parsed.flags)).toStrictEqual(['string', 'boolean']);
expect<string[]>(parsed._).toStrictEqual(['world']);
expect<string[]>(parsed._).toStrictEqual(
Object.assign(
['world'],
{ '--': [] },
),
);
});

test('unknown flags', () => {
Expand Down Expand Up @@ -309,7 +331,12 @@ describe('Parsing', () => {
unknownString: ['hello', true],
'kebab-case': [true],
});
expect<string[]>(parsed._).toStrictEqual(['a']);
expect<string[]>(parsed._).toStrictEqual(
Object.assign(
['a'],
{ '--': [] },
),
);
});

test('custom type', () => {
Expand Down Expand Up @@ -356,7 +383,12 @@ describe('Parsing', () => {
expect<number | undefined>(parsed.flags.number).toBe(Number.NaN);
expect<string[]>(parsed.flags.stringArray).toStrictEqual(['a', 'b']);
expect<number[]>(parsed.flags.numberArray).toStrictEqual([1, 2]);
expect<string[]>(parsed._).toStrictEqual(['world']);
expect<string[]>(parsed._).toStrictEqual(
Object.assign(
['world'],
{ '--': [] },
),
);
});

describe('Default flag value', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/type-flag.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ExpectedType = {
unknownFlags: {
[flag: string]:(string | boolean)[];
};
_: string[];
_: string[] & { '--': string[] };
};

expectType<ExpectedType>(parsed);