l0bsterssg

node.js static responsive blog post generator
Log | Files | Refs | README

line_count_wrap.js (759B)


      1 #!/usr/bin/env node
      2 var argv = require('optimist')
      3     .usage('Count the lines in a file.\nUsage: $0')
      4     .wrap(80)
      5     .demand('f')
      6     .alias('f', [ 'file', 'filename' ])
      7     .describe('f',
      8         "Load a file. It's pretty important."
      9         + " Required even. So you'd better specify it."
     10     )
     11     .alias('b', 'base')
     12     .describe('b', 'Numeric base to display the number of lines in')
     13     .default('b', 10)
     14     .describe('x', 'Super-secret optional parameter which is secret')
     15     .default('x', '')
     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 });