How to set up a command-based communication using I2C

Some hints:

You are storing slave input in commandBuffer[2] ff., but output from [0] so that the first two bytes are garbage.

In "else" you should read() excess bytes, else Wire.available() will never return zero.

Finally add the null byte at buffer[i], just after the last input byte.

Calling Wire.write() outside requestEvent() is undefined behaviour. Please follow the examples.

With the master code you can determine the number of received bytes and output just these. Asking for more bytes might trigger another requestFrom.