TypeScript decorators for commander that makes developing command line tools with Node even easier.
- Import
commander-ts
into your project.$ npm install --save commander-ts
index.ts
import {
action, command, commandOption, description, option,
optionalArg, program, requiredArg, usage,
variadicArg, version,
Command
} from 'commander-ts';
@program()
@version('1.0.0')
@description('A basic program')
@usage('--help')
export class Program {
@option('--env <env>')
env: string = null;
constructor() {}
run(@requiredArg('message') message) {
console.log(`Message: ${message}`);
}
@command()
@commandOption('--reverse')
print(
this: Command,
@requiredArg('first') first,
@optionalArg('last') last,
@variadicArg('credentials') credentials
) {
if (this.reverse) {
console.log(`Name: ${last}, ${first}, ${credentials.join(', ')}`);
} else {
console.log(`Name: ${first} ${last}, ${credentials.join(', ')}`);
}
}
}
const p = new Program();
$ node index.js "Hello world"
Hello world
$ node index.js print Jack Bauer
Jack Bauer
$ node index.js print James Bond --reverse
Bond, James