run method
Run the CLI with the provided arguments
Implementation
Future<int> run(List<String> arguments) async {
// Handle global flags and version first
if (arguments.contains('--version') || arguments.contains('-v')) {
_printVersion();
return 0;
}
if (arguments.isEmpty ||
arguments.contains('--help') ||
arguments.contains('-h')) {
_printGlobalUsage();
return 0;
}
try {
// Get command name
final commandName = arguments.first;
final commandArgs = arguments.skip(1).toList();
// Handle help for specific command
if (commandArgs.contains('--help') || commandArgs.contains('-h')) {
final command = _commands[commandName];
if (command != null) {
command.printHelp();
return 0;
}
}
// Find and execute command
final command = _commands[commandName];
if (command == null) {
ToolkitLogger.err('Unknown command: $commandName');
_printGlobalUsage();
return 1;
}
// Set verbose mode if global verbose flag is present
if (arguments.contains('--verbose')) {
command.verbose = true;
// Remove --verbose from command args to avoid conflicts
commandArgs.removeWhere((arg) => arg == '--verbose');
}
return await command.run(commandArgs);
} catch (e) {
ToolkitLogger.err('Unexpected error: $e');
return 1;
}
}