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.

feat: Implement long-form switch parsing

This adds support for long-form switches; a long-form switch works similarly
to a flag switch, but the switch itself is identified by a string encompassing
multiple characters.

To enable a long-form switch, pass its name preceded by a double dash, like so:

```bash
$ node program --long-form-switch
```

Since long-form switches can have arbitrarily long names, they can't be grouped
like flag switches are, and each switch requires a new argument:

```bash
$ node program --switch-one --switch-two
```

+13 -1
+3 -1
src/pargser.ts
··· 3 3 const flags: string[] = []; 4 4 5 5 argv.forEach((argument) => { 6 - if (argument.startsWith('-')) { 6 + if (argument.startsWith('--')) { 7 + flags.push(argument.slice(2)); 8 + } else if (argument.startsWith('-')) { 7 9 flags.push(...argument.slice(1).split('')); 8 10 } 9 11 });
+10
test/pargser.test.ts
··· 28 28 s: true, 29 29 }); 30 30 }); 31 + 32 + it('should handle long-form switches', () => { 33 + expect( 34 + pargser(['node', 'index.js', '--first-flag', '--another', '--thirdly']), 35 + ).toEqual({ 36 + 'first-flag': true, 37 + another: true, 38 + thirdly: true, 39 + }); 40 + }); 31 41 });