Last active
July 18, 2021 16:28
-
-
Save jrobinsonc/67bc4acf39c4f3abd539d9c9a83436cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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