Skip to content

Commit

Permalink
[Fix] properly support the escape option
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
ljharb committed Apr 6, 2023
1 parent 8f0c5c3 commit 7bcd90e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -57,14 +57,14 @@ output

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

output

```
[ 'beep', '--boop=/home/robot' ]
[ 'beep --boop=/home/robot' ]
```

## parsing shell operators
Expand Down
11 changes: 6 additions & 5 deletions parse.js
Expand Up @@ -7,7 +7,6 @@ var CONTROL = '(?:' + [
].join('|') + ')';
var controlRE = new RegExp('^' + CONTROL + '$');
var META = '|&;()<> \\t';
var BAREWORD = '(\\\\[\'"' + META + ']|[^\\s\'"' + META + '])+';
var SINGLE_QUOTE = '"((\\\\"|[^"])*?)"';
var DOUBLE_QUOTE = '\'((\\\\\'|[^\'])*?)\'';
var hash = /^#$/;
Expand All @@ -24,6 +23,12 @@ for (var i = 0; i < 4; i++) {
var startsWithToken = new RegExp('^' + TOKEN);

function parseInternal(s, env, opts) {
if (!opts) {
opts = {};
}
var BS = opts.escape || '\\';
var BAREWORD = '(\\' + BS + '[\'"' + META + ']|[^\\s\'"' + META + '])+';

var chunker = new RegExp([
'(' + CONTROL + ')', // control chars
'(' + BAREWORD + '|' + SINGLE_QUOTE + '|' + DOUBLE_QUOTE + ')*'
Expand All @@ -36,9 +41,6 @@ function parseInternal(s, env, opts) {
if (!env) {
env = {};
}
if (!opts) {
opts = {};
}

var commented = false;

Expand Down Expand Up @@ -75,7 +77,6 @@ function parseInternal(s, env, opts) {
// 4. quote context can switch mid-token if there is no whitespace
// between the two quote contexts (e.g. all'one'"token" parses as
// "allonetoken")
var BS = opts.escape || '\\';
var quote = false;
var esc = false;
var out = '';
Expand Down
1 change: 1 addition & 0 deletions test/parse.js
Expand Up @@ -20,6 +20,7 @@ test('parse shell commands', function (t) {
t.same(parse('a\\ b"c d"\\ e\'f g\' h'), ['a bc d ef g', 'h']);
t.same(parse("x \"bl'a\"'h'"), ['x', "bl'ah"]);
t.same(parse("x bl^'a^'h'", {}, { escape: '^' }), ['x', "bl'a'h"]);
t.same(parse('abcH def', {}, { escape: 'H' }), ['abc def']);

t.end();
});

0 comments on commit 7bcd90e

Please sign in to comment.