index.js 1.1 KB

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