Skip to content

Commit

Permalink
add shell.exec support for auto-quoting arguments with spaces in them…
Browse files Browse the repository at this point in the history
… (when arguments are passed as an array)
  • Loading branch information
coreybutler committed Aug 5, 2021
1 parent 83aa3e6 commit 6b6eae3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
8 changes: 7 additions & 1 deletion src/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() })
Expand Down
47 changes: 47 additions & 0 deletions tests/100-regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit 6b6eae3

Please sign in to comment.