Command-line Configuration with confab and yargs

  • 3/1/2016

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!

Hey, I'm RJ! For more learnings about software and management, find me @rjzaworski or sign up for my semi-regular newsletter.

Let’s keep in touch

Send me timely updates on software, product, and process.