Skip to content

Commit

Permalink
doc: switch legacy .argv to .parse() (#2346)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Jan 5, 2024
1 parent 4266485 commit 0c95f9c
Show file tree
Hide file tree
Showing 36 changed files with 120 additions and 126 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -61,7 +61,7 @@ npm i yargs@next
#!/usr/bin/env node
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const argv = yargs(hideBin(process.argv)).argv
const argv = yargs(hideBin(process.argv)).parse()

if (argv.ships > 3 && argv.distance < 53.5) {
console.log('Plunder more riffiwobbles!')
Expand Down
32 changes: 16 additions & 16 deletions docs/advanced.md
Expand Up @@ -18,7 +18,7 @@ const argv = require('yargs/yargs')(process.argv.slice(2))
.command('$0', 'the default command', () => {}, (argv) => {
console.log('this command will be run by default')
})
.argv
.parse()
```

The command defined above will be executed if the program
Expand All @@ -31,7 +31,7 @@ const argv = require('yargs/yargs')(process.argv.slice(2))
.command(['serve', '$0'], 'the serve command', () => {}, (argv) => {
console.log('this command will be run by default')
})
.argv
.parse()
```

The command defined above will be executed if the program
Expand All @@ -47,7 +47,7 @@ take the form `[bar]`. The parsed positional arguments will be populated in
```js
yargs.command('get <source> [proxy]', 'make a get HTTP request')
.help()
.argv
.parse()
```

#### Positional Argument Aliases
Expand All @@ -59,7 +59,7 @@ an email as the first argument:
```js
yargs.command('get <username|email> [password]', 'fetch a user by username or email.')
.help()
.argv
.parse()
```

In this way, both `argv.username` and `argv.email` would be populated with the
Expand All @@ -73,7 +73,7 @@ values, by using the `..` operator:
```js
yargs.command('download <url> [files..]', 'download several files')
.help()
.argv
.parse()
```

#### Describing Positional Arguments
Expand All @@ -91,7 +91,7 @@ yargs.command('get <source> [proxy]', 'make a get HTTP request', (yargs) => {
})
})
.help()
.argv
.parse()
```

### Command Execution
Expand Down Expand Up @@ -140,7 +140,7 @@ require('yargs/yargs')(process.argv.slice(2))
.demandCommand()
.help()
.wrap(72)
.argv
.parse()
```

```
Expand Down Expand Up @@ -196,15 +196,15 @@ You then register the module like so:
```js
yargs.command(require('my-module'))
.help()
.argv
.parse()
```

Or if the module does not export `command` and `describe` (or if you just want to override them):

```js
yargs.command('get <source> [proxy]', 'make a get HTTP request', require('my-module'))
.help()
.argv
.parse()
```

#### Testing a Command Module
Expand Down Expand Up @@ -311,7 +311,7 @@ require('yargs/yargs')(process.argv.slice(2))
.commandDir('cmds')
.demandCommand()
.help()
.argv
.parse()
```

cmds/init.js:
Expand Down Expand Up @@ -391,7 +391,7 @@ import { commands } from './cmds/index.mjs';

yargs(hideBin(process.argv))
.command(commands)
.argv;
.parse();
```

<a name="configuration"></a>
Expand Down Expand Up @@ -419,7 +419,7 @@ const configPath = findUp.sync(['.myapprc', '.myapprc.json'])
const config = configPath ? JSON.parse(fs.readFileSync(configPath)) : {}
const argv = require('yargs/yargs')(process.argv.slice(2))
.config(config)
.argv
.parse()
```

### Providing Configuration in Your package.json
Expand Down Expand Up @@ -447,7 +447,7 @@ method:
```js
const argv = require('yargs/yargs')(process.argv.slice(2))
.pkgConf('nyc')
.argv
.parse()
```

### Creating a Plugin Architecture
Expand Down Expand Up @@ -552,13 +552,13 @@ var argv = require('yargs/yargs')(process.argv.slice(2))
},
[normalizeCredentials]
)
.argv;
.parse();
```

## Using Yargs with Async/await

If you use async middleware or async builders/handlers for commands, `yargs.parse` and
`yargs.argv` will return a `Promise`. When you `await` this promise the
If you use async middleware or async builders/handlers for commands, `yargs.parse()`
will return a `Promise`. When you `await` this promise the
parsed arguments object will be returned after the handler completes:

```js
Expand Down

0 comments on commit 0c95f9c

Please sign in to comment.