cmdrouter
TypeScript icon, indicating that this package has built-in type declarations

0.3.1 • Public • Published

Intro

Simple command line router that maps command line arguments to javascript function name and params. Very simple mapping at this point (first param is the method name, subsequent ones are passed as param).

Note: From v0.2.x now node.js > 8 is required.

Note: From v0.3.x needs to import as const { router } = require("cmdrouter"); (rather than the 0.2.x const router = require("cmdrouter");)

usage

testcmd.js

const { router } = require("cmdrouter");
// or import { router } from 'cmdrouter'
 
// create a router instance with the name/function dictionary, 
// and call route() to route from the process.argv
router({_default, helloWorld, hello}).route();
 
function _default(){
    console.log("no command, _default function executed");
}
 
function helloWorld(){
    console.log("hello world");
}
 
function hello(name){
    console.log(`Hello ${name}`);
}
 
async function asyncWork(){
    var a = yield 12;
    var b = await getPromiseForB(); // returns a promise that will resolve with 8.
    console.log(`a + b = ${+ b}`);
}

From command line:

> node testcmd
no command, _default function executed
> node testcmd hellowWorld
hello world
> node testcmd hello John
Hello John
> node asyncWork
a + b = 20

Readme

Keywords

none

Package Sidebar

Install

npm i cmdrouter

Weekly Downloads

170

Version

0.3.1

License

MIT

Unpacked Size

3.29 kB

Total Files

5

Last publish

Collaborators

  • jeremychone