import { Options , Argv } from "yargs" ; export type ICliCommandOptions < OwnArgs > = Required < { [ key in keyof OwnArgs ] : Options ; } > ; export interface ICliCommand < OwnArgs = Record < never , never > , ParentArgs = Record < never , never > , R = any > { command : string ; describe : string ; examples ? : { command : string ; description : string ; } [ ] ; options ? : ICliCommandOptions < OwnArgs > ; subcommands ? : ICliCommand < any , OwnArgs & ParentArgs > [ ] ; handler ? : ( args : OwnArgs & ParentArgs ) => Promise < R > ; } param yargs param cliCommand export function registerCommandToYargs ( yargs , cliCommand ) { yargs . command ( { command : cliCommand . command , describe : cliCommand . describe , builder : ( yargsBuilder ) => { yargsBuilder . options ( cliCommand . options || { } ) ; for ( const subcommand of cliCommand . subcommands || [ ] ) { registerCommandToYargs ( yargsBuilder , subcommand ) ; } if ( cliCommand . examples ) { for ( const example of cliCommand . examples ) { yargsBuilder . example ( `$0 ${ example . command }` , example . description ) ; } } return yargs ; } , handler : cliCommand . handler || function emptyHandler ( ) : void { } , } ) ; }