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

docs: only use parse and argv at top level #1990

Merged
merged 4 commits into from Sep 5, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/api.md
Expand Up @@ -87,6 +87,8 @@ If `yargs` is executed in an environment that embeds node and there's no script
expects it to be the script name. In order to override this behavior, use `.parse(process.argv.slice(1))`
instead of `.argv` and the first parameter won't be ignored.

***Note:*** `.argv` should only be used at the top level, not inside a command's builder function.

<a name="array"></a>.array(key)
----------

Expand Down Expand Up @@ -336,6 +338,8 @@ yargs
.argv
```

***Note:*** `.parse()` and `.argv` should only be used at the top level, not inside a command's builder function.

Please see [Advanced Topics: Commands](https://github.com/yargs/yargs/blob/master/docs/advanced.md#commands) for a thorough
discussion of the advanced features exposed in the Command API.

Expand Down Expand Up @@ -1344,6 +1348,8 @@ the resulting error and output will not be passed to the `parse()` callback (the
returning a promise. If your use case requires `parse()` to be called several times, any asynchronous
operation performed in a command handler should not result in the handler returning a promise.

***Note:*** `.parse()` should only be used at the top level, not inside a command's builder function.

.parseAsync([args], [context], [parseCallback])
------------

Expand Down
54 changes: 54 additions & 0 deletions example/nested.js
@@ -0,0 +1,54 @@
const argv = require('yargs/yargs')(process.argv.slice(2)).command(
'math',
'math description',
yargs =>
yargs
.command(
'add <a> <b>',
'add description',
yargs =>
yargs
.positional('a', {
describe: 'addend "a"',
type: 'number',
default: 0,
})
.positional('b', {
describe: 'addend "b"',
type: 'number',
default: 0,
}),
argv => {
const {a, b} = argv;
console.log(`${a} + ${b} = ${a + b}`);
}
)
.command(
'sum <numbers..>',
'sum description',
yargs =>
yargs
.positional('numbers', {
describe: 'numbers to sum',
type: 'array',
default: [],
})
.check(argv =>
isArrayOfNumbers(argv.numbers)
? true
: 'Positional argument "numbers" must only contain numbers'
),
argv => {
const sum = argv.numbers.reduce((a, b) => a + b, 0);
console.log(`The sum of numbers is ${sum}`);
}
)
).argv;

console.log(argv);

function isArrayOfNumbers(arr) {
return Array.isArray(arr) && arr.every(n => typeof n === 'number');
}

// NOTE: ".argv" and ".parse()" should only be used at top level, not inside builder functions.