From 9cdd6657bfbfdd66bb02b9736f8a4af381122f4c Mon Sep 17 00:00:00 2001 From: "Vincent Peybernes [Techniv]" Date: Fri, 28 Jun 2013 15:21:26 +0200 Subject: [PATCH] Add the addCommands method. Recursive commands adding. --- .npmignore | 2 +- Gruntfile.js | 2 +- LICENSE | 2 +- README.md | 6 ++++- libs/commandio.js | 53 +++++++++++++++++++++++++++++++++++++----- package.json | 2 +- tests/global.js | 15 +++++++++++- tests/internal-test.js | 2 +- 8 files changed, 71 insertions(+), 13 deletions(-) diff --git a/.npmignore b/.npmignore index bea836a..23e2c04 100644 --- a/.npmignore +++ b/.npmignore @@ -1,2 +1,2 @@ tests -Gruntfile.js \ No newline at end of file +Gruntfile.js diff --git a/Gruntfile.js b/Gruntfile.js index 6af81c6..0d16cf6 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -10,4 +10,4 @@ module.exports = function(grunt){ grunt.registerTask('default', ['nodeunit']); grunt.loadNpmTasks('grunt-contrib-nodeunit'); -} \ No newline at end of file +} diff --git a/LICENSE b/LICENSE index 38d87a9..2283483 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) +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 diff --git a/README.md b/README.md index 30ca0a1..62e9eed 100644 --- a/README.md +++ b/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). diff --git a/libs/commandio.js b/libs/commandio.js index d3734b4..2c87c43 100644 --- a/libs/commandio.js +++ b/libs/commandio.js @@ -1,3 +1,16 @@ +/* + * 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. + * + */ + var EventEmitter = require('events').EventEmitter; var emitter = new EventEmitter(); @@ -47,7 +60,7 @@ function parseCommand(key){ var split = key.split(" "); return split; -}; +} function processCommand(params){ @@ -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 @@ -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){ @@ -105,11 +147,12 @@ function help(name){ } // Add blank line separation. - console.log('--'); + console.log('--\n'); } module.exports = { addCommand: addCommand, + addCommands: addCommands, beforeExit: beforeExit }; @@ -152,5 +195,3 @@ function formatHelpLine(command, description){ return currantRow; } } - - diff --git a/package.json b/package.json index 4b86aaf..e6d646c 100644 --- a/package.json +++ b/package.json @@ -37,4 +37,4 @@ "nodeunit": "*" } -} \ No newline at end of file +} diff --git a/tests/global.js b/tests/global.js index bdfc86c..5f7d213 100644 --- a/tests/global.js +++ b/tests/global.js @@ -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"); -}) \ No newline at end of file +}); + +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);} + } +]); diff --git a/tests/internal-test.js b/tests/internal-test.js index 635acbf..d73cfd7 100644 --- a/tests/internal-test.js +++ b/tests/internal-test.js @@ -15,4 +15,4 @@ module.exports = { test.equal(endLine.length,formatEndLine); test.done(); } -} \ No newline at end of file +}