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

fix! yargs.parsed now populated before returning, when yargs.parse() called with no args #1382

Merged
merged 1 commit into from Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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