in lib/command_runner.dart [151:210]
Future<T?> runCommand(ArgResults topLevelResults) async {
var argResults = topLevelResults;
var commands = _commands;
Command? command;
var commandString = executableName;
while (commands.isNotEmpty) {
if (argResults.command == null) {
if (argResults.rest.isEmpty) {
if (command == null) {
// No top-level command was chosen.
printUsage();
return null;
}
command.usageException('Missing subcommand for "$commandString".');
} else {
var requested = argResults.rest[0];
// Build up a help message containing similar commands, if found.
var similarCommands =
_similarCommandsText(requested, commands.values);
if (command == null) {
usageException(
'Could not find a command named "$requested".$similarCommands');
}
command.usageException('Could not find a subcommand named '
'"$requested" for "$commandString".$similarCommands');
}
}
// Step into the command.
argResults = argResults.command!;
command = commands[argResults.name]!;
command._globalResults = topLevelResults;
command._argResults = argResults;
commands = command._subcommands as Map<String, Command<T>>;
commandString += ' ${argResults.name}';
if (argResults.options.contains('help') && argResults['help']) {
command.printUsage();
return null;
}
}
if (topLevelResults['help']) {
command!.printUsage();
return null;
}
// Make sure there aren't unexpected arguments.
if (!command!.takesArguments && argResults.rest.isNotEmpty) {
command.usageException(
'Command "${argResults.name}" does not take any arguments.');
}
return (await command.run()) as T?;
}