Skip to content

Instantly share code, notes, and snippets.

@itacirgabral
Created December 16, 2021 14:40
Show Gist options
  • Save itacirgabral/eef7ffc5bdc6d289d895189b28b0b201 to your computer and use it in GitHub Desktop.
Save itacirgabral/eef7ffc5bdc6d289d895189b28b0b201 to your computer and use it in GitHub Desktop.
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