Skip to content

Commit

Permalink
feat: allow parse with no arguments as alias for yargs.argv (#944)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Sep 3, 2017
1 parent 792564d commit a9f03e7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
8 changes: 7 additions & 1 deletion docs/api.md
Expand Up @@ -20,6 +20,12 @@ or use `.parse()` to do the same thing:
require('yargs').parse([ '-x', '1', '-y', '2' ])
````

Calling `.parse()` with no arguments is equivalent to calling `yargs.argv`:

```javascript
require('yargs').parse()
```

The rest of these methods below come in just before the terminating `.argv`.

<a name="alias"></a>.alias(key, alias)
Expand Down Expand Up @@ -1007,7 +1013,7 @@ Valid `opt` keys include:
- `'number'`: synonymous for `number: true`, see [`number()`](#number)
- `'string'`: synonymous for `string: true`, see [`string()`](#string)

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

Parse `args` instead of `process.argv`. Returns the `argv` object.
Expand Down
11 changes: 11 additions & 0 deletions test/yargs.js
Expand Up @@ -791,6 +791,17 @@ describe('yargs dsl tests', () => {
})
a1.x.should.equal(42)
})

it('parses process.argv if no arguments are provided', () => {
const r = checkOutput(() => {
yargs(['--help'])
.command('blerg', 'blerg command')
.wrap(null)
.parse()
})

r.logs[0].should.match(/Commands:[\s\S]*blerg command/)
})
})

// yargs.parse(['foo', '--bar'], function (err, argv, output) {}
Expand Down
3 changes: 2 additions & 1 deletion yargs.js
Expand Up @@ -511,7 +511,8 @@ function Yargs (processArgs, cwd, parentRequire) {
let parseFn = null
let parseContext = null
self.parse = function parse (args, shortCircuit, _parseFn) {
argsert('<string|array> [function|boolean|object] [function]', [args, shortCircuit, _parseFn], arguments.length)
argsert('[string|array] [function|boolean|object] [function]', [args, shortCircuit, _parseFn], arguments.length)
if (typeof args === 'undefined') args = processArgs

// a context object can optionally be provided, this allows
// additional information to be passed to a command handler.
Expand Down

0 comments on commit a9f03e7

Please sign in to comment.