run method

  1. @override
Null run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Null run() {
  // Show the default help if no command was specified.
  if (argResults!.rest.isEmpty) {
    runner!.printUsage();
    return;
  }

  // Walk the command tree to show help for the selected command or
  // subcommand.
  var commands = runner!.commands;
  Command? command;
  var commandString = runner!.executableName;

  for (var name in argResults!.rest) {
    if (commands.isEmpty) {
      command!.usageException('Command "$commandString" does not expect a subcommand.');
    }

    if (commands[name] == null) {
      if (command == null) {
        runner!.usageException('Could not find a command named "$name".');
      }

      command.usageException('Could not find a subcommand named "$name" for "$commandString".');
    }

    command = commands[name];
    commands = command!.subcommands as Map<String, Command<T>>;
    commandString += ' $name';
  }

  command!.printUsage();
}