You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VS Code任务中拆分${fileBasename}变量?

在VS Code任务里拆分变量实现目标curl命令

VS Code任务本身没法直接用连字符拆分变量,但有两种靠谱的方法能实现你要的效果:

方法1:用VS Code内置的正则替换变量

VS Code支持对变量做正则替换,直接用这个语法就能提取出schemaName:

把任务改成这样:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "上传Schema",
      "type": "shell",
      "command": "curl -X PUT http://localhost:9000/schema/${fileBasename/(.*)-schema.json/$1/} -d @${fileBasename}",
      "problemMatcher": []
    }
  ]
}

这里${fileBasename/(.*)-schema.json/$1/}的作用是:匹配xxx-schema.json格式的文件名,把xxx(也就是你要的schemaName)提取出来,替换到URL路径里。

方法2:用Shell的字符串处理(适合类Unix系统)

如果你的终端是bash/zsh这类,可以用shell自带的参数扩展来拆分:

任务命令改成:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "上传Schema",
      "type": "shell",
      "command": "FILE=${fileBasename}; curl -X PUT http://localhost:9000/schema/${FILE%-schema.json} -d @${FILE}",
      "problemMatcher": []
    }
  ]
}

${FILE%-schema.json}会自动去掉文件名末尾的-schema.json,直接得到schemaName,语法简单直接。

要是需要跨Windows、Mac、Linux用,选方法1;只在Mac/Linux上用,方法2更省心。

内容的提问来源于stack exchange,提问作者HotFix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 08:15:43