Guest User

Untitled

a guest
Dec 11th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. const VALUES = Symbol('VALUES');
  2.  
  3. const Parser = (argv, { subcommands } = {}) => {
  4. argv = argv.slice(2);
  5. return {
  6. get (key) {
  7. if (key === VALUES) {
  8. return argv.filter((x, i) =>
  9. (!subcommands || i !== 0) && !x.startsWith('-')
  10. && (!argv[i - 1] || !argv[i - 1].startsWith('-')));
  11. } else if (key.startsWith('-')) {
  12. if (key.startsWith('--')) {
  13. const i = argv.findIndex(x => x === key);
  14. if (i === -1) return null;
  15. if (argv[i + 1] && !argv[i+1].startsWith('-'))
  16. return argv[i + 1];
  17. return true;
  18. }
  19. } else {
  20. if (subcommands && argv[0] === key)
  21. return argv.slice(1).findIndex(x => x === key) !== -1;
  22. if (argv.findIndex((x, i) =>
  23. x === key && (!argv[i - 1] || !argv[i-1].startsWith('-'))
  24. ) !== -1) return true;
  25. }
  26. return null;
  27. }
  28. };
  29. };
  30.  
  31. /* -- */
  32. const parser = Parser(process.argv);
  33. console.log(parser.get("--thing"));
  34. /* -- */
  35.  
  36. module.exports = Parser;
  37. module.exports.VALUES = VALUES;
Add Comment
Please, Sign In to add comment