index.cjs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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, processArgv} = require('./build/index.cjs');
  6. Argv(processArgv.hideBin(process.argv));
  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 if (typeof inst[key] === 'function') {
  25. Argv[key] = inst[key].bind(inst);
  26. } else {
  27. Argv.__defineGetter__('$0', () => {
  28. return inst.$0;
  29. });
  30. Argv.__defineGetter__('parsed', () => {
  31. return inst.parsed;
  32. });
  33. }
  34. });
  35. }