[Alt]-[Right click] on a unit will order your troops to attack with melee weapons (the exception being turreted elephants, with them it will order them to use their ranged weapons). However, you cannot order your archers to switch to melee weapons and then hold their ground to counter a cavalry charge, unfortunately. Only attacking orders can change weapons.