extract-flags

Extract command-line arguments from a list, removing matches from the original array

extract-flags

Extract command-line arguments from a list, removing matches from the original array. Useful for providing custom arguments for a wrapper executable, but passing unexpected arguments back onto the original executable if not expected.

Only supports a subset of what you'd get with minimist, so unless you're explicitly looking to modify the arguments array you're better off using that module.

Where argv is an array of arguments, and flags is an object formatted like so:

{
    t: String
  , transform: String
  , debug: Boolean
}

Each key represents a command-line flag, and the value should be either String, Number or Boolean to denote its type. argv will be modified in-place to remove matched flags, and you will get an object in return with the extracted values:

{
    t: ['envify', 'es6ify']
  , transform: ['installify']
  , debug: true
}

Here's a simple example of the kind of thing you might want to use extract-flags for:

var spawn = require('child_process').spawn
var extract = require('extract-flags')
var argv = process.argv
 
var options = extract(argv, {
    'transform': String
  , 'debug': Boolean
})
 
if (options.debug) {
  process.env.NODE_ENV = 'development'
}
 
var ps = spawn('node', argv.slice(1), {
    cwd: process.cwd()
  , env: process.env
})
 
var input = process.stdin
options.transform.forEach(function(tr) {
  input = input.pipe(require(tr)('filename'))
})
input.pipe(process.stdout)

MIT. See LICENSE.md for details.