Yargs记录了一面旗帜

时间:2015-03-25 01:43:14

标签: node.js command-line-interface yargs

我试图找出如何记录和别名一个不使用yargs

的值的参数

我想要做的是别名-c--compile,并且能够记录--compile。如果--compile

script sources -c

我原以为它是这样的

  var argv = require('yargs')
    .usage('Usage: $0 <input> [options]')
    .example('$0 src/**.js -c', 'Generate a build')
    .demand(1)

    .boolean('compile')
    .alias('compile', ['c'])
    .nargs('c', 1)
    .describe('compile', 'Whether to compile the results')

    .version(function() {
      return require('../package').version;
    })
    .argv;

但是,调用script sources -c会产生错误

TypeError: Cannot read property 'newAliases' of undefined
    at Object.self.help (/home/gyeates/code/lodash.modularize/node_modules/yargs/lib/usage.js:135:45)
    at Object.self.showHelp (/home/gyeates/code/lodash.modularize/node_modules/yargs/lib/usage.js:211:29)
    at Object.Argv.self.showHelp (/home/gyeates/code/lodash.modularize/node_modules/yargs/index.js:303:15)
    at Object.self.fail (/home/gyeates/code/lodash.modularize/node_modules/yargs/lib/usage.js:37:39)

1 个答案:

答案 0 :(得分:1)

摆脱nargs('c', 1)。该方法指定密钥后应消耗的参数数量,在本例中为1.我们不希望密钥采用任何值。

var argv = require('yargs')
  .usage('Usage: $0 <input> [options]')
  .example('$0 src/**.js -c', 'Generate a build')
  .demand(1)

  .boolean('compile')
  .alias('compile', ['c'])
  .describe('compile', 'Whether to compile the results')

  .version(function() {
    return require('../package').version;
  })
  .argv;

可以找到有关yargs方法的更多信息here