使用 Visual Studio Code 的多个命令/任务 [英] Multiple commands/tasks with Visual Studio Code

查看:40
本文介绍了使用 Visual Studio Code 的多个命令/任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地文件夹,用作多个小示例和玩具代码的便笺簿.我在这个目录中存储了大量的 python、C++、shell 脚本等.

I have a local folder that I use as a scratch pad for multiple little sample and toy pieces of code. I store a host of python, C++, shell scripts etc. in this directory.

我正在使用 Visual Studio Code(在 OS X 上)并且正在研究它的任务无需切换到终端即可运行/编译代码片段.

I'm using Visual Studio Code (on OS X) and am looking into its tasks to run/compile the code snippets without having to switch to a terminal.

例如,我发现以下任务将运行python在当前打开的文件上.

For example, I found this following task will run python on the currently open file.

// A task runner that runs a python program
{
    "version": "0.1.0",
    "command": "/usr/bin/python",
    "args": ["${file}"]
}

无论我当前正在编辑的文件类型如何,此任务都将使用 python 作为任务运行器.

This task will use python as the task runner irrespective of the type of file I'm currently editing.

如何实现任务以根据文件类型运行命令(或在多个命令之间进行选择)?IE.如果我正在编辑 C++ 文件,它将运行 clang++.

How do I implement a task to run a command based on the file type (or select between multiple commands)? I.e. if I'm editing a C++ file, it will run clang++.

  • 如果我无法根据文件类型执行此操作;有什么替代方法吗?
  • 另一种选择是;是否支持多个命令?

推荐答案

您始终可以使用 bash 作为您的任务运行器,然后将任意终端命令分配为您的任务.

You can always use bash as your task runner and then assign arbitrary terminal commands as your tasks.

{
    "version": "0.1.0",
    "command": "bash",
    "isShellCommand": true,
    "showOutput": "always",
    "args": [
        "-c"
    ],
    "tasks": [
        {
            "taskName": "My First Command",
            "suppressTaskName": true,
            "isBuildCommand": true,
            "args": ["echo cmd1"]
        },
        {
            "taskName": "My Command Requiring .bash_profile",
            "suppressTaskName": true,
            "args": ["source ~/.bash_profile && echo cmd2"]
        },
        {
            "taskName": "My Python task",
            "suppressTaskName": true,
            "args": ["/usr/bin/python ${file}"]
        }
    ]
}

关于这里发生的事情的一些说明:

A few notes on what is happening here:

  • 将bash -c 用于所有任务,将其放入命令的args 列表中,以便我们可以运行任意命令.echo 语句只是示例,但可以是任何可从 bash 终端执行的内容.
  • args 数组将包含要传递给 bash -c 的单个字符串(单独的项目将被视为 bash 命令的多个参数,而不是关联的命令使用 -c 参数).
  • suppressTaskName 用于将 taskName 排除在外
  • 第二个命令显示了如何加载 ~/.bash_profile 如果您需要它提供的任何东西,例如别名、环境变量等
  • 第三个命令显示了如何使用您提到的 Python 命令
  • Using bash -c for all tasks by putting it in args list of the command so that we can run arbitrary commands. The echo statements are just examples but could be anything executable from your bash terminal.
  • The args array will contain a single string to be passed to bash -c (separate items would be treated as multiple arguments to the bash command and not the command associated with the -c arg).
  • suppressTaskName is being used to keep the taskName out of the mix
  • The second command shows how you can load your ~/.bash_profile if you need anything that it provides such as aliases, env variables, whatever
  • Third command shows how you could use your Python command you mentioned

这不会给你任何类型的文件扩展名检测,但你至少可以使用 cmd+p 然后输入task"来获取你的任务列表.你总是可以用 isBuildCommandisTestCommand 标记你的 2 个最常用的命令,以通过 cmd+shift+<运行它们kbd>b 或 cmd+shift+t 分别.

This will not give you any sort of file extension detection, but you can at least use cmd+p then type "task " to get a list of your tasks. You can always mark your 2 most common commands with isBuildCommand and isTestCommand to run them via cmd+shift+b or cmd+shift+t respectively.

这个答案提供了一些可能对您有用的有用信息.

This answer has some helpful information that might be useful to you as well.

这篇关于使用 Visual Studio Code 的多个命令/任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆