index.js 853 B

12345678910111213141516171819202122232425262728293031
  1. // classic singleton yargs API, to use yargs
  2. // without running as a singleton do:
  3. // require('yargs/yargs')(process.argv.slice(2))
  4. const yargs = require('./yargs')
  5. Argv(process.argv.slice(2))
  6. module.exports = Argv
  7. function Argv (processArgs, cwd) {
  8. const argv = yargs(processArgs, cwd, require)
  9. singletonify(argv)
  10. return argv
  11. }
  12. /* Hack an instance of Argv with process.argv into Argv
  13. so people can do
  14. require('yargs')(['--beeble=1','-z','zizzle']).argv
  15. to parse a list of args and
  16. require('yargs').argv
  17. to get a parsed version of process.argv.
  18. */
  19. function singletonify (inst) {
  20. Object.keys(inst).forEach(function (key) {
  21. if (key === 'argv') {
  22. Argv.__defineGetter__(key, inst.__lookupGetter__(key))
  23. } else {
  24. Argv[key] = typeof inst[key] === 'function' ? inst[key].bind(inst) : inst[key]
  25. }
  26. })
  27. }