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

0.0.6 • Public • Published

Bashr

Create stellar CLI applications

NPM

About

Bashr is a library for building NodeJS based command line interfaces.

  • Route based design
  • Command parameters and options/flags
  • Middleware architecture
  • Lazy-load route modules for fast response time

Example

import * as bashr from 'bashr';
 
const food = new bashr.CLI('food');
 
food.command('hello', () => {
    console.log('world');
});
 
food.run(process.argv);
$ food hello
world

Overview

Commands

import * as bashr from 'bashr';
 
const food = new bashr.CLI('food');
 
food.command('hello', () => {
    console.log('world');
});
 
food.run(process.argv);
$ food hello
world

Multiple handlers

import * as bashr from 'bashr';
 
const cli = new bashr.CLI('cli');
 
let count = 0;
 
const handler = () => {
    console.log(count);
    count++;
};
 
cli.command('hello', handler, handler, handler);
 
cli.run(process.argv);
$ cli hello
0
1
2

Parameters

Parameters are described with a : prefix. You can extract input parameters in command handlers.

Example:

import * as bashr from 'bashr';
 
const cli = new bashr.CLI('cli');
 
const handler = (input, output) => {
    console.log(input.params['param']);
};
 
cli.command('hello :param', handler);
 
cli.run(process.argv);
$ cli hello world
world

Optional Parameters

import * as bashr from 'bashr';
 
const cli = new bashr.CLI('cli');
 
const handler = (input, output) => {
    console.log(input.params['param']);
};
 
cli.command('hello :[param]', handler);
 
cli.run(process.argv);
$ cli hello world
world

Options/Flags

Options and Flags are parsed and configured with yargs-parser.

import * as bashr from 'bashr';
 
const cli = new bashr.CLI('cli');
 
const command = cli.command('hello', ()=> {
    console.log(input.options['world']);
});
 
command.option('world', { alias: ['w'] });
 
cli.run(process.argv);
$ cli hello --world
true

or

$ cli hello -w earth
earth

Routes

import * as bashr from 'bashr';
 
const food = new bashr.CLI('food');
 
const fruit = food.route('fruit');
 
fruit.command('banana', () => {
    console.log("I'm a banana!");
});
 
food.run(process.argv);
$ food fruit banana
I'm a banana!

Package Sidebar

Install

npm i bashr

Weekly Downloads

6

Version

0.0.6

License

ISC

Unpacked Size

80.7 kB

Total Files

33

Last publish

Collaborators

  • ferrantejake
  • boats
  • droplit-admin
  • chriswoodle