@cthru/cmdr

1.4.7 • Public • Published

cmdr

A simple solution for single or multi-command node cli scripts.

Version Downloads/week License

Description

cmdr is a utility to make it easy to create cli apps with command line parsing

Scaffolding

To get going quickly with a new cli command:

  • npx @cthru/cmdr create:single --init <command> for a single command with npm init and dependency installation
  • npx @cthru/cmdr create:multi --init <app> <command> for a multi command app with npm init and dependency installation

Examples

Single-command app

const {App, Param, Flag, Argument} = require('@cthru/cmdr')

const hello=(options)=>{
	padding=parseInt(options.padding)
	out = `${isNaN(padding)?'':Array(padding).join(' ')}Hello ${options.name}`
	options.capitalize && (out = out.toUpperCase())
	console.log(out)
}


const app =  new App(
	{
		name : "Hello world test",
		command: 'test',
		version: '1.0',
		description: "This app only exists as a proof of concept",
		callback: hello
	},
	new Param(
		{
			name:'name',
			description: 'Your first name'
		}
	),
	new Flag(
		{
			name: 'capitalize',
			short: 'C',
			description: 'Capitalize the output'
		}
	),
	new Argument(
		{
			name: 'padding',
			short: 'P',
			description: 'Make some padding before the output',
			defaultValue: '0'
		}
	)
)

app.run()

Multi-command App

cmdr also allows for multiple commands in the same app. The following example has two commands

index.js


const app =  new App(
	{
		name : "Hello World Test",
		command: 'test',
		version: '1.0',
		description: "This app only exists as a proof of concept"
	},
	require('./hello'),
	require('./bye')
)

app.run()

hello.js

const {Command, Argument, Flag, Param} = require('@cthru/cmdr')

const hello=(options)=>{
	padding=parseInt(options.padding)
	out = `${isNaN(padding)?'':Array(padding).join(' ')}Hello ${options.name}`
	options.capitalize && (out = out.toUpperCase())
	console.log(out)
}

const command = new Command(
	{
		command: 'hello',
		description: 'A simple Hello world app',
		callback: hello
	},
	new Param(
		{
			name:'name',
			description: 'Your first name'
		}
	),
	new Flag(
		{
			name: 'capitalize',
			short: 'C',
			description: 'Capitalize the output'
		}
	),
	new Argument(
		{
			name: 'padding',
			short: 'P',
			description: 'Make some padding before the output',
			defaultValue: '0'
		}
	)
)

module.exports = command

bye.js

const {Command, Argument, Flag, Param} = require('@cthru/cmdr')

const bye=(options)=>{
	padding=parseInt(options.padding)
	out = `${isNaN(padding)?'':Array(padding).join(' ')}Goodbye ${options.name}`
	options.capitalize && (out = out.toUpperCase())
	console.log(out)
}

const command = new Command(
	{
		command: 'bye',
		description: 'A simple Bye world app',
		callback: bye
	},
	new Param(
		{
			name:'name',
			description: 'Your first name'
		}
	),
	new Flag(
		{
			name: 'capitalize',
			short: 'C',
			description: 'Capitalize the output'
		}
	),
	new Argument(
		{
			name: 'padding',
			short: 'P',
			description: 'Make some padding before the output',
			defaultValue: '0'
		}
	)
)

module.exports = command

TODO

  • argument validation
  • --argument=20 format support, currently only allows --argument 20 format
  • interactive mode
  • more flexible help generation

Package Sidebar

Install

npm i @cthru/cmdr

Weekly Downloads

11

Version

1.4.7

License

ISC

Unpacked Size

22.3 kB

Total Files

22

Last publish

Collaborators

  • mark.h