1.0.0-alpha • Published 5 years ago
@teplovs/js-cli v1.0.0-alpha
js-cli
Small library for creating Javascript CLI applications
Installation
npm install @teplovs/js-cli --save
# or:
yarn add @teplovs/js-cli
Exampe usage
import { Command, Flag } from '@teplovs/js-cli'
const command = (
new Command({
name: 'hello',
run: (command, ...args) => {
// running subcommand 'help' if command is called like:
// $ hello help
if ('0' in args && args[0] === 'help') {
return command.getSubcommandWithName('help').parse(process.argv)
}
// get the greeting. user can use custom greeting:
// $ hello world --greeting Hey
// or use default ones (formal or non-formal):
// $ hello world
// $ hello Jack --non-formal
var greeting = command.options.greeting
? command.options.greeting
: command.options.nonFormal
? 'Hi'
: 'Hello'
// just print greeting if user didn't specify any name
if (args.length === 0) {
console.log(`${greeting}!`)
} else {
args.forEach((name) => {
console.log(`${greeting} ${name}!`)
})
}
},
})
// add flag --non-formal
.flag(
new Flag({
flag: '--non-formal',
hasValue: false,
description: 'use non-formal greeting',
})
)
// add flag --greeting with an alias -g
.flag(
new Flag({
flag: '-g, --greeting',
hasValue: true,
description: 'custom greeting',
})
)
// add subcommand 'help' that outputs help information
.subcommand(
new Command({
name: 'help',
run: (command) => {
command.parent.help()
},
})
)
)
// parse CLI arguments
command.parse(process.argv)
1.0.0-alpha
5 years ago