Visual Studio Code中tasks.json全局任务命令选项CommandOptions配置介绍

2023-12-14 21:34:49
? ? 前往老猿Python博客 ? https://blog.csdn.net/LaoYuanPython

一、引言

从官方文档可见,一个vscode典型的task.json文件包含多种属性,格式复杂,任务可以分为全局任务局部可选任务,在tasks.json一级配置的 任务为全局任务,在二级tasks中配置的任务老猿称为局部可选任务,这个名称是老猿根据全局任务的说法自定义的。

全局任务比局部可选任务多了version、options和tasks这三个子配置项,options的类型为CommandOptions,表示传递给外部执行程序或shell的选项。

二、vscode官方文档的CommandOptions定义

下面是CommandOptions的官方文档定义:

/**
 * Options to be passed to the external program or shell
 */
export interface CommandOptions {
  /**
 * The current working directory of the executed program or shell.
 * If omitted the current workspace's root is used.
   */
  cwd?: string;

  /**
 * The environment of the executed program or shell. If omitted
 * the parent process' environment is used.
   */
  env?: { [key: string]: string };

  /**
 * Configuration of the shell when task type is `shell`
   */
  shell: {
    /**
     * The shell to use.
     */
    executable: string;

    /**
     * The arguments to be passed to the shell executable to run in command mode
     * (e.g ['-c'] for bash or ['/S', '/C'] for cmd.exe).
     */
    args?: string[];
  };
}

三、CommandOptions详解

从上述官方文档定义可以看出,CommandOptions是用于传递给shell或后台进程的运行选项,包括以下可选项:

  • cwd: 执行进程或shell的工作目录,如果没有设置则使用当前项目的根目录
  • env :执行进程或shell的需要的环境变量,如果没有设置则使用父进程的环境,如果需要设置,采用:"环境变量名:环境变量值"的方式传递,类似: “env”: { “MY_VAR1”: “value1”, “MY_VAR2”:1 }
  • shell:当type设置为"shell"时,用于配置使用的shell类型,其下有两个子项配置,executable配置执行的shell,如“bash”、“ksh”、“csh”、“dash”等,使用的前提是机器上安装了对应shell;args为传递给shell以在命令模式下运行的options参数,如bash的使用语法为:bash [options] [command_string | file],则args是options相关选项值,而command_string则是前面的command命令。

四、CommandOptions配置举例

为了说明清楚,下面举个配置正确实际不合理的一个全局任务的部分配置来看看:

"command":"g++",
    "args": [
      "-fdiagnostics-color=always",
      "-g",
      "-Wall",
      "-Wextra",
      "-O0",
      "${workspaceFolder}/hello.cpp",
      "-o",
      "${fileDirname}/$hello1",
    ],
    "options": {
         "shell": {  
          "executable":"bash",
          "args":["-c","ls -lrt *.txt"],
           }  

        }

这个配置在运行任务时选择执行g++任务时,实际执行的指令为:

bash '-c' 'ls -lrt *.txt' 'g++ -fdiagnostics-color=always -g -Wall -Wextra -O0 /home/administrator/E_DRIVER/vcwork/test/hello.cpp -o /home/administrator/E_DRIVER/vcwork/test/.vscode/$hello1'

具体信息请见如下截图:
在这里插入图片描述

五、小结

本文介绍了Visual Studio Code中tasks.json全局任务命令选项CommandOptions的详细内容,并通过配置案例来说明CommandOptions各个子配置项的作用。

vscode中关于tasks.json的官方文档地址:https://code.visualstudio.com/docs/editor/tasks-appendix

写博不易,敬请支持

如果阅读本文于您有所获,敬请点赞、评论、收藏,谢谢大家的支持!

更多关于vscode配置介绍的内容请参考专栏《国产信创之光》的其他文章。

关于老猿的付费专栏

  1. 付费专栏《https://blog.csdn.net/laoyuanpython/category_9607725.html 使用PyQt开发图形界面Python应用》专门介绍基于Python的PyQt图形界面开发基础教程,对应文章目录为《 https://blog.csdn.net/LaoYuanPython/article/details/107580932 使用PyQt开发图形界面Python应用专栏目录》;
  2. 付费专栏《https://blog.csdn.net/laoyuanpython/category_10232926.html moviepy音视频开发专栏 )详细介绍moviepy音视频剪辑合成处理的类相关方法及使用相关方法进行相关剪辑合成场景的处理,对应文章目录为《https://blog.csdn.net/LaoYuanPython/article/details/107574583 moviepy音视频开发专栏文章目录》;
  3. 付费专栏《https://blog.csdn.net/laoyuanpython/category_10581071.html OpenCV-Python初学者疑难问题集》为《https://blog.csdn.net/laoyuanpython/category_9979286.html OpenCV-Python图形图像处理 》的伴生专栏,是笔者对OpenCV-Python图形图像处理学习中遇到的一些问题个人感悟的整合,相关资料基本上都是老猿反复研究的成果,有助于OpenCV-Python初学者比较深入地理解OpenCV,对应文章目录为《https://blog.csdn.net/LaoYuanPython/article/details/109713407 OpenCV-Python初学者疑难问题集专栏目录
  4. 付费专栏《https://blog.csdn.net/laoyuanpython/category_10762553.html Python爬虫入门 》站在一个互联网前端开发小白的角度介绍爬虫开发应知应会内容,包括爬虫入门的基础知识,以及爬取CSDN文章信息、博主信息、给文章点赞、评论等实战内容。

前两个专栏都适合有一定Python基础但无相关知识的小白读者学习,第三个专栏请大家结合《https://blog.csdn.net/laoyuanpython/category_9979286.html OpenCV-Python图形图像处理 》的学习使用。

对于缺乏Python基础的同仁,可以通过老猿的免费专栏《https://blog.csdn.net/laoyuanpython/category_9831699.html 专栏:Python基础教程目录)从零开始学习Python。

如果有兴趣也愿意支持老猿的读者,欢迎购买付费专栏。

老猿Python,跟老猿学Python!

? ? 前往老猿Python博文目录 https://blog.csdn.net/LaoYuanPython ?

文章来源:https://blog.csdn.net/LaoYuanPython/article/details/135003682
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。