Skip to content

Commit

Permalink
Add the addCommands method.
Browse files Browse the repository at this point in the history
Recursive commands adding.
  • Loading branch information
Techniv committed Jun 28, 2013
1 parent b5fc10e commit 9cdd665
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .npmignore
@@ -1,2 +1,2 @@
tests
Gruntfile.js
Gruntfile.js
2 changes: 1 addition & 1 deletion Gruntfile.js
Expand Up @@ -10,4 +10,4 @@ module.exports = function(grunt){
grunt.registerTask('default', ['nodeunit']);

grunt.loadNpmTasks('grunt-contrib-nodeunit');
}
}
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright (c) <year> <copyright holders>
Copyright (c) 2013 Vincent Peybernes, Thomas Pons.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
Expand Down
6 changes: 5 additions & 1 deletion README.md
@@ -1,4 +1,8 @@
node-command-io ![build-status](http://status.ci.techniv.fr/Command.IO)
===============

Console io provider for running Node.JS applications.
This program is a library that provides management of the console user input during the execution of the application.
With Command.IO, you can simply intercept the console input commands and bind action on this. For each command you
can associate description that is used by Command.IO to generate an inline help.

Command.IO is publish under [MIT license](https://raw.github.com/Techniv/node-command-io/master/LICENSE).
53 changes: 47 additions & 6 deletions libs/commandio.js
@@ -1,3 +1,16 @@
/*
* Command.IO
* <https://github.com/Techniv/node-command-io>
*
* This program is a library that provides management of the console user input during the execution of the application.
* With Command.IO, you can simply intercept the console input commands and bind action on this. For each command you
* can associate description that is used by Command.IO to generate an inline help.
*
* Command.IO is publish under MIT license.
* Copyright (c) 2013 Vincent Peybernes, Thomas Pons.
* <https://raw.github.com/Techniv/node-command-io/master/LICENSE>
*/


var EventEmitter = require('events').EventEmitter;
var emitter = new EventEmitter();
Expand Down Expand Up @@ -47,7 +60,7 @@ function parseCommand(key){
var split = key.split(" ");

return split;
};
}

function processCommand(params){

Expand All @@ -56,8 +69,15 @@ function processCommand(params){

// Create an emitter and broadcast the command
emitter.emit(command, params);
};
}

/**
* Add a command
* @param name string
* @param description string
* @param action Function
* @returns Object return Command.IO API.
*/
function addCommand(name, description, action){

// Associate the command name with his description
Expand All @@ -72,12 +92,34 @@ function addCommand(name, description, action){

// Chain addCommand
return module.exports;
};
}

/**
* Add commands recursively.
* @param commands Object[] An array of command descriptor {name: string, description: string, :action: function}.
* @return Object Return Command.IO API.
*/
function addCommands(commands){
for(var i in commands){
var commandObj = commands[i];
addCommand(commandObj.name, commandObj.description, commandObj.action);
}

return module.exports;
}

/**
* Add an action to be executed just before the end of application run, when the user call the exit command
* (auto generated by Command.IO)
* @param action Function
* @returns Object Return Command.IO API.
*/
function beforeExit(action){
if( typeof action != 'function') throw new Error('[command.io] The action must be a function.');

exitActions.push(action);

return module.exports;
}

function help(name){
Expand Down Expand Up @@ -105,11 +147,12 @@ function help(name){
}

// Add blank line separation.
console.log('--');
console.log('--\n');
}

module.exports = {
addCommand: addCommand,
addCommands: addCommands,
beforeExit: beforeExit
};

Expand Down Expand Up @@ -152,5 +195,3 @@ function formatHelpLine(command, description){
return currantRow;
}
}


2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -37,4 +37,4 @@
"nodeunit": "*"
}

}
}
15 changes: 14 additions & 1 deletion tests/global.js
Expand Up @@ -13,4 +13,17 @@ commandio.addCommand('test', "The test function", function(){

commandio.beforeExit(function(){
console.log("I've just the time to say you good bye");
})
});

commandio.addCommands([
{
name: 'cmd1',
description: 'Command 1',
action: function(arg){console.log(arg);}
},
{
name: 'cmd2',
description: 'Command 2',
action: function(arg){console.log(arg*2);}
}
]);
2 changes: 1 addition & 1 deletion tests/internal-test.js
Expand Up @@ -15,4 +15,4 @@ module.exports = {
test.equal(endLine.length,formatEndLine);
test.done();
}
}
}

0 comments on commit 9cdd665

Please sign in to comment.