Toy parser of command line arguments. Part of the "toys" collection.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: tighten error boundaries

An empty long-form switch (`--`) is commonly used to end argument parsing and
formard the remaining arguments verbatim to the next step in the pipeline.

Since this is just a toy parser, it will simply ignore the double dash for the
time being.

Argument ingestion is not happening anyway, since the parsed arguments remain
in the `argv` array.

+16 -4
+3 -1
src/pargser.ts
··· 3 3 4 4 argv.forEach((argument) => { 5 5 if (argument.startsWith('--')) { 6 - parsedArguments[argument.slice(2)] = true; 6 + if (argument.length > 2) { 7 + parsedArguments[argument.slice(2)] = true; 8 + } 7 9 } else if (argument.startsWith('-')) { 8 10 const flags = argument.slice(1).split(''); 9 11 flags.forEach((flag) => {
+13 -3
test/pargser.test.ts
··· 46 46 }); 47 47 }); 48 48 49 - it('should ignore empty keys', () => { 50 - expect(pargser(['node', 'program.js', 'foo=bar', '=baz'])).toEqual({ 51 - foo: 'bar', 49 + describe('Error boundaries', () => { 50 + it('should ignore empty keys', () => { 51 + expect(pargser(['node', 'program.js', 'foo=bar', '=baz'])).toEqual({ 52 + foo: 'bar', 53 + }); 54 + }); 55 + 56 + it('should ignore empty flag names', () => { 57 + expect(pargser(['node', 'program.js', '-'])).toEqual({}); 58 + }); 59 + 60 + it('should ignore empty long-form switches', () => { 61 + expect(pargser(['node', 'program.js', '--'])).toEqual({}); 52 62 }); 53 63 }); 54 64 });