You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
860 B

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