1.4.7 • Published 5 years ago

@cthru/cmdr v1.4.7

Weekly downloads
1
License
ISC
Repository
-
Last release
5 years ago

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
1.4.7

5 years ago

1.4.6

5 years ago

1.4.5

5 years ago

1.4.4

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago