xcode 如何从终端运行命令行应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7780301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:08:21  来源:igfitidea点击:

How to run command-line application from the terminal?

xcodemacos

提问by Phil

I'm a recent convert from Windows to Macbook Pro. I am getting use to Xcode. One thing that I would enjoy is when running a debug application in the terminal. Currently, I have to press Command+R for it to compile and run in Xcode. To have the application run in the terminal, I have to do an additional step by opening the Products folder, right click the application, then 'Open as Exterior Editor'. Then the terminal opens and runs the program.

我是最近从 Windows 转换到 Macbook Pro 的人。我开始习惯 Xcode。我喜欢的一件事是在终端中运行调试应用程序。目前,我必须按 Command+R 才能在 Xcode 中编译和运行。要在终端中运行应用程序,我必须执行一个额外的步骤,打开 Products 文件夹,右键单击应用程序,然后“以外部编辑器打开”。然后终端打开并运行程序。

I would like this behavior to work automatically by pressing Command+R. It seems to me like there would be a setting to direct the output.

我希望通过按 Command+R 自动执行此行为。在我看来,会有一个设置来指导输出。

Are there any steps to accomplish this?

是否有任何步骤可以完成此操作?

回答by chown

First, make a new scheme (or edit the current one) and change the executable to Terminal.app:

首先,创建一个新方案(或编辑当前方案)并将可执行文件更改为 Terminal.app:

Scheme Info

方案信息

Then, under the "Arguments" tab, make sure "Base Expansions On" is set to your App. Then put open -a ${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}

然后,在“Arguments”选项卡下,确保将“Base Expansions On”设置为您的应用程序。然后放open -a ${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}

Scheme Arguments? ?

方案参数? ?

The command will get expanded to something like open -a /Users/Me/Library/Developer/Xcode/DerivedData/MyProj-abcdefghijklmnopqrrstuvwxyz/Build/Products/Debug-iphonesimulator/Universal.app

该命令将扩展为类似 open -a /Users/Me/Library/Developer/Xcode/DerivedData/MyProj-abcdefghijklmnopqrrstuvwxyz/Build/Products/Debug-iphonesimulator/Universal.app

open -ais how you open an App from the command line.

open -a是从命令行打开应用程序的方式。

Edit: Use ${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}instead (see comments).

编辑:${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}改为使用(见评论)。

回答by chown

Building on chown's insightful idea:

基于 chown 富有洞察力的想法:

  1. Create an AppleScript file containing:

    on run argv
        set product to item 1 of argv
        tell application "Terminal"
            activate
            do script product
        end tell
    end run
    

    This AppleScript opens Terminal.app and runs the first command-line argument inside Terminal.app

    In my configuration, I saved it as runproduct.scptunder $HOME/bin.

  2. Add a new scheme (you can duplicate your current scheme) or edit your current scheme. In the Info tab, set the executable to /usr/bin/osascript, which is a program that executes AppleScripts:

    enter image description here

  3. In the Arguments tab, add two arguments: the AppleScript location (${HOME}/bin/runproduct.scpt) and the target executable location ("${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}"), the latter being the first argument passed to the AppleScript:

    enter image description here

  1. 创建一个 AppleScript 文件,其中包含:

    on run argv
        set product to item 1 of argv
        tell application "Terminal"
            activate
            do script product
        end tell
    end run
    

    此 AppleScript 打开 Terminal.app 并运行 Terminal.app 中的第一个命令行参数

    在我的配置,我救它作为runproduct.scpt$HOME/bin

  2. 添加新方案(您可以复制当前方案)或编辑当前方案。在 Info 选项卡中,将可执行文件设置为/usr/bin/osascript,这是一个执行 AppleScripts 的程序:

    在此处输入图片说明

  3. 在 Arguments 选项卡中,添加两个参数:AppleScript 位置 ( ${HOME}/bin/runproduct.scpt) 和目标可执行文件位置 ( "${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}"),后者是传递给 AppleScript 的第一个参数:

    在此处输入图片说明

I'm not sure if that can be made to work with the debugger, though.

不过,我不确定这是否可以与调试器一起使用。

回答by MikeR

In Xcode (at least at 8 version) there is a checkbox "Use Terminal" available from Edit Scheme... > Run > Options > Console. With that option Xcode starts system Terminal.app with your binary attached (for debugging purposes for example).

在 Xcode(至少 8 版本)中有一个复选框“使用终端”,可从编辑方案...>运行>选项>控制台。使用该选项,Xcode 启动系统 Terminal.app,并附上您的二进制文件(例如用于调试目的)。

enter image description here

在此处输入图片说明