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)
    }
}
When using the @Cli decorator, Program.run() method is called automatically to run the program.

@Cli decorator

This is a class decorator that can be used to configure the program. It also runs the program by calling Program.run() method internally.

parameters

programDefinition?:IProgramDefinition optional

object to configure the program/cli tool definition like friendly name, binary name, help text etc. Please check Decorator Types for more information.

cbExecPromise?: (promise?: any) ⇒ any) optional

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))
}

@Command decorator

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.

parameters

commandDefinition?: ICommandDefinition optional

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)
    }
}

@required decorator

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)
    }
}
⚠️ **GitHub.com Fallback** ⚠️