Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FlagParser {
- static parse(args) {
- // guard
- if (false === Array.isArray(args)) { return []; }
- // Creates a flag from KeyValue pair
- let makeFlag = (kv) => {
- let b = {}
- let k = kv[0]
- let v = kv[1]
- // Test empty key, but not value
- if (k.length === 0 && v.length > 0) {
- k = v
- v = true
- }
- // Test one entry
- if (kv.length === 1) {
- k = kv[0]
- v = true
- }
- // set the object
- b[k] = v;
- return b;
- }
- let result = {}
- Array.from(args)
- .slice(2)
- .forEach(v => {
- let a = v.replace(/-{1,2}/g, "").split(/=/g) || [];
- result = Object.assign(result, makeFlag(a));
- });
- return result;
- }
- }
- function test_parser() {
- console.log(
- FlagParser.parse(
- [
- 'C:\\Program Files\\nodejs\\node.exe',
- 'path\\to\\file.js',
- 'flagA=1',
- '--flagB=1',
- '--flagC',
- '-flagD',
- '-flagE=2'
- ])
- )
- }
Add Comment
Please, Sign In to add comment