which 985 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env node
  2. var which = require("../")
  3. if (process.argv.length < 3)
  4. usage()
  5. function usage () {
  6. console.error('usage: which [-as] program ...')
  7. process.exit(1)
  8. }
  9. var all = false
  10. var silent = false
  11. var dashdash = false
  12. var args = process.argv.slice(2).filter(function (arg) {
  13. if (dashdash || !/^-/.test(arg))
  14. return true
  15. if (arg === '--') {
  16. dashdash = true
  17. return false
  18. }
  19. var flags = arg.substr(1).split('')
  20. for (var f = 0; f < flags.length; f++) {
  21. var flag = flags[f]
  22. switch (flag) {
  23. case 's':
  24. silent = true
  25. break
  26. case 'a':
  27. all = true
  28. break
  29. default:
  30. console.error('which: illegal option -- ' + flag)
  31. usage()
  32. }
  33. }
  34. return false
  35. })
  36. process.exit(args.reduce(function (pv, current) {
  37. try {
  38. var f = which.sync(current, { all: all })
  39. if (all)
  40. f = f.join('\n')
  41. if (!silent)
  42. console.log(f)
  43. return pv;
  44. } catch (e) {
  45. return 1;
  46. }
  47. }, 0))