• Stars
    star
    165
  • Rank 228,170 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Command Line Interface Utility for Node.js using ES6 idioms

CMND

Command Line Interface Utility for Node.js

CMND is a package that lets you easily create CLI tools in Node.js. It's also simple to create associated manual (help) pages for each command.

Usage

To use CMND, first install it in your Node project with npm install cmnd --save.

Next, modify your project's package.json to include:

"bin": {
  "mycli": "./cli.js"
}

Where mycli is the intended name of your command in the CLI.

Now create a file: ./cli.js and folder ./commands:

#!/usr/bin/env node

'use strict';

const { CommandLineInterface } = require('cmnd');
const CLI = new CommandLineInterface();

CLI.load(__dirname, './commands');
CLI.run(process.argv.slice(2));

Finally, populate your commands directory with your commands, here's an example file: ./commands/example.js

const { Command } = require('cmnd');

class ExampleCommand extends Command {

  constructor() {
    // Name of the command
    super('example');
  }

  help () {
    return {
      description: 'An example command',
      args: ['example_arg1', 'example_arg2'],
      flags: {flag: 'An example flag'},
      vflags: {vflag: 'An example verbose flag'}
    };
  }

  async run (params) {
    // Run code here.
    // To throw an error, throw new Error('an error')
    // To return a result that gets printed to console, return notUndefinedVar;
  }

}

module.exports = ExampleCommand;

Creating manual pages (help)

View all the commands available to your CLI with mycli help where mycli is the intended name of your command in the CLI. To modify help information, change the return value of the help() method for each command.

Creating Subcommands

To subclass a command (i.e. mycli command_name:sub_name) simply change the contructor() method in your command to the following:

constructor () {

  super('command_name', 'sub_name');

}

Running your commands

Each command has a run() method which takes three arguments: args, flags, and vflags.

args

args is the array of arguments, passed before any flags.

i.e. mycli command alpha beta would populate args with ['alpha', 'beta']

flags

flags is an object containing any flags (prefixed with -), where each entry is an array of values passed after the flag

i.e. mycli command -f my flag would populate vflags with {f: ['my', 'flag']}

vflags

vflags works identically to flags, but with "verbose flags" (prefixed with --).

Additional notes

All argument arrays passed to args or any flags or vflags options will be separated by spaces, except in the case of quotation marks. Use quotation marks to specify an argument with spaces in it.

i.e. mycli command -f "argument one" argument_two

Acknowledgements

Thanks for checking out the library! Feel free to submit issues or PRs if you'd like to see more features.

Follow me on Twitter, @keithwhor.

Feel free to check out more of my GitHub projects.

More Repositories

1

nodal

API Services Made Easy With Node.js
JavaScript
4,515
star
2

multithread.js

In-browser multithreading made easy
JavaScript
686
star
3

audiosynth

JS Dynamic Audio Synth
JavaScript
599
star
4

UnitGraph

Lightweight Graph Library for Node 4.x
JavaScript
261
star
5

NtSeq

JavaScript (node + browser) bioinformatics library for nucleotide sequence manipulation and analysis.
JavaScript
208
star
6

FSO.js

(Deprecated!) JavaScript FileSystemObject library for temporary and permanent client-side file storage
JavaScript
130
star
7

canvasBlurRect

Real-time (30+ FPS) iOS-style box blur + saturation with HTML5 Canvas
JavaScript
87
star
8

nodal-graphql

GraphQL Example Nodal Application
JavaScript
55
star
9

NCBIConnect

JavaScript library for interfacing with National Center for Biotechnology Information's databases
JavaScript
16
star
10

inherit.js

A more elegant way to prototype
JavaScript
16
star
11

nodal-angular

Nodal Angular SPA Initializer & Helpers
JavaScript
10
star
12

instatweet-api

Nodal Example (0.7.x), Instatweet API server
JavaScript
10
star
13

halo-stat-notifier

Halo: Master Chief Collection Stat Notifier, send an SMS to tell your friends you're online
JavaScript
9
star
14

stdlib-sequence

StdLib DNA Sequence Alignment Service
JavaScript
6
star
15

APIConnect

Quick and Easy RESTful API XHR Library
JavaScript
6
star
16

api-res

Node.js HTTP(S) Request Library intended for Nodal API Services
JavaScript
4
star
17

casino

A module for simulating and playing Blackjack games
JavaScript
4
star
18

cubicBezier.js

Simple cubic bezier interpolator (find Y given X) for mimicking CSS transitions
JavaScript
2
star
19

tobi-said-autocode

Replies with a YouTube video when a Slack message starts with "Tobi said"
JavaScript
1
star