Command-line configuration with confab and yargs

confab is a great tool for configuring node.js applications, but it doesn’t ship with support for command-line flags. CLIs can vary quite a bit from one application to the next, and confab does not make assumptions (or force opinions) about their design.

Fortunately, there are plenty of other argument-processing tools that can be easily integrated with confab. One such is yargs, which (in addition to a pirate motif and a laundry list of other useful features) exposes command-line arguments as the yargs.argv object:

var argv = require('yargs').argv;

$ node index.js --foo=baz

Since argv is just an object, integrating with confab requires an almost-trivial use of the assign transform:

var confab = require('confab');
var argv = require('yargs').argv;
var pick = require('lodash.pick');

  foo: 'bar'


var config = confab([
  confab.assign(pick(argv, CONFIG_KEYS)),

process.stdout.write(JSON.stringify(config, null, 2) + '\n');


No surprises here.

$ node index.js
{ foo: 'bar' }

$ node index.js --foo=baz
{ foo: 'baz' }

Just like that, we’ve bolstered our configuration with command-line overrides. CLI support is missing no longer–we just needed to set it up!

Let’s keep in touch

Reach out on Twitter or subscribe for (very) occasional updates.

Hey, I'm RJ: digital entomologist and intermittent micropoet, writing from the beautiful Rose City.