Skip to content

Commit

Permalink
fix! yargs.parsed now populated before returning, when yargs.parse() …
Browse files Browse the repository at this point in the history
…called with no args (#1382)

BREAKING CHANGE: we now only officially support yargs.$0 parameter and discourage direct access to yargs.parsed
  • Loading branch information
bcoe committed Jul 29, 2019
1 parent 026b151 commit e3981fd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
8 changes: 7 additions & 1 deletion index.js
Expand Up @@ -28,7 +28,13 @@ function singletonify (inst) {
} else if (typeof inst[key] === 'function') {
Argv[key] = inst[key].bind(inst)
} else {
Argv.__defineGetter__(key, () => inst[key])
Argv.__defineGetter__('$0', () => {
return inst.$0
})
Argv.__defineGetter__('parsed', () => {
console.warn('In future major releases of yargs, "parsed" will be a private field. Use the return value of ".parse()" or ".argv" instead')
return inst.parsed
})
}
})
}
9 changes: 8 additions & 1 deletion test/yargs.js
Expand Up @@ -28,7 +28,8 @@ describe('yargs dsl tests', () => {
process.env._ = '/usr/local/bin/ndm'
process.execPath = '/usr/local/bin/ndm'
const argv = yargs([]).parse()
argv['$0'].should.eql('ndm')
argv['$0'].should.equal('ndm')
yargs.$0.should.equal('ndm')
})

it('accepts an object for aliases', () => {
Expand Down Expand Up @@ -862,12 +863,18 @@ describe('yargs dsl tests', () => {

describe('parsed', () => {
it('should be false before parsing', () => {
const warn = global.console.warn
global.console.warn = () => {}
yargs.parsed.should.equal(false)
global.console.warn = warn
})

it('should not be false after parsing', () => {
const warn = global.console.warn
global.console.warn = () => {}
yargs.parse()
yargs.parsed.should.not.equal(false)
global.console.warn = warn
})
})

Expand Down
5 changes: 3 additions & 2 deletions yargs.js
Expand Up @@ -545,7 +545,8 @@ function Yargs (processArgs, cwd, parentRequire) {
if (typeof args === 'undefined') {
const parsed = self._parseArgs(processArgs)
unfreeze()
return parsed
// TODO: remove this compatibility hack when we release yargs@15.x:
return (this.parsed = parsed)
}

// a context object can optionally be provided, this allows
Expand Down Expand Up @@ -1028,7 +1029,7 @@ function Yargs (processArgs, cwd, parentRequire) {
// Deprecated
let pkgConfig = pkgUp()['yargs']
if (pkgConfig) {
console.warn('Configuring yargs through package.json is deprecated and will be removed in the next major release, please use the JS API instead.')
console.warn('Configuring yargs through package.json is deprecated and will be removed in a future major release, please use the JS API instead.')
options.configuration = Object.assign({}, pkgConfig, options.configuration)
}

Expand Down

0 comments on commit e3981fd

Please sign in to comment.