runCommand static method

Future<void> runCommand(
  1. List<String> arguments, {
  2. required List<TcCommand?> allCommands,
  3. required String menu,
})

Runs a command with the given arguments and allCommands. The menu is the text that will be written to the console. The allCommands is the list of all commands that can be given with Taycan.

Implementation

static Future<void> runCommand(List<String> arguments,
    {required List<TcCommand?> allCommands, required String menu}) async {
  List<String> argumentsForAction = arguments.toList();

  if (argumentsForAction.isEmpty) {
    TaycanConsole.writeInGreen(menu);
    return;
  }

  List<String> argumentSplit = arguments[0].split(':');

  if (argumentSplit.isEmpty || argumentSplit.length <= 1) {
    TaycanConsole.writeInRed('Invalid arguments $arguments');
    exit(2);
  }

  String type = argumentSplit[0];
  String action = argumentSplit[1];

  TcCommand? tcCommand = allCommands.firstWhereOrNull(
      (command) => type == command?.category && command?.name == action);

  if (tcCommand == null) {
    TaycanConsole.writeInRed('Invalid arguments $arguments');
    exit(1);
  }

  argumentsForAction.removeAt(0);

  await tcCommand.action!(argumentsForAction);
}