A class and functions to quickly write a loop over standard input or a list of files. Heavilly inspired by the fileinput module of Python.
$ npm install fileinput
var fileinput = require('fileinput');
fileinput.input()
.on('line', function(line) {
console.log( fileinput.lineno(), line.toString('utf8') );
});
This will read files specified in process.argv
and emit each line to the
event line. When no filenames are found in argv it will default to reading
from stdin (this can also be achieved by using -
as a filename).
Files can also be passed to the .input() function directly:
var fileinput = require('fileinput');
var lines = 0;
fileinput.input(['index.js', 'lib/fileinput.js')
.on('line', function(line) {
if(line.toString().trim().length > 0) {
lines += 1;
}
})
.on('end', function() {
console.log('lines:', lines);
});
Or use it in combination with commander:
var fileinput = require('fileinput');
var program = require('commander');
program.version('1.0.0')
.usage('[options] file...')
.option('-v, --verbose', 'output line')
.parse(process.argv);
fileinput.input(program)
.on('line', function(line) {
if(program.verbose) {
process.stdout.write(line.toString());
}
});
See examples/
from more uses.
Read the examples, tests and source for now, sorry...
- Support stdin reading for Windows (it now uses /dev/stdin)