Skip to content

Commit 7bcd90e

Browse files
committed
[Fix] properly support the escape option
Fixes #5
1 parent 8f0c5c3 commit 7bcd90e

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ output
5757

5858
``` js
5959
var parse = require('shell-quote/parse');
60-
var xs = parse('beep --boop="$PWD"', { PWD: '/home/robot' }, { escape: '^' });
60+
var xs = parse('beep ^--boop="$PWD"', { PWD: '/home/robot' }, { escape: '^' });
6161
console.dir(xs);
6262
```
6363

6464
output
6565

6666
```
67-
[ 'beep', '--boop=/home/robot' ]
67+
[ 'beep --boop=/home/robot' ]
6868
```
6969

7070
## parsing shell operators

parse.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var CONTROL = '(?:' + [
77
].join('|') + ')';
88
var controlRE = new RegExp('^' + CONTROL + '$');
99
var META = '|&;()<> \\t';
10-
var BAREWORD = '(\\\\[\'"' + META + ']|[^\\s\'"' + META + '])+';
1110
var SINGLE_QUOTE = '"((\\\\"|[^"])*?)"';
1211
var DOUBLE_QUOTE = '\'((\\\\\'|[^\'])*?)\'';
1312
var hash = /^#$/;
@@ -24,6 +23,12 @@ for (var i = 0; i < 4; i++) {
2423
var startsWithToken = new RegExp('^' + TOKEN);
2524

2625
function parseInternal(s, env, opts) {
26+
if (!opts) {
27+
opts = {};
28+
}
29+
var BS = opts.escape || '\\';
30+
var BAREWORD = '(\\' + BS + '[\'"' + META + ']|[^\\s\'"' + META + '])+';
31+
2732
var chunker = new RegExp([
2833
'(' + CONTROL + ')', // control chars
2934
'(' + BAREWORD + '|' + SINGLE_QUOTE + '|' + DOUBLE_QUOTE + ')*'
@@ -36,9 +41,6 @@ function parseInternal(s, env, opts) {
3641
if (!env) {
3742
env = {};
3843
}
39-
if (!opts) {
40-
opts = {};
41-
}
4244

4345
var commented = false;
4446

@@ -75,7 +77,6 @@ function parseInternal(s, env, opts) {
7577
// 4. quote context can switch mid-token if there is no whitespace
7678
// between the two quote contexts (e.g. all'one'"token" parses as
7779
// "allonetoken")
78-
var BS = opts.escape || '\\';
7980
var quote = false;
8081
var esc = false;
8182
var out = '';

test/parse.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ test('parse shell commands', function (t) {
2020
t.same(parse('a\\ b"c d"\\ e\'f g\' h'), ['a bc d ef g', 'h']);
2121
t.same(parse("x \"bl'a\"'h'"), ['x', "bl'ah"]);
2222
t.same(parse("x bl^'a^'h'", {}, { escape: '^' }), ['x', "bl'a'h"]);
23+
t.same(parse('abcH def', {}, { escape: 'H' }), ['abc def']);
2324

2425
t.end();
2526
});

0 commit comments

Comments
 (0)