Skip to content

Commit

Permalink
Add support for newlines in -a="" arguments
Browse files Browse the repository at this point in the history
Previously, you couldn't do this:

$ node program.js --foo="hey
  bar"

You'd end up with args.foo === 'hey'
Now you get the correct args.foo === "hey\nbar"

Note: the below WAS workign and continues to work (no = )

$ node program.js --foo "hey
  bar"
  • Loading branch information
danielbeardsley committed Apr 1, 2013
1 parent 71e1fb5 commit e72346a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,10 @@ function Argv (args, cwd) {
break;
}
else if (arg.match(/^--.+=/)) {
var m = arg.match(/^--([^=]+)=(.*)/);
// Using [\s\S] instead of . because js doesn't support the
// 'dotall' regex modifier. See:
// http://stackoverflow.com/a/1068308/13216
var m = arg.match(/^--([^=]+)=([\s\S]*)$/);
setArg(m[1], m[2]);
}
else if (arg.match(/^--no-.+/)) {
Expand Down
13 changes: 13 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,19 @@ test('boolean groups', function (t) {
t.end();
});

test('newlines in params' , function (t) {
var args = optimist.parse([ '-s', "X\nX" ])
t.same(args, { _ : [], s : "X\nX", $0 : $0 });

// reproduce in bash:
// VALUE="new
// line"
// node program.js --s="$VALUE"
args = optimist.parse([ "--s=X\nX" ])
t.same(args, { _ : [], s : "X\nX", $0 : $0 });
t.end();
});

test('strings' , function (t) {
var s = optimist([ '-s', '0001234' ]).string('s').argv.s;
t.same(s, '0001234');
Expand Down

0 comments on commit e72346a

Please sign in to comment.