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

doc: refine sample code on tokens of util/parseArgs #49263

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
58 changes: 26 additions & 32 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -1518,25 +1518,22 @@ const options = {
'logfile': { type: 'string' },
'no-logfile': { type: 'boolean' },
};
const { values, tokens } = parseArgs({ options, tokens: true });
const { values, tokens = [] } = parseArgs({ options, tokens: true });
imcotton marked this conversation as resolved.
Show resolved Hide resolved

// Reprocess the option tokens and overwrite the returned values.
tokens
.filter((token) => token.kind === 'option')
.forEach((token) => {
if (token.name.startsWith('no-')) {
// Store foo:false for --no-foo
const positiveName = token.name.slice(3);
values[positiveName] = false;
delete values[token.name];
} else {
// Resave value so last one wins if both --foo and --no-foo.
values[token.name] = token.value ?? true;
}
});
const { color, logfile = 'default.log' } = tokens
.filter(({ kind }) => kind === 'option')
.reduce((acc, { name, value }) => {

const { [name]: _, ...rest } = acc;

const negate = name.startsWith('no-');
const k = negate ? name.slice(3) : name;
const v = negate ? false : (value ?? true);

return { ...rest, [k]: v };

const color = values.color;
const logfile = values.logfile ?? 'default.log';
}, values);

console.log({ logfile, color });
```
Expand All @@ -1550,25 +1547,22 @@ const options = {
'logfile': { type: 'string' },
'no-logfile': { type: 'boolean' },
};
const { values, tokens } = parseArgs({ options, tokens: true });
const { values, tokens = [] } = parseArgs({ options, tokens: true });
imcotton marked this conversation as resolved.
Show resolved Hide resolved

// Reprocess the option tokens and overwrite the returned values.
tokens
.filter((token) => token.kind === 'option')
.forEach((token) => {
if (token.name.startsWith('no-')) {
// Store foo:false for --no-foo
const positiveName = token.name.slice(3);
values[positiveName] = false;
delete values[token.name];
} else {
// Resave value so last one wins if both --foo and --no-foo.
values[token.name] = token.value ?? true;
}
});
const { color, logfile = 'default.log' } = tokens
.filter(({ kind }) => kind === 'option')
.reduce((acc, { name, value }) => {

const { [name]: _, ...rest } = acc;

const negate = name.startsWith('no-');
const k = negate ? name.slice(3) : name;
const v = negate ? false : (value ?? true);

return { ...rest, [k]: v };

const color = values.color;
const logfile = values.logfile ?? 'default.log';
}, values);

console.log({ logfile, color });
```
Expand Down