Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const VALUES = Symbol('VALUES');
- const Parser = (argv, { subcommands } = {}) => {
- argv = argv.slice(2);
- return {
- get (key) {
- if (key === VALUES) {
- return argv.filter((x, i) =>
- (!subcommands || i !== 0) && !x.startsWith('-')
- && (!argv[i - 1] || !argv[i - 1].startsWith('-')));
- } else if (key.startsWith('-')) {
- if (key.startsWith('--')) {
- const i = argv.findIndex(x => x === key);
- if (i === -1) return null;
- if (argv[i + 1] && !argv[i+1].startsWith('-'))
- return argv[i + 1];
- return true;
- }
- } else {
- if (subcommands && argv[0] === key)
- return argv.slice(1).findIndex(x => x === key) !== -1;
- if (argv.findIndex((x, i) =>
- x === key && (!argv[i - 1] || !argv[i-1].startsWith('-'))
- ) !== -1) return true;
- }
- return null;
- }
- };
- };
- /* -- */
- const parser = Parser(process.argv);
- console.log(parser.get("--thing"));
- /* -- */
- module.exports = Parser;
- module.exports.VALUES = VALUES;
Add Comment
Please, Sign In to add comment