获取vscode中任务文件的相对路径

Get relative path of the file for tasks in vscode(获取vscode中任务文件的相对路径)

本文介绍了获取vscode中任务文件的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 vscode 的 tasks.json 文件中定义了一个任务,如下所示

<代码>{版本":0.1.0",任务": [{命令":吞咽","taskName": "eslint_task",参数":[埃斯林特","- 文件",${文件}"],回声命令":真}]}

${file} 为我提供了运行此命令的文件的绝对路径(在 macOS 中从 /Volumes 开始).有什么方法可以获取同一文件的相对路径(从工作区文件夹开始的路径)?

我已经查看了任务的官方文档,但找不到任何那里的参数列表.

解决方案

找到了答案.我只需要使用 ${relativeFile} 而不是 ${file}.

这是我找到的占位符列表 -

  • ${workspaceRoot}:工作区根文件夹
  • ${file}:保存文件的路径
  • ${relativeFile}:保存文件的相对路径
  • ${fileBasename}:保存文件的基本名称
  • ${fileDirname}:保存文件的目录名
  • ${fileExtname}:保存文件的扩展名(包括.)
  • ${fileBasenameNoExt}:保存文件的基本名称,不带扩展名
  • ${cwd}:当前工作目录

来源:取自这里.p>

I have a task defined in vscode's tasks.json file as following

{
    "version": "0.1.0",
    "tasks": [
        {
            "command": "gulp",
            "taskName": "eslint_task",
            "args": [
                "eslint",
                "--file",
                "${file}"
            ],
            "echoCommand": true
        }
    ]
}

The ${file} is providing me the absolute path (starting from /Volumes in macOS) of the file on which I run this command. Is there any way I can get the relative path (Path starting from the workspace folder) of the same file instead?

I already checked the official documentation for tasks, but couldn't find any list of arguments there.

解决方案

Found the answer. I just need to use ${relativeFile} instead of ${file}.

Here's the list of placeholder I have found -

  • ${workspaceRoot}: workspace root folder
  • ${file}: path of saved file
  • ${relativeFile}: relative path of saved file
  • ${fileBasename}: saved file's basename
  • ${fileDirname}: directory name of saved file
  • ${fileExtname}: extension (including .) of saved file
  • ${fileBasenameNoExt}: saved file's basename without extension
  • ${cwd}:current working directory

Source: Taken from here.

这篇关于获取vscode中任务文件的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:获取vscode中任务文件的相对路径

基础教程推荐