神刀安全网

Namespaced Terminal Client Commands

Namespaced Terminal Client Commands

Namespaced Terminal Client Commands Namespaced Terminal Client Commands Namespaced Terminal Client Commands Namespaced Terminal Client Commands Namespaced Terminal Client Commands Namespaced Terminal Client Commands Namespaced Terminal Client Commands Namespaced Terminal Client Commands Namespaced Terminal Client Commands

This module aims to provide an easy and structured way to create modular commands for Node commandline clients and load them only on demand.

It extends a client object with an execute function which can execute modules created with a declared namespaced convention. Each namespaced module must contain a method called invoke . This method runs always in context of the client object.

Usage

Create a command with arguments

"test-client-commands/ clientcommand. testcommand.open-with-arguments.js"

"use strict";  var Cmd = function() {     return this; };  Cmd.prototype.invoke = function(name1,name2,name3) {     // runs in context of client.     return "This testcommand is using open called from " + this.id + " (" + name1 + "," + name2 + "," + name3 + ")"; };  module.exports = Cmd;

Extend a terminal client

var NTCC = require("ntcc"); var ntcc = new NTCC();  var TestClient = function() {     this.id = "A Testing Client";     return this; }; var testClient = new TestClient();  ntcc.extend({     client: testClient,     dir: path.join(__dirname, "..", "test-client-commands"),     namespacePrefix: "clientcommand." });

console.log(testClient.__) yields:

  • execute() | Method to exec a command.
  • listCommands() | Usefull to provide a commandline help.
  • getCommandFilename() | returns a filename for a given command namespace.

Invoke the test command

it("should execute testcommand.open-with-arguments to pass custom arguments", function() {     var args = ["arg1", "arg2", "argN"];     var string = testClient.__.execute(["testcommand", "open-with-arguments"], args);     string.should.equal("This testcommand is using open called from " + testClient.id + " (" + args.join(",") + ")"); });

For more examples see the test-client-commands andthe tests.

A more realistic usecase

Human readable:

testClient.__.execute(["open",      "db", "connection"], ["connection-1"]); testClient.__.execute(["close",     "db", "connection"], ["connection-1"]); testClient.__.execute(["new",       "db", "connection"]); testClient.__.execute(["remove",    "db", "connection"], ["connection-1"]);

Or straight namespaced:

testClient.__.execute(["connection", "db", "open"], ["connection-1"]); testClient.__.execute(["connection", "db", "close"], ["connection-1"]); testClient.__.execute(["connection", "db", "new"]); testClient.__.execute(["connection", "db", "remove"], ["connection-1"]);

I prefer to use this module in association withminimist.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Namespaced Terminal Client Commands

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮