Skip to content

Commit

Permalink
Allow repeated config options
Browse files Browse the repository at this point in the history
FIXES yargs#430
  • Loading branch information
nwf-msr committed Sep 14, 2022
1 parent 3aba24c commit 7a46377
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
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

0 comments on commit 7a46377

Please sign in to comment.