megawac
megawac

Reputation: 11353

Yargs documenting a flag

I was trying to figure out how to document and alias a param that takes no values with yargs

What I want to do is alias -c to --compile and be able to document --compile. If --compile

script sources -c

I was expecting it to be something like this

  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;

However, calling script sources -c will generate an error

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)

Upvotes: 3

Views: 3853

Answers (1)

gregnr
gregnr

Reputation: 1272

Get rid of nargs('c', 1). That method specifies the number of arguments that should be consumed after a key, in this case 1. We don't want the key to take any values.

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;

More information on yargs methods can be found here.

Upvotes: 4

Related Questions