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

Allow repeated config options #461

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions lib/yargs-parser.ts
Expand Up @@ -659,8 +659,13 @@ export class YargsParser {
applyDefaultsAndAliases(configLookup, flags.aliases, defaults)

Object.keys(flags.configs).forEach(function (configKey) {
const configPath = argv[configKey] || configLookup[configKey]
if (configPath) {
var configPaths = argv[configKey] || configLookup[configKey]

if (typeof configPaths === "string") {
configPaths = [configPaths]
}

configPaths.forEach(function (configPath : string) {
try {
let config = null
const resolvedConfigPath = mixin.resolve(mixin.cwd(), configPath)
Expand All @@ -687,7 +692,7 @@ export class YargsParser {
if (ex.name === 'PermissionDenied') error = ex
else if (argv[configKey]) error = Error(__('Invalid JSON config file: %s', configPath))
}
}
})
})
}

Expand Down
13 changes: 13 additions & 0 deletions test/yargs-parser.cjs
Expand Up @@ -597,6 +597,19 @@ describe('yargs-parser', function () {
argv.should.have.property('foo').and.deep.equal('bar')
})

// see https://github.com/yargs/yargs-parser/issues/430
it("should allow repeated config options", function () {
const argv = parser(['--settings', jsonPath, '--settings', jsonPath], {
alias: {
z: 'zoom'
},
config: ['settings']
})

argv.should.have.property('herp', 'derp')
argv.should.have.property('zoom', 55)
})

it('should load options and values from a JS file when config has .js extention', function () {
const jsPath = path.resolve(__dirname, './fixtures/settings.cjs')
const argv = parser(['--settings', jsPath, '--foo', 'bar'], {
Expand Down
13 changes: 13 additions & 0 deletions test/yargs-parser.mjs
Expand Up @@ -112,6 +112,19 @@ describe('yargs-parser (esm)', function () {
argv.should.have.property('foo').and.deep.equal('bar')
})

// see https://github.com/yargs/yargs-parser/issues/430
it("should allow repeated config options", function () {
const argv = parser(['--settings', jsonPath, '--settings', jsonPath], {
alias: {
z: 'zoom'
},
config: ['settings']
})

argv.should.have.property('herp', 'derp')
argv.should.have.property('zoom', 55)
})

// for esm, only support importing json files
it('should fail to load options and values from a JS file when config has .js extention', function () {
const jsPath = path.resolve(__dirname, './fixtures/settings.cjs')
Expand Down