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

feat: expose hideBin helper for CJS #1768

Merged
merged 13 commits into from Oct 12, 2020
30 changes: 24 additions & 6 deletions README.md
Expand Up @@ -21,9 +21,22 @@ Yargs helps you build interactive command line tools, by parsing arguments and g
It gives you:

* commands and (grouped) options (`my-program.js serve --port=5000`).
* a dynamically generated help menu based on your arguments.
* a dynamically generated help menu based on your arguments:

> <img width="400" src="https://raw.githubusercontent.com/yargs/yargs/master/screen.png">
```
mocha [spec..]

Run tests with Mocha

Commands
mocha inspect [spec..] Run tests with Mocha [default]
mocha init <path> create a client-side Mocha setup at <path>

Rules & Behavior
--allow-uncaught Allow uncaught errors to propagate [boolean]
--async-only, -A Require all tests to use a callback (async) or
return a Promise [boolean]
```

* bash-completion shortcuts for commands and options.
* and [tons more](/docs/api.md).
Expand All @@ -46,7 +59,9 @@ npm i yargs@next

```javascript
#!/usr/bin/env node
const {argv} = require('yargs')
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const argv = yargs(hideBin(process.argv)).argv

if (argv.ships > 3 && argv.distance < 53.5) {
console.log('Plunder more riffiwobbles!')
Expand All @@ -67,7 +82,10 @@ Retreat from the xupptumblers!

```javascript
#!/usr/bin/env node
require('yargs') // eslint-disable-line
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')

yargs(hideBin(process.argv))
.command('serve [port]', 'start the server', (yargs) => {
yargs
.positional('port', {
Expand Down Expand Up @@ -108,7 +126,7 @@ As of `v16`, `yargs` supports [Deno](https://github.com/denoland/deno):
import yargs from 'https://deno.land/x/yargs/deno.ts'
import { Arguments, YargsType } from 'https://deno.land/x/yargs/types.ts'

yargs()
yargs(Deno.args)
.command('download <files...>', 'download a list of files', (yargs: YargsType) => {
return yargs.positional('files', {
describe: 'a list of files to do something with'
Expand All @@ -118,7 +136,7 @@ yargs()
})
.strictCommands()
.demandCommand(1)
.parse(Deno.args)
.argv
```

### ESM
Expand Down
23 changes: 7 additions & 16 deletions docs/advanced.md
Expand Up @@ -14,7 +14,7 @@ commands. tldr; default commands allow you to define the entry point to your
application using a similar API to subcommands.

```js
const argv = require('yargs')
const argv = require('yargs/yargs')(process.argv.slice(2))
.command('$0', 'the default command', () => {}, (argv) => {
console.log('this command will be run by default')
})
Expand All @@ -27,7 +27,7 @@ is run with `./my-cli.js --x=22`.
Default commands can also be used as a command alias, like so:

```js
const argv = require('yargs')
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')
})
Expand Down Expand Up @@ -124,7 +124,7 @@ line, the command will be executed.

```js
#!/usr/bin/env node
require('yargs')
require('yargs/yargs')(process.argv.slice(2))
.command(['start [app]', 'run', 'up'], 'Start up an app', {}, (argv) => {
console.log('starting up the', argv.app || 'default', 'app')
})
Expand Down Expand Up @@ -305,7 +305,7 @@ cli.js:

```js
#!/usr/bin/env node
require('yargs')
require('yargs/yargs')(process.argv.slice(2))
.commandDir('cmds')
.demandCommand()
.help()
Expand Down Expand Up @@ -383,7 +383,7 @@ const findUp = require('find-up')
const fs = require('fs')
const configPath = findUp.sync(['.myapprc', '.myapprc.json'])
const config = configPath ? JSON.parse(fs.readFileSync(configPath)) : {}
const argv = require('yargs')
const argv = require('yargs/yargs')(process.argv.slice(2))
.config(config)
.argv
```
Expand Down Expand Up @@ -411,7 +411,7 @@ Yargs gives you this functionality using the [`pkgConf()`](/docs/api.md#config)
method:

```js
const argv = require('yargs')
const argv = require('yargs/yargs')(process.argv.slice(2))
.pkgConf('nyc')
.argv
```
Expand Down Expand Up @@ -521,7 +521,7 @@ yargs.middleware(normalizeCredentials)
#### yargs parsing configuration

```js
var argv = require('yargs')
var argv = require('yargs/yargs')(process.argv.slice(2))
.usage('Usage: $0 <command> [options]')
.command('login', 'Authenticate user', (yargs) =>{
return yargs.option('username')
Expand All @@ -533,12 +533,3 @@ var argv = require('yargs')
)
.argv;
```

### Using the non-singleton interface

To use yargs without running as a singleton, do:
```js
const argv = require('yargs/yargs')(process.argv.slice(2))
```

This is especially useful when using yargs in a library, as library authors should not pollute the global state.