Sin*_*ico 8 javascript node.js vorpal.js
我正在开发基于Vorpal(http://vorpal.js.org/)和NodeJs 的命令行应用程序.
我想知道是否有一种方法允许(并在帮助中列出)命令取决于上下文.
例如,我可能希望有可能在上下文1上执行命令A和B,在上下文2上执行命令C和D.然后我将有一个命令来切换应始终有效的上下文.
您需要组合函数show并exit针对上下文重新定义函数。简化的实现示例:
var Vorpal = require('vorpal')
var mainDelimiter = 'main'
var main = new Vorpal().delimiter(mainDelimiter + '>')
var contexts = [
{
name: 'context1', help: 'context1 help',
init: function (instance) {
instance
.command('A', 'A help')
.action(function (args, cb) {
this.log('A...')
cb()
})
instance
.command('B', 'B help')
.action(function (args, cb) {
this.log('B...')
cb()
})
}
},
{
name: 'context2', help: 'context2 help',
init: function (instance) {
instance
.command('C', 'C help')
.action(function (args, cb) {
this.log('C...')
cb()
})
instance
.command('D', 'D help')
.action(function (args, cb) {
this.log('D...')
cb()
})
}
}
]
contexts.forEach(function (ctx, i) {
var instance = new Vorpal().delimiter(mainDelimiter + '/' + ctx.name + '>')
ctx.init(instance)
// Override the function "exit" for the context
instance.find('exit').remove()
instance
.command('exit', 'Exit context')
.action(function (args, cb) {
// Switch to the main context
main.show()
cb()
})
main
.command(ctx.name, ctx.help)
.action(function (args, cb) {
// Switch to the selected context
instance.show()
cb()
})
})
main.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
222 次 |
| 最近记录: |