line_count_options.js (635B)
1 #!/usr/bin/env node 2 var argv = require('optimist') 3 .usage('Count the lines in a file.\nUsage: $0') 4 .options({ 5 file : { 6 demand : true, 7 alias : 'f', 8 description : 'Load a file' 9 }, 10 base : { 11 alias : 'b', 12 description : 'Numeric base to use for output', 13 default : 10, 14 }, 15 }) 16 .argv 17 ; 18 19 var fs = require('fs'); 20 var s = fs.createReadStream(argv.file); 21 22 var lines = 0; 23 s.on('data', function (buf) { 24 lines += buf.toString().match(/\n/g).length; 25 }); 26 27 s.on('end', function () { 28 console.log(lines.toString(argv.base)); 29 });