Permalink
Browse files

commands work

  • Loading branch information...
1 parent c90dfcd commit d01df946a927c156a7644c9b0862d65beaa8bd43 @tricknik committed Dec 18, 2010
Showing with 73 additions and 37 deletions.
  1. +1 −2 example.js
  2. +13 −9 example_commands.js
  3. +21 −0 example_test.js
  4. +38 −26 operetta.js
View
3 example.js
@@ -1,5 +1,6 @@
require.paths.push('.');
+var sys = require('sys');
var Operetta = require('operetta').Operetta;
@@ -49,5 +50,3 @@ operetta.start(function(values) {
console.log(values);
});
-operetta.usage();
-
View
22 example_commands.js
@@ -2,21 +2,25 @@
require.paths.push('.');
var Operetta = require('operetta').Operetta;
+var sys = require('sys');
-var args = ['test', 'hello',
- '-t', '-tclosetest', '-xtcloser', '-xt', 'another', 'test', '--test', 'long', 'test',
- '-t', 'short', 'test', '--test=equals_test','--flag','/path/to/some/file'];
-
-console.log("[", args.join(" "), "]\n");
+var args = ['say', '-x', '--name', 'Gilbert', 'hello', 'there'];
var operetta = new Operetta(args);
-operetta.command('say', function(command) {
+operetta.command('say', 'say something', function(command) {
+ command.parameters(["-n","--name"], "Add Name ");
+ command.options("-x", "Add Exlamation Mark");
command.start(function(values) {
- console.log(values);
+ var saying = values.positional.join(" ");
+ if (values["-n"]) {
+ saying = saying + ", " + values["-n"][0];
+ };
+ if (values["-x"]) {
+ saying = saying + "!";
+ };
+ console.log(saying);
});
});
operetta.start();
-operetta.usage();
-
View
21 example_test.js
@@ -0,0 +1,21 @@
+
+require.paths.push('.');
+var Operetta = require('operetta').Operetta;
+
+var operetta = new Operetta();
+operetta.command("say", "Say Something", function(command) {
+ command.start(function(values) {
+ console.log(values.positional.join(" "));
+ console.log(values);
+ });
+});
+operetta.parameters(["-p","--param"], "A Paramater", function(value) {
+ console.log("Value:", value);
+});
+operetta.start(function(values) {
+ console.log(values);
+});
+
+operetta.start();
+
+
View
64 operetta.js
@@ -45,67 +45,79 @@ var events = require('events'),
sys = require('sys');
var Operetta = function(args) {
- this.args = args || process.argv;
+ if (args) this.args = args
+ else {
+ if (process.argv[0] == "node") this.args = process.argv.slice(2);
+ else this.args = process.argv.slice(1);
+ }
// options which are paramaters
this.params = {};
// options which are not paramaters
this.opts = {};
this.commands = {};
- this.values = [];
+ this.values = {}
+ this.values.positional = [];
this.banner = '';
- this.help = '';
+ this.help = 'Usage:';
this.parent = null;
// universal option detector
this.re = /^(-[^-])([A-Za-z0-9_\-]+)?$|^(--[A-Za-z0-9_\-]+)[=]?(.+)?$/;
this.parse = function(listener) {
var operetta = this, parameter;
var sing = function(argument, data) {
+ argument = argument || current;
operetta.values[argument] = operetta.values[argument] || [];
operetta.values[argument].push(data);
- operetta.emit(argument, data);
- if (operetta.parent) operetta.parent.emit(argument, data);
- parameter = undefined
+ if (operetta.listeners(argument).length > 0) operetta.emit(argument, data);
+ parameter = undefined;
+ current = undefined;
}
var process = function(option, data) {
parameter = operetta.params[option];
- if (data || !parameter) sing(parameter || operetta.opts[option], data || true);
+ if (data || !parameter) sing(parameter || operetta.opts[option] || option, data || true);
}
while (operetta.args.length > 0) {
- var arg = operetta.args.shift(),
- m = operetta.re.exec(arg);
+ var current = operetta.args.shift(),
+ m = operetta.re.exec(current);
if (m) {
if (parameter) sing(parameter, null);
if (m[2]) {
var options = m[1][1] + m[2];
for (i in options) {
- var option = operetta.opts["-" + options[i]];
- if (option) process(option);
- else {
- process("-" + options[i], options.slice(parseInt(i) + 1));
+ var a = operetta.params["-" + options[i]];
+ if (a) {
+ process(a, options.slice(parseInt(i) + 1));
break;
- }
+ } else process("-" + options[i]);
}
} else process(m[1] || m[3], m[4]);
- } else if (parameter) sing(parameter, arg);
- else sing("positional", arg);
+ } else if (parameter) sing(parameter, current);
+ else sing("positional", current);
}
if (listener) listener(operetta.values);
};
};
sys.inherits(Operetta, events.EventEmitter);
-Operetta.prototype.start = function(listener) {
- if (this.commands.length > 0) {
- var command = operetta.args[0],
- callback = this.commands(command);
- if (listener) {
- operetta.args.shift(),
- operetta = new Operetta(operetta.args);
- operetta.parent = this;
- callback(operetta);
+Operetta.prototype.start = function(callback) {
+ var operetta = this;
+ if (operetta.parent && operetta.args.length == 0) operetta.usage();
+ else {
+ if (!operetta.opts["-h"]) {
+ operetta.options(['-h','--help'], "Show Help", function() {
+ operetta.usage();
+ });
+ }
+ var arg = operetta.args[0],
+ command = operetta.commands[arg];
+ if (command) {
+ operetta.args.shift();
+ var child = new Operetta(operetta.args);
+ child.parent = operetta;
+ command(child);
}
+ operetta.parse(callback);
}
- this.parse(listener);
};
Operetta.prototype.bind = function(args, description, listener, takes_arguments) {

0 comments on commit d01df94

Please sign in to comment.