[Pkg-javascript-commits] [node-shell-quote] 108/137: add escape option to .parse

Bastien Roucariès rouca at moszumanska.debian.org
Fri Aug 25 19:19:44 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-shell-quote.

commit 4d400e773be448c320b6dc9b2eb1323d7a3461ca
Author: Karissa McKelvey <krmckelv at gmail.com>
Date:   Wed Mar 16 10:48:27 2016 -0700

    add escape option to .parse
---
 index.js        | 15 ++++++++-------
 readme.markdown | 14 ++++++++++++++
 test/parse.js   |  3 ++-
 3 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/index.js b/index.js
index a917871..b78e692 100644
--- a/index.js
+++ b/index.js
@@ -33,8 +33,8 @@ for (var i = 0; i < 4; i++) {
     TOKEN += (Math.pow(16,8)*Math.random()).toString(16);
 }
 
-exports.parse = function (s, env) {
-    var mapped = parse(s, env);
+exports.parse = function (s, env, opts) {
+    var mapped = parse(s, env, opts);
     if (typeof env !== 'function') return mapped;
     return reduce(mapped, function (acc, s) {
         if (typeof s === 'object') return acc.concat(s);
@@ -49,15 +49,16 @@ exports.parse = function (s, env) {
     }, []);
 };
 
-function parse (s, env) {
+function parse (s, env, opts) {
     var chunker = new RegExp([
         '(' + CONTROL + ')', // control chars
         '(' + BAREWORD + '|' + SINGLE_QUOTE + '|' + DOUBLE_QUOTE + ')*'
     ].join('|'), 'g');
     var match = filter(s.match(chunker), Boolean);
-    
+
     if (!match) return [];
     if (!env) env = {};
+    if (!opts) opts = {};
     return map(match, function (s) {
         if (RegExp('^' + CONTROL + '$').test(s)) {
             return { op: s };
@@ -76,8 +77,8 @@ function parse (s, env) {
         //     "allonetoken")
         var SQ = "'";
         var DQ = '"';
-        var BS = '\\';
         var DS = '$';
+        var BS = opts.escape || '\\';
         var quote = false;
         var varname = false;
         var esc = false;
@@ -168,11 +169,11 @@ function parse (s, env) {
             return getVar(null, '', varname);
         }
     });
-    
+
     function getVar (_, pre, key) {
         var r = typeof env === 'function' ? env(key) : env[key];
         if (r === undefined) r = '';
-        
+
         if (typeof r === 'object') {
             return pre + TOKEN + json.stringify(r) + TOKEN;
         }
diff --git a/readme.markdown b/readme.markdown
index e1f95b9..6146e9b 100644
--- a/readme.markdown
+++ b/readme.markdown
@@ -50,6 +50,20 @@ output
 [ 'beep', '--boop=/home/robot' ]
 ```
 
+## parse with custom escape charcter
+
+``` js
+var parse = require('shell-quote').parse;
+var xs = parse('beep --boop="$PWD"', { PWD: '/home/robot' }, { escape: '^' });
+console.dir(xs);
+```
+
+output
+
+```
+[ 'beep', '--boop=/home/robot' ]
+```
+
 ## parsing shell operators
 
 ``` js
diff --git a/test/parse.js b/test/parse.js
index 5787897..2df4419 100644
--- a/test/parse.js
+++ b/test/parse.js
@@ -17,6 +17,7 @@ test('parse shell commands', function (t) {
     t.same(parse('a\\ b"c d"\\ e f'), [ 'a bc d e', 'f' ]);
     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.end();
 });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-shell-quote.git



More information about the Pkg-javascript-commits mailing list