line_count_options.js 635 B

1234567891011121314151617181920212223242526272829
  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. var fs = require('fs');
  19. var s = fs.createReadStream(argv.file);
  20. var lines = 0;
  21. s.on('data', function (buf) {
  22. lines += buf.toString().match(/\n/g).length;
  23. });
  24. s.on('end', function () {
  25. console.log(lines.toString(argv.base));
  26. });