import LifecycleUnit from '../../lib/src/lifecycle/Unit.ts' import {CLIService} from './service/CLI.service.ts' import {Unit} from '../../lib/src/lifecycle/decorators.ts' import {Logging} from '../../lib/src/service/logging/Logging.ts' import {parseArgs} from '../../lib/src/external/std.ts' @Unit() export default class CLIAppUnit extends LifecycleUnit { constructor( protected readonly cli: CLIService, protected readonly logger: Logging, ) { super() } public async up() { this.logger.verbose(`Handling CLI invocation...`) const args = Deno.args const parsed = parseArgs(args) console.log('args', {args, parsed}) const keyword = parsed._[0] || '' const directive = this.cli.get_directive_by_keyword(String(keyword)) this.logger.verbose(`Parsed directive: "${keyword}"`) if ( !directive ) { const help = this.cli.get_directive_by_keyword('help') if ( !help ) throw new Error('Usage directive not registered!') if ( keyword ) this.logger.error(`Invalid directive keyword: ${keyword}`) await help.prepare(args, parsed) await help.invoke() } else { await directive.prepare(args, parsed) await directive.invoke() } } }