run method

Future<void> run(
  1. AFCommandContext ctx
)

Runs the command specified by the arguments in ctx

Implementation

Future<void> run(AFCommandContext ctx) async {
  final args = ctx.arguments.arguments;
  if(args.isEmpty) {
    printUsage();
    return;
  }

  final commandName = args.first;
  final command = findByName(commandName);
  if(command == null) {
    printUsage(error: "Unknown command $commandName");
    return;
  }


  if(command.subcommands.isNotEmpty) {
    if(args.length < 2) {
      printUsage(error: "Command $commandName expects a subcommand", command: command);
      return;
    }
    final subcommandName = args[1];
    final subcommand = command.subcommands[subcommandName];
    if(subcommand == null) {
      printUsage(error: "Command $commandName does not have a subcommand named $subcommandName, command: command");
      return;
    }
    ctx.setCommandArgCount(2);
    try {
      await subcommand.run(ctx);
    } on AFCommandError catch(e) {
      _handleError(ctx, e, subcommand);
    } on Exception catch(e) {
      _handleException(ctx, e, subcommand);
    }
  } else {
    ctx.setCommandArgCount(1);
    try {
      await command.run(ctx);
    } on AFCommandError catch(e) {
      _handleError(ctx, e, command);
    } on Exception catch (e) {
      _handleException(ctx, e, command);
    }
  }
}