@@ -1518,25 +1518,22 @@ const options = {
1518
1518
' logfile' : { type: ' string' },
1519
1519
' no-logfile' : { type: ' boolean' },
1520
1520
};
1521
- const { values , tokens } = parseArgs ({ options, tokens: true });
1521
+ const { values , tokens = [] } = parseArgs ({ options, tokens: true });
1522
1522
1523
1523
// Reprocess the option tokens and overwrite the returned values.
1524
- tokens
1525
- .filter ((token ) => token .kind === ' option' )
1526
- .forEach ((token ) => {
1527
- if (token .name .startsWith (' no-' )) {
1528
- // Store foo:false for --no-foo
1529
- const positiveName = token .name .slice (3 );
1530
- values[positiveName] = false ;
1531
- delete values[token .name ];
1532
- } else {
1533
- // Resave value so last one wins if both --foo and --no-foo.
1534
- values[token .name ] = token .value ?? true ;
1535
- }
1536
- });
1524
+ const { color , logfile = ' default.log' } = tokens
1525
+ .filter (({ kind }) => kind === ' option' )
1526
+ .reduce ((acc , { name, value }) => {
1527
+
1528
+ const { [name ]: _ , ... rest } = acc;
1529
+
1530
+ const negate = name .startsWith (' no-' );
1531
+ const k = negate ? name .slice (3 ) : name;
1532
+ const v = negate ? false : (value ?? true );
1533
+
1534
+ return { ... rest, [k]: v };
1537
1535
1538
- const color = values .color ;
1539
- const logfile = values .logfile ?? ' default.log' ;
1536
+ }, values);
1540
1537
1541
1538
console .log ({ logfile, color });
1542
1539
` ` `
@@ -1550,25 +1547,22 @@ const options = {
1550
1547
' logfile' : { type: ' string' },
1551
1548
' no-logfile' : { type: ' boolean' },
1552
1549
};
1553
- const { values , tokens } = parseArgs ({ options, tokens: true });
1550
+ const { values , tokens = [] } = parseArgs ({ options, tokens: true });
1554
1551
1555
1552
// Reprocess the option tokens and overwrite the returned values.
1556
- tokens
1557
- .filter ((token ) => token .kind === ' option' )
1558
- .forEach ((token ) => {
1559
- if (token .name .startsWith (' no-' )) {
1560
- // Store foo:false for --no-foo
1561
- const positiveName = token .name .slice (3 );
1562
- values[positiveName] = false ;
1563
- delete values[token .name ];
1564
- } else {
1565
- // Resave value so last one wins if both --foo and --no-foo.
1566
- values[token .name ] = token .value ?? true ;
1567
- }
1568
- });
1553
+ const { color , logfile = ' default.log' } = tokens
1554
+ .filter (({ kind }) => kind === ' option' )
1555
+ .reduce ((acc , { name, value }) => {
1556
+
1557
+ const { [name ]: _ , ... rest } = acc;
1558
+
1559
+ const negate = name .startsWith (' no-' );
1560
+ const k = negate ? name .slice (3 ) : name;
1561
+ const v = negate ? false : (value ?? true );
1562
+
1563
+ return { ... rest, [k]: v };
1569
1564
1570
- const color = values .color ;
1571
- const logfile = values .logfile ?? ' default.log' ;
1565
+ }, values);
1572
1566
1573
1567
console .log ({ logfile, color });
1574
1568
` ` `
0 commit comments