import 'dart:async'; import 'package:dart_framework/utils/system_util.dart'; import 'command_base.dart'; import 'package:oto_cli/cli/cli.dart'; class CommandManager { Map commandMap = {}; CommandManager(List commands) { for (var command in commands) { commandMap[command.name] = command; } } Future execute(String? command, List parameters) async { if (command == null) { printHelp(); } else { if (commandMap.containsKey(command)) { var argStr = ''; for (var arg in parameters) { argStr += '$arg, '; } var printHelp = false; var commandItem = commandMap[command]!; if (argStr.isNotEmpty) { if (parameters[0].contains('-h')) { commandItem.printHelp(); printHelp = true; } else { argStr = argStr.replaceFirst(', ', '', argStr.length - 3); argStr = ', Arguments: [$argStr]'; } } if (!printHelp) { await CLI.println('Execute command: $command$argStr', color: Color.green); await commandItem.execute(parameters); } } else { printHelp(append: [ '${CLI.style('Error:', background: Color.red)} The ${CLI.style('"$command"', color: Color.red, style: Style.bold)} command does not exist.' ]); } } return simpleFuture; } void printHelp({List? append}) { var serviceName = CLI.current.config.serviceName; var exeName = CLI.executableFileName; final commandTag = '{COMMAND}'; var list = [ '', 'A command-line $serviceName.', '', 'Usage: ${CLI.style('$exeName <-h|[arguments]>', color: Color.greenStrong)}', '', 'Available Command:', commandTag, '', 'Command options:', ' -h Print help for the current command.', '', '', "Run `$exeName -h` for more information about a command.", ]; var index = list.indexOf(commandTag); list.removeAt(index); if (commandMap.isNotEmpty) { List commandDescs = []; for (var item in commandMap.entries) { commandDescs.add(item.value.getCommandDescription()); } list.insertAll(index, commandDescs); } if (append != null) { list.insertAll(0, append); list.insert(0, ''); } CLI.print(list); } }