Retrieving classical information encoded in optical modes is at the heart of many quantum information processing tasks, especially in the field of quantum communication and sensing. Yet, despite its importance, the fundamental limits of optical mode discrimination have been studied only in few specific examples. Here we present a toolbox to find the optimal discrimination of any set of optical modes. The toolbox uses linear and semi-definite programming techniques, which provide rigorous (not heuristic) bounds, and which can be efficiently solved on standard computers. We study both probabilistic and unambiguous single-shot discrimination in two scenarios: the channel-discrimination scenario, typical of metrology, in which the verifier holds the light source and can set up a reference frame for the phase; and the source-discrimination scenario, more frequent in cryptography, in which the verifier only sees states that are diagonal in the photon-number basis. Our techniques are illustrated with several examples. Among the results, we find that, for many sets of modes, the optimal state for mode discrimination is a superposition or mixture of at most two number states; but this is not general, and we also exhibit counter-examples.