runCommand static method

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

Run a command from the terminal menu should contain the list of commands that can be run.

Implementation

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

  if (argumentsForAction.isEmpty) {
    MetroConsole.writeInBlack(menu);
    return;
  }

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

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

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

  NyCommand? nyCommand = allCommands.firstWhereOrNull(
      (command) => type == command?.category && command?.name == action);

  if (nyCommand == null) {
    MetroConsole.writeInBlack('Invalid arguments $arguments');
    exit(1);
  }

  argumentsForAction.removeAt(0);

  await nyCommand.action!(argumentsForAction);
}