Skip to content

Commit

Permalink
fix(docs): describe usage of .check() in more detail
Browse files Browse the repository at this point in the history
I added an example of usage that was distilled from my own code.

Co-authored-by: Benjamin E. Coe <bencoe@google.com>
  • Loading branch information
roryokane and bcoe committed May 10, 2020
1 parent 69f29a9 commit 932cd11
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions docs/api.md
Expand Up @@ -87,12 +87,27 @@ Check that certain conditions are met in the provided arguments.

`fn` is called with two arguments, the parsed `argv` hash and an array of options and their aliases.

If `fn` throws or returns a non-truthy value, show the thrown error, usage information, and
exit.
If `fn` throws or returns a non-truthy value, Yargs will show the thrown error
and usage information. Yargs will then exit, unless
[`.exitProcess()`](#exitprocess) was used to prevent Yargs from exiting after a
failed check.

`global` indicates whether `check()` should be enabled both
at the top-level and for each sub-command.

```js
const argv = require('yargs')
.check((argv, options) => {
const filePaths = argv._
if (filePaths.length > 1) {
throw new Error("Only 0 or 1 files may be passed.")
} else {
return true // tell Yargs that the arguments passed the check
}
})
.argv
```

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

Expand Down

0 comments on commit 932cd11

Please sign in to comment.