Yargs: allow to pass yargs-parser config programmatically

Created on 17 Mar 2017  路  4Comments  路  Source: yargs/yargs

not only read from package.json, maybe:

yargs.config({ camel-case-expansion: true}).parse(argv)

enhancement

Most helpful comment

BTW, the pkg file you guess at https://github.com/yargs/yargs/blob/master/yargs.js#L490,

plz provide a getter for it~

All 4 comments

@atian25 this seems like a reasonable request ... I wonder if we could even introduce a new method, e.g., parserConfig, with the caveat that I want to (within reason) avoid adding any more documentation into README.md until we refactor our docs into multiple .md files 馃槢

BTW, the pkg file you guess at https://github.com/yargs/yargs/blob/master/yargs.js#L490,

plz provide a getter for it~

we have similar problems with using yargs with mocha tests in and IDE like webstorm. The problem is that the package.json file that's always found by yargs pkgUp is mocha's package file, not ours. So when we run tests this way, they fail to find their properties because the configuration options we specify for yargs don't get loaded. If we could also pass them in programmatically then we can get the tests to work properly. This used to work with yargs but stopped working correctly some time ago. We've tried using the cwd parameter, but that doesn't seem to work as a result of the fix here: https://github.com/yargs/yargs/issues/720

In the meantime, here's a possible workaround:

yargs-parser-options.js :

import yargsParser from 'yargs-parser';

const originalParsingFunction = yargsParser.detailed;

export default function setYargsParserConfig(configuration) {
  if (!configuration)
    yargsParser.detailed = originalParsingFunction;
  else
    yargsParser.detailed = args => originalParsingFunction(args, { configuration });
}

test.js

import yargs from 'yargs';
import setYargsParserConfig from './yargs-parser-options';

setYargsParserConfig({ 'camel-case-expansion': false });
console.log(yargs.parse([ '--camel-case' ]));
// => { _: [], 'camel-case': true,'$0': 'test.js' }

setYargsParserConfig(); // reset default
console.log(yargs.parse([ '--camel-case' ]));
// => { _: [], 'camel-case': true, camelCase: true, '$0': 'test.js' }
Was this page helpful?
0 / 5 - 0 ratings