Skip to content

Commit f16a077

Browse files
committed
doc: refine sample code on tokens of util/parseArgs
1 parent 484ad83 commit f16a077

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

doc/api/util.md

+26-32
Original file line numberDiff line numberDiff line change
@@ -1518,25 +1518,22 @@ const options = {
15181518
'logfile': { type: 'string' },
15191519
'no-logfile': { type: 'boolean' },
15201520
};
1521-
const { values, tokens } = parseArgs({ options, tokens: true });
1521+
const { values, tokens = [] } = parseArgs({ options, tokens: true });
15221522

15231523
// 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 };
15371535

1538-
const color = values.color;
1539-
const logfile = values.logfile ?? 'default.log';
1536+
}, values);
15401537

15411538
console.log({ logfile, color });
15421539
```
@@ -1550,25 +1547,22 @@ const options = {
15501547
'logfile': { type: 'string' },
15511548
'no-logfile': { type: 'boolean' },
15521549
};
1553-
const { values, tokens } = parseArgs({ options, tokens: true });
1550+
const { values, tokens = [] } = parseArgs({ options, tokens: true });
15541551

15551552
// 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 };
15691564

1570-
const color = values.color;
1571-
const logfile = values.logfile ?? 'default.log';
1565+
}, values);
15721566

15731567
console.log({ logfile, color });
15741568
```

0 commit comments

Comments
 (0)