Ruby-like ARGF for Node.js

tokuhirom's node-argf module offers a Ruby-like ARGF for Node.js.

Install via:

npm install argf

or by adding

{
  "dependencies" : {
    "argf" : "latest"
  }
}

to your package.json file.

Use ARGF like this:

ARGF = require('argf');
argf = new ARGF();  // create argf based on current
                    // command line parameters or
                    // input streams.

// register a callback for when all input data has been read
argf.on('finished', function() {
  console.log("Done processing all inputs.");
});

// process the input(s)
argf.forEach( function(line) {
  console.log("Read:",line);
  console.log("From source:",argv.stream.path);
}

Like Ruby's ARGF, the module assumes any elements in process.argv represent files to process (and uses the input stream if no files are provided.

You can also pass an array to new ARGF() to provide the list of files, which is handy if you're using something like node-optimist. (Note that in node-optimist you can use argv._ to get the remaining parameters after parsing.) For example:

optimist = require('optimist');
ARGF = require('argf');

options = {
  # ...
}
argv = optimist.usage('Usage: $0 ...', options).argv;

argf = new ARGF(argv._);

argf.on('finished', function() {
  console.log("Done processing all inputs.");
});

// process the input(s)
argf.forEach( function(line) {
  console.log("Read:",line);
  console.log("From source:",argv.stream.path);
}
Published 3 Mar 2013

 

This page was generated at 4:16 PM on 26 Feb 2018.
Copyright © 1999 - 2018 Rodney Waldhoff.