Repository: microsoft/terminal
File: src/cascadia/WindowsTerminal/AppHost.cpp (C++)

Code before:
void AppHost::_HandleCommandlineArgs(const Remoting::WindowRequestedArgs& windowArgs)
{
    // We did want to make a window, so let's instantiate it here.
    // We don't have XAML yet, but we do have other stuff.
    _windowLogic = _appLogic.CreateNewWindow();

    if (_peasant)
    {
        const auto& args{ _peasant.InitialArgs() };
        const bool startedForContent = !windowArgs.Content().empty();
        if (startedForContent)
        {
            _windowLogic.SetStartupContent(windowArgs.Content(), windowArgs.InitialBounds());
        }
        else if (args)
        {
            const auto result = _windowLogic.SetStartupCommandline(args.Commandline(), args.CurrentDirectory(), args.CurrentEnvironment());
            const auto message = _windowLogic.ParseCommandlineMessage();
            if (!message.empty())
            {
                AppHost::s_DisplayMessageBox({ message, result });

                if (_windowLogic.ShouldExitEarly())
                {
                    TerminateProcess(GetCurrentProcess(), gsl::narrow_cast<UINT>(result));
                    __assume(false);
                }
            }
        }

        _launchShowWindowCommand = windowArgs.ShowWindowCommand();

        // This is a fix for GH#12190 and hopefully GH#12169.
        //
        // If the commandline we were provided is going to result in us only
        // opening elevated terminal instances, then we need to not even create
        // the window at all here. In that case, we're going through this
        // special escape hatch to dispatch all the calls to elevate-shim, and
        // then we're going to exit immediately.
        if (_windowLogic.ShouldImmediatelyHandoffToElevated())
        {
            _windowLogic.HandoffToElevated();
            return;
        }

        // After handling the initial args, hookup the callback for handling
        // future commandline invocations. When our peasant is told to execute a
        // commandline (in the future), it'll trigger this callback, that we'll
        // use to send the actions to the app.
        //
        // MORE EVENT HANDLERS, same rules as the ones above.

Review comment (bug):
...this bit. I'm not entirely sure what this `ShouldExitEarly` branch is supposed to do here. Is it like the `wt.exe` flag and should just `PostQuitMessage` here? In any case, the `ExitThread` call was definitely wrong since it leaks any and all resources without cleanup.

Code after:
void AppHost::_HandleCommandlineArgs(const Remoting::WindowRequestedArgs& windowArgs)
{
    // We did want to make a window, so let's instantiate it here.
    // We don't have XAML yet, but we do have other stuff.
    _windowLogic = _appLogic.CreateNewWindow();

    if (_peasant)
    {
        const auto& args{ _peasant.InitialArgs() };
        const bool startedForContent = !windowArgs.Content().empty();
        if (startedForContent)
        {
            _windowLogic.SetStartupContent(windowArgs.Content(), windowArgs.InitialBounds());
        }
        else if (args)
        {
            const auto result = _windowLogic.SetStartupCommandline(args.Commandline(), args.CurrentDirectory(), args.CurrentEnvironment());
            const auto message = _windowLogic.ParseCommandlineMessage();
            if (!message.empty())
            {
                AppHost::s_DisplayMessageBox({ message, result });

                if (_windowLogic.ShouldExitEarly())
                {
                    ExitThread(result);
                }
            }
        }

        _launchShowWindowCommand = windowArgs.ShowWindowCommand();

        // This is a fix for GH#12190 and hopefully GH#12169.
        //
        // If the commandline we were provided is going to result in us only
        // opening elevated terminal instances, then we need to not even create
        // the window at all here. In that case, we're going through this
        // special escape hatch to dispatch all the calls to elevate-shim, and
        // then we're going to exit immediately.
        if (_windowLogic.ShouldImmediatelyHandoffToElevated())
        {
            _windowLogic.HandoffToElevated();
            return;
        }

        // After handling the initial args, hookup the callback for handling
        // future commandline invocations. When our peasant is told to execute a
        // commandline (in the future), it'll trigger this callback, that we'll
        // use to send the actions to the app.
        //
        // MORE EVENT HANDLERS, same rules as the ones above.
        _revokers.peasantExecuteCommandlineRequested = _peasant.ExecuteCommandlineRequested(winrt::auto_revoke, { this, &AppHost::_DispatchCommandline });