This function looks really really like this one 

https://github.com/symfony/symfony/blob/db4f5515272874eac36fb46a9d45737c2a1319ef/src/Symfony/Component/Console/Application.php#L1048