EasyCli Decorators - auttam/easycli GitHub Wiki
EasyCli provides decorators that can be used in typescript based npm projects.
// import base program class and decorators
import { Program, Cli, Command, required } from '@auttam/easycli'
@Cli()
class DemoProgram extends Program {
main(message: string) {
console.log(message)
}
}@Cli decorator, Program.run() method is called automatically to run the program.
This is a class decorator that can be used to configure the program. It also runs the program by calling Program.run() method internally.
object to configure the program/cli tool definition like friendly name, binary name, help text etc. Please check Decorator Types for more information.
A method called with a promise returned by Program.run() method. When programDefinition.autorun is set to false, this method is not called.
// import base program class and decorators
import { Program, Cli, Command, required } from '@auttam/easycli'
@Cli({name:'this is a demo program'}, execFunc)
class DemoProgram extends Program {
main(message: string) {
console.log(message)
}
}
function execFunc(promise:any) {
promise.then((finalResult:any)=>console.log('Program ended with:', promise))
.catch((err:any)=>console.log('Program ended with error:', err))
}This is a method decorator that marks a method as the CLI command. By default methods ending with word Command e.g. printCommand are treated as the commands of the CLI but using this decorator you can mark any method as a Command method.
object to define the cli command definition. Please check Decorator Types for more information.
// import base program class and decorators
import { Program, Cli, Command, required } from '@auttam/easycli'
@Cli()
class DemoProgram extends Program {
@Command({name:'print', params:[{required:true}]})
fnPrint(message: string) {
console.log(message)
}
}This is a parameter decorator that marks a parameter of a the method as the required parameter.
Note: this decorator automatically marks the method as the command method thus using @Command is not required until another command configuration is needed.
// import base program class and decorators
import { Program, Cli, Command, required } from '@auttam/easycli'
@Cli()
class DemoProgram extends Program {
@Command({name:'print'})
fnPrint(@required message: string) {
console.log(message)
}
// this will be treated as the command method
save(@required path) {
console.log(path)
}
}