Skip to content

Commit 73637a6

Browse files
rgroothuijsenSBoudrias
authored andcommittedSep 2, 2017
Support for multi-word command line arguments (#88)
1 parent 3922881 commit 73637a6

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed
 

‎lib/environment.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ const Store = require('./store');
1414
const resolver = require('./resolver');
1515
const TerminalAdapter = require('./adapter');
1616

17+
/**
18+
* Two-step argument splitting function that first splits arguments in quotes,
19+
* and then splits up the remaining arguments if they are not part of a quote.
20+
*/
21+
function splitArgsFromString(argsString) {
22+
let result = [];
23+
const quoteSeparatedArgs = argsString.split(/(\x22[^\x22]*\x22)/).filter(x => x);
24+
quoteSeparatedArgs.forEach(arg => {
25+
if (arg.match('\x22')) {
26+
result.push(arg.replace(/\x22/g, ''));
27+
} else {
28+
result = result.concat(arg.trim().split(' '));
29+
}
30+
});
31+
return result;
32+
}
33+
1734
/**
1835
* `Environment` object is responsible of handling the lifecyle and bootstrap
1936
* of generators in a specific environment (your app).
@@ -107,7 +124,7 @@ class Environment extends EventEmitter {
107124
super();
108125

109126
args = args || [];
110-
this.arguments = Array.isArray(args) ? args : args.split(' ');
127+
this.arguments = Array.isArray(args) ? args : splitArgsFromString(args);
111128
this.options = opts || {};
112129
this.adapter = adapter || new TerminalAdapter();
113130
this.cwd = this.options.cwd || process.cwd();
@@ -376,7 +393,7 @@ class Environment extends EventEmitter {
376393
options = options || {};
377394

378395
let args = options.arguments || options.args || _.clone(this.arguments);
379-
args = Array.isArray(args) ? args : args.split(' ');
396+
args = Array.isArray(args) ? args : splitArgsFromString(args);
380397

381398
const opts = options.options || _.clone(this.options);
382399

@@ -410,7 +427,7 @@ class Environment extends EventEmitter {
410427
args = this.arguments;
411428
}
412429

413-
args = Array.isArray(args) ? args : args.split(' ');
430+
args = Array.isArray(args) ? args : splitArgsFromString(args);
414431
options = options || this.options;
415432

416433
const name = args.shift();

‎test/environment.js

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ describe('Environment', () => {
5555
it('instantiates a mem-fs instance', function () {
5656
assert.ok(this.env.sharedFs);
5757
});
58+
59+
it('takes multi-word arguments', () => {
60+
const args = 'foo bar "foo bar" baz "bar foo"';
61+
assert.deepEqual(new Environment(args).arguments, ['foo', 'bar', 'foo bar', 'baz', 'bar foo']);
62+
});
5863
});
5964

6065
describe('#help()', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.