argv-split
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "argv-split", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Split argv(argument vector) and handle special cases, such as quoted values.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,3 +9,3 @@ # argv-split [![NPM version](https://badge.fury.io/js/argv-split.svg)](http://badge.fury.io/js/argv-split) [![Build Status](https://travis-ci.org/kaelzhang/node-argv-split.svg?branch=master)](https://travis-ci.org/kaelzhang/node-argv-split) [![Dependency Status](https://gemnasium.com/kaelzhang/node-argv-split.svg)](https://gemnasium.com/kaelzhang/node-argv-split) | ||
'--abc "a b c"'.split(' '); | ||
// -> ['--abc', '"a', 'b', 'c"'] -> Oooooooooops! | ||
// ['--abc', '"a', 'b', 'c"'] -> Oooooooooops! | ||
``` | ||
@@ -25,7 +25,30 @@ | ||
split('--abc "a b c"'); | ||
// ['--abc', 'a b c'] | ||
// ['--abc', 'a b c'], Oh yeah !!!! | ||
``` | ||
### split(string) | ||
Splits a string, and balance quoted parts. | ||
```js | ||
split('--abc "a \'b\' c"'); // ['--abc', "a 'b' c"] | ||
split('--abc "a b c'); // ['--abc', '"a', 'b', 'c'] | ||
``` | ||
### split.balance(array) | ||
Balances an array and join incorrect splited parts. | ||
```js | ||
split.balance(['--abc', '"a', 'b"']); // ['--abc', 'a b'] | ||
``` | ||
### split.join(array, [quote=']) | ||
```js | ||
split.join(['--abc', 'a b']); // '--abc "a b"' | ||
``` | ||
## License | ||
MIT |
'use strict'; | ||
var expect = require('chai').expect; | ||
var argv_split = require('../'); | ||
var split = require('../'); | ||
// #4 | ||
var command = 'command -a "a \'b\' c" -b \'a "b" c\' -c' | ||
+ ' "a b" -d \'a b\' -e "a" -f \'a\' -g "a b c'; | ||
var args = [ | ||
'command', | ||
'-a', "a 'b' c", | ||
'-b', 'a "b" c', | ||
'-c', 'a b', | ||
'-d', "a b", | ||
'-e', 'a', | ||
'-f', "a", | ||
'-g', '"a', 'b', 'c' | ||
]; | ||
describe("split(string)", function(){ | ||
it("quoted", function(){ | ||
expect(split(command)).to.deep.equal(args); | ||
}); | ||
it("normal", function(){ | ||
var command = 'command -a b --abc false'; | ||
expect(split(command)).to.deep.equal([ | ||
'command', | ||
'-a', | ||
'b', | ||
'--abc', | ||
'false' | ||
]); | ||
}); | ||
}); | ||
describe("split.balance()", function(){ | ||
it("balance arguments", function(){ | ||
var splited = command.split(' '); | ||
expect(split.balance(splited)).to.deep.equal(args); | ||
}); | ||
}); | ||
describe("split.join()", function(){ | ||
it("join arguments", function(){ | ||
var args = [ | ||
'-a', | ||
'a b c' | ||
]; | ||
expect(split.join(args)).to.equal('-a \'a b c\''); | ||
}); | ||
it("specified quote", function(){ | ||
var args = [ | ||
'-a', | ||
'a b c' | ||
]; | ||
expect(split.join(args, '"')).to.equal('-a "a b c"'); | ||
}); | ||
}); |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
6754
133
53
0