minimist-string
v1.0.2
Published
A minimist extension to parse command line sentences as strings
Downloads
2,506
Readme
minimist-string
minimist-string
is a minimist wrapper that is able to pasrse command line sentences as strings. The problem with minimist
is that you need to give the arguments in an array with every argument in separated strings (as in node's process.argv
). The following wouldn't work:
console.log(minimist(['foo --bar "Hello!"']));
// { _: [ 'foo --bar "Hello!"' ] } wich is not what we want
The next logical step is doing:
console.log(minimist('foo --bar "Hello!"'.split(' ')));
// { _: [ 'foo' ], bar: '"Hello!"' }
That actually returns what we expect. The problem comes when the user-defined string has spaces:
console.log(minimist('foo --bar "Hello world!"'.split(' ')));
// { _: [ 'foo', 'world!"' ], bar: '"Hello' }
Only "Hello
gets to the bar
parameter, while the rest of it gets to argv._
, wich is a disaster. minimist-string
solves this problem:
Usage
npm install --save minimist-string
const parseSentence = require('minimist-string');
console.log(parseSentence('foo --bar "Hello world!"'));
// { _: [ 'foo' ], bar: 'Hello world!' }
It even works with escaped quotes!
console.log(parseSentence('foo --bar "Hello \\"world\\"!"'));
// { _: [ 'foo' ], bar: 'Hello "world"!' }