Skip to content

Instantly share code, notes, and snippets.

@jrobinsonc
Last active July 18, 2021 16:28
Show Gist options
  • Save jrobinsonc/67bc4acf39c4f3abd539d9c9a83436cc to your computer and use it in GitHub Desktop.
Save jrobinsonc/67bc4acf39c4f3abd539d9c9a83436cc to your computer and use it in GitHub Desktop.
const parseArgs = (slice = 0) => {
return process.argv.slice(slice).reduce((map, item, index) => {
let match = null;
switch (true) {
case (match = item.match(/^(?:\-\-([a-z]+)|\-([a-z]))(?:=(.+))?$/)) !== null:
const [, longName, shortName, value] = match;
const parsedValue = typeof value === 'undefined' ? true : value;
const parsedName = typeof longName === 'undefined' ? shortName : longName;
map.set(parsedName, parsedValue);
break;
default:
map.set(index, item);
break;
}
return map;
}, new Map)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment