Created
December 16, 2021 14:40
-
-
Save itacirgabral/eef7ffc5bdc6d289d895189b28b0b201 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 minimist = require('minimist'); | |
console.log(`text=${text}`) | |
// https://stackoverflow.com/questions/16261635/javascript-split-string-by-space-but-ignore-space-in-quotes-notice-not-to-spli#answer-46946490 | |
const idxAt = text.indexOf(' ') | |
const arg = text | |
.slice(idxAt + 1) | |
.match(/\\?.|^$/g) | |
.reduce((p, c) => { | |
if(c === '"'){ | |
p.quote ^= 1; | |
}else if(!p.quote && c === ' '){ | |
p.a.push(''); | |
}else{ | |
p.a[p.a.length-1] += c.replace(/\\(.)/,"$1"); | |
} | |
return p; | |
}, | |
{a: ['']} | |
).a | |
console.log(`arg=${arg}`) | |
const conf = minimist(arg) | |
console.dir(conf) | |
\* | |
{ | |
_: ['mandabotao' ], | |
o: 'pa', | |
grande: 'com espaço' | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment