mini-commander

a single-file helper for making simple cli tools in nodejs
git clone https://github.com/TanguyAndreani/mini-commander
Log | Files | Refs | README

commit 6d1c055f3c36e0e85da74940065a98586bba71b4
Author: Tanguy Andreani <tanguy.andreani@tuta.io>
Date:   Sat,  8 Feb 2020 21:55:02 +0100

first commit

Diffstat:
AREADME.md | 25+++++++++++++++++++++++++
Aexample.js | 33+++++++++++++++++++++++++++++++++
Amini-commander.js | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 131 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md @@ -0,0 +1,24 @@ +# mini-commander + +My first nodejs project. I simply tried to make something that looks +like the interface to Commander. See `example.js` to see how to use it. + +```nodejs +const Commander = require('./mini-commander') +``` + +## See it in action + +``` +$ node main.js help +NAME + mini-commander's example + +DESCRIPTION + a do-nothing tool + +COMMANDS + help (h) display this help message + install (i) install things + uninstall (u) uninstall things +```+ \ No newline at end of file diff --git a/example.js b/example.js @@ -0,0 +1,32 @@ +const Commander = require('./mini-commander') + +program = Object.create(Commander) +program.name = 'mini-commander\'s example' +program.description = 'a do-nothing tool' + +program + .command('help') + .alias('h') + .description('display this help message') + .action(() => { + program.usage() + process.exit(0) + }) + +program + .command('install') + .alias('i') + .description('install things') + .action(() => { + console.log("installing things!") + }) + +program + .command('uninstall') + .alias('u') + .description('uninstall things') + .action(() => { + console.log("uninstalling things!") + }) + +program.parse(process.argv)+ \ No newline at end of file diff --git a/mini-commander.js b/mini-commander.js @@ -0,0 +1,72 @@ +const util = require('util'); + +const Commander = { + + + /* PRIVATE ATTRIBUTES */ + + command_object: { + alias(s) { + this.alias = s + return (this) + }, + description(s) { + this.description = s + return (this) + }, + action(fn) { + this.action = fn + return (this) + } + }, + + /* list of command_objects */ + commands: [], + + + /* PUBLIC INTERFACE */ + + command(name) { + var c = Object.create(this.command_object) + c.name = name + this.commands.push(c) + return (c) + }, + + usage() { + if (this.name) { + console.log("NAME") + console.log("\t" + this.name + '\n') + } + + if (this.description) { + console.log("DESCRIPTION") + console.log("\t" + this.description + '\n') + } + + if (this.command) { + console.log("COMMANDS") + for (let command of this.commands) { + console.log(util.format('\t%s (%s)\t%s', + command.name, + command.alias, + command.description)) + } + } else { + console.log("Add some commands!") + } + }, + + parse(av) { + for (let command of this.commands) { + if (command.name == av[2] || command.alias == av[2]) { + command.action(av) + return; + } + } + this.usage() + process.exit(1) + } +} + +module.exports = Commander+ \ No newline at end of file