execute method
Execute the command.
Implementation
@override
Future<CommandResult> execute(String args, ToolUseContext context) async {
final parts = args.trim().split(RegExp(r'\s+'));
final subcommand = parts.isNotEmpty && parts[0].isNotEmpty
? parts[0]
: 'list';
switch (subcommand) {
case 'list':
return _listHooks();
case 'add':
if (parts.length < 3) {
return const TextCommandResult(
'Usage: /hooks add <event> <command>\n'
'Events: PreToolUse, PostToolUse, Notification, Stop',
);
}
final event = parts[1];
final command = parts.sublist(2).join(' ');
addHook(event, command);
return TextCommandResult('Added hook on $event: $command');
case 'remove':
if (parts.length < 3) {
return const TextCommandResult(
'Usage: /hooks remove <event> <index>',
);
}
final event = parts[1];
final index = int.tryParse(parts[2]);
if (index == null) {
return const TextCommandResult('Index must be a number.');
}
removeHook(event, index);
return TextCommandResult('Removed hook #$index from $event.');
default:
return TextCommandResult(
'Unknown subcommand: $subcommand\n'
'Usage: /hooks [list|add <event> <cmd>|remove <event> <index>]',
);
}
}