Load an arguable module and invoke it using this Program
as the parent. Not
at all certain that I want to have all this formatting nonsense. Can’t the
parent simply invoke it with a string?
We’re only going to support accepting a package name, but leave the
formatting logic in for now to see where it’s being used.
Program.prototype.delegate = cadence(function (async, format, argv) {
if (argv == null) {
if (this.argv.length == 0) {
this.abend('sub command missing')
}
var argv = this.argv.slice()
var command = argv.shift()
var pkg
if (typeof format == 'function') {
pkg = format(command, this)
} else {
pkg = util.format(format, command)
}
} else {
pkg = format
}
var arguable
try {
arguable = this._module.require(pkg)
} catch (error) {
if (error.code == 'MODULE_NOT_FOUND') {
this.abend('sub command module not found', command, pkg)
} else {
throw error
}
}
arguable(argv, {
stdout: this.stdout,
env: this.env,
stdin: this.stdin,
stderr: this.stderr,
events: this,
send: this.send
}, async())
})
module.exports = Program