Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/s-a/ntcc
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/s-a/ntcc
- Owner: s-a
- License: other
- Created: 2016-04-14T00:16:29.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-04-20T07:44:37.000Z (almost 9 years ago)
- Last Synced: 2024-11-09T02:38:55.141Z (3 months ago)
- Language: JavaScript
- Size: 20.5 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Namespaced Terminal Client Commands
[](https://www.npmjs.org/package/ntcc)
[](https://travis-ci.org/s-a/ntcc)
[](https://coveralls.io/github/s-a/ntcc?branch=master)
[](https://www.codacy.com/app/stephanahlf/ntcc)
[](https://david-dm.org/s-a/ntcc)
[](https://david-dm.org/s-a/ntcc#info=devDependencies)
[](https://www.npmjs.org/package/ntcc)
[](/LICENSE.md#mit)
[](http://s-a.github.io/donate/)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"
```javascript
"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
```javascript
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
```javascript
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](/test-client-commands) and [the tests](test).
## A more realistic usecase
Human readable:
```javascript
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:
```javascript
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 with [minimist](https://github.com/substack/minimist).