Skip to content

Commit

Permalink
Modified the command method. Allowing args to be multi-word strings e…
Browse files Browse the repository at this point in the history
…nclosed in single/double quotes
  • Loading branch information
sonyseng committed Apr 19, 2016
1 parent 9434d27 commit e2f10d9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ node_modules

# Optional REPL history
.node_repl_history

# Ignore IDE files
.idea
4 changes: 3 additions & 1 deletion src/command/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var tokenize = require('./tokenize');

/** @public module */
module.exports = function( b9 ){

Expand Down Expand Up @@ -38,7 +40,7 @@ module.exports = function( b9 ){
return;
}
// {array} argument vector
var argv = msg.text.split(/ +/), cmd, i = 1, direct;
var argv = tokenize(msg.text), cmd, i = 1, direct;
// indicates a specific [start of message] mention
if ( msg.text.indexOf('<@'+ b9.self.id +'>') === 0 ){
argv = argv.slice(1); // shift arguments
Expand Down
24 changes: 24 additions & 0 deletions src/command/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ describe('src/command',function(){
assert.equal( arg, 'xyz' );
});

it('registers a command and tokenize multi-word string arguments',function(){
var argv;

// register another command
bot.command('foo [arg1] [arg2] [arg3] [arg4] [arg5] [arg6] [arg7]',
'the foo command',
function( msg ){
argv = msg.argv;
}
);

// simulate command
simulate('foo \"hello world\" arg1 arg2 \'multi arg that has nested strings \"haha hehe\"\' arg3 anotherArg _arg4_');

// optional arg is defined
assert.equal(argv.arg1, 'hello world' );
assert.equal(argv.arg2, 'arg1' );
assert.equal(argv.arg3, 'arg2' );
assert.equal(argv.arg4, 'multi arg that has nested strings "haha hehe"' );
assert.equal(argv.arg5, 'arg3' );
assert.equal(argv.arg6, 'anotherArg' );
assert.equal(argv.arg7, '_arg4_' );
});

it('has an informative `help` command',function(){
// local ref
var lines = [];
Expand Down
38 changes: 38 additions & 0 deletions src/command/tokenize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function tokenize (str) {
var tokens = [];
var k, i, temp;
var strLen = str.length;

for (i = 0; i < strLen; i++) {
if (str[i] === '\"') {
// Keep reading chars until
for (temp = [], k = i+1; k < strLen && str[k] !== '\"'; k++) {
temp.push(str[k]);
}
tokens.push(temp.join(''));
i = k+1;
}

if (str[i] === '\'') {
// Keep reading chars until
for (temp = [], k = i+1; k < strLen && str[k] !== '\''; k++) {
temp.push(str[k]);
}
tokens.push(temp.join(''));
i = k+1;
}

if (str[i] && str[i] !== ' ') {
// Keep reading chars until
for (temp = [], k = i; k < strLen && str[k] !== ' '; k++) {
temp.push(str[k]);
}
tokens.push(temp.join(''));
i = k;
}
}

return tokens;
}

module.exports = tokenize;

0 comments on commit e2f10d9

Please sign in to comment.