From 6b6eae3231b6b7f4912d5f34977fab988e158779 Mon Sep 17 00:00:00 2001 From: Corey Butler Date: Thu, 5 Aug 2021 11:47:33 -0500 Subject: [PATCH] add shell.exec support for auto-quoting arguments with spaces in them (when arguments are passed as an array) --- package.json | 2 +- src/shell.js | 8 ++++++- tests/100-regression.js | 47 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 8c2e78e..efa45f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@author.io/shell", - "version": "1.8.9", + "version": "1.8.10", "description": "A micro-framework for creating CLI-like experiences. This supports Node.js and browsers.", "main": "src/index.js", "scripts": { diff --git a/src/shell.js b/src/shell.js index edca10a..c5bad75 100644 --- a/src/shell.js +++ b/src/shell.js @@ -167,7 +167,13 @@ export default class Shell extends Base { // The array check exists because people are passing process.argv.slice(2) into this // method, often forgetting to join the values into a string. if (Array.isArray(input)) { - input = input.join(' ') + input = input.map(i => { + if (i.indexOf(' ') >= 0 && !/^[\"\'].+ [\"\']$/.test(i)) { + return `"${i}"` + } else { + return i + } + }).join(' ') } this.#history.unshift({ input, time: new Date().toLocaleString() }) diff --git a/tests/100-regression.js b/tests/100-regression.js index 5ebceda..762f27c 100644 --- a/tests/100-regression.js +++ b/tests/100-regression.js @@ -72,3 +72,50 @@ test('Properly parsing input values with spaces', t => { sh.exec('run -c "a connection"').catch(t.fail) }) + +test('Recognize flags with quotes', t => { + const input = 'run --connection "a connection" --save' + const sh = new Shell({ + name: 'test', + commands: [{ + name: 'run', + flags: { + connection: { + alias: 'c', + description: 'connection string', + type: 'string' + } + }, + handler(meta) { + t.expect('a connection', meta.data.connection, 'Support flag values with spaces') + t.end() + } + }] + }) + + sh.exec('run -c "a connection"').catch(t.fail) +}) + +test('Accept arrays with values containing spaces', t => { + const input = 'run --connection "a connection" --save' + const sh = new Shell({ + name: 'test', + commands: [{ + name: 'run', + flags: { + connection: { + alias: 'c', + description: 'connection string', + type: 'string' + } + }, + handler(meta) { + t.expect('a connection', meta.data.connection, 'Support flag values with spaces') + t.end() + } + }] + }) + + const argv = ["run", "-c", "a connection", "--save"] + sh.exec(argv).catch(t.fail) +})