Permalink
Browse files

initial release

  • Loading branch information...
0 parents commit 42e6808f8269b0918022b83950f3905dc757e153 Fabricio Zuardi committed Aug 20, 2010
Showing with 181 additions and 0 deletions.
  1. +21 −0 LICENSE
  2. +52 −0 README.creole
  3. +39 −0 examples/example1.js
  4. +69 −0 index.js
21 LICENSE
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2010 Fabricio Campos Zuardi
+
+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 the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,52 @@
+= Node Arguments
+A simple command line arguments parser for node.js... Yes, another! :)
+
+== Usage
+{{{
+var arguments = require('./path/to/node-arguments');
+
+arguments.parse([
+ {'name': /^(-h|--help)$/, 'expected': null, 'callback': printHelp}
+ ,{'name': /^(-b|--beatle)$/, 'expected': /^(george|paul|ringo|john)$/i, 'callback': setupBeatle}
+ ,{'name': /^(-v|--volume)$/, 'expected': /^([0-9]|1[0-1])$/i, 'callback': setupVolume}
+ ], main, invalidArgument);
+
+function printHelp(end){
+ console.log('----------\nA pretty help message.');
+ end();
+}
+
+function setupBeatle(end, beatle){
+ console.log('----------\n%s rocks!', beatle);
+ end();
+}
+
+function setupVolume(end, volume){
+ console.log('----------\n%s is a number from 0 to 11', volume);
+ end();
+}
+
+function main(){
+ console.log('----------\nEverything is set.\n\nHello World!\n\n')
+}
+
+function invalidArgument(arg, value_missing){
+ console.log('----------\nError: the argument %s %s', arg, (value_missing?'expects a value':'is not valid'))
+}
+
+/*
+Examples:
+node examples/example1.js
+node examples/example1.js --help
+node examples/example1.js --beatle
+node examples/example1.js --beatle bieber
+node examples/example1.js --beatle george -v 11
+*/
+
+}}}
+
+== Credits
+
+Copyright (c) 2010 Fabricio Campos Zuardi
+
+Released under the MIT License.
@@ -0,0 +1,39 @@
+var arguments = require('../index');
+
+arguments.parse([
+ {'name': /^(-h|--help)$/, 'expected': null, 'callback': printHelp}
+ ,{'name': /^(-b|--beatle)$/, 'expected': /^(george|paul|ringo|john)$/i, 'callback': setupBeatle}
+ ,{'name': /^(-v|--volume)$/, 'expected': /^([0-9]|1[0-1])$/i, 'callback': setupVolume}
+ ], main, invalidArgument);
+
+function printHelp(end){
+ console.log('----------\nA pretty help message.');
+ end();
+}
+
+function setupBeatle(end, beatle){
+ console.log('----------\n%s rocks!', beatle);
+ end();
+}
+
+function setupVolume(end, volume){
+ console.log('----------\n%s is a number from 0 to 11', volume);
+ end();
+}
+
+function main(){
+ console.log('----------\nEverything is set.\n\nHello World!\n\n')
+}
+
+function invalidArgument(arg, value_missing){
+ console.log('----------\nError: the argument %s %s', arg, (value_missing?'expects a value':'is not valid'))
+}
+
+/*
+Examples:
+node examples/example1.js
+node examples/example1.js --help
+node examples/example1.js --beatle
+node examples/example1.js --beatle bieber
+node examples/example1.js --beatle george -v 11
+*/
@@ -0,0 +1,69 @@
+/*
+Node Arguments - Node.js command line arguments made simple.
+
+Copyright (c) 2010 Fabricio Campos Zuardi
+
+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 the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+this.parse = function(valid_arguments, afterParseCallback, invalidArgumentCallback){
+ var error, argument, valid_argument
+ ,invalid_syntax = true
+ ,options_callbacks = []
+ ,callbacks_finished_count = 0
+ ,args = process.argv.slice(2);
+
+ invalidArgumentCallback ? invalidArgumentCallback : invalidArgument;
+ if (args.length == 0) { afterParseCallback(); return true; }
+ for (i=0; i<args.length; i++){
+ invalid_syntax = true;
+ argument = args[i];
+ next_argument = args[i+1];
+ for (j=0;j<valid_arguments.length;j++){
+ valid_argument = valid_arguments[j];
+ if (valid_argument['name'].test(argument)){
+ invalid_syntax = false;
+ if (valid_argument['expected']){
+ i++;
+ if (!valid_argument['expected'].test(next_argument)){
+ invalidArgumentCallback(next_argument ? next_argument : argument, (next_argument==undefined));
+ break;
+ }
+ }
+ if (valid_argument['callback']) {
+ options_callbacks.push({
+ 'callback': valid_argument['callback']
+ ,'argument': valid_argument['expected'] ? next_argument : null
+ });
+ }
+ continue;
+ }
+ }
+ if (invalid_syntax) { invalidArgumentCallback(argument); break; }
+ }
+ callbacks_finished_count = options_callbacks.length
+ options_callbacks.forEach(function(item){
+ item['callback'](function(){
+ callbacks_finished_count --;
+ if (callbacks_finished_count == 0){
+ afterParseCallback();
+ }
+ }, item['argument']);
+ });
+}

0 comments on commit 42e6808

Please sign in to comment.