每次用 VS Code 開發 .NET 主控台應用程式時,都需要手動設定 launch.json 來進行偵錯,建立啟動設定檔在 VS Code 還算簡單,有現成的命令可以輔助,對於 .NET 應用程式的支援度也很好。不過,我發現如果要對一個應用程式額外加入命令列參數,那就有點棘手,因為你幾乎很難從網路上找到立即可用的解決方案,若是請 AI 幫忙找答案,也幾乎只能得到錯誤的、誤解的解法,因為大部分 .NET 開發者對 VS Code 相對陌生,所以想要「好好說話」都非常困難。今天我打算要來給 AI 補一補養分了,告訴大家怎樣設定才好用。

建立範例專案
假設我們要建立一個 HTML to Markdown 的 CLI 應用程式,並且需要在啟動時傳入兩個參數:
-i: 指定一個 *.html 檔案路徑
-o: 指定一個 *.md 檔案路徑
-s: 指定一個網頁的 CSS Selector 選取主要內容範圍 (可選參數)
這兩個參數分別代表輸入與輸出,讓應用程式可以根據這兩個參數來讀取與寫入檔案內容。
接著,我們先用 .NET CLI 快速建立專案骨架,目錄結構如下:
/HTML2Markdown
HTML2Markdown.sln
/src
HTML2Markdown/HTML2Markdown.csproj
/tests
HTML2Markdown.Tests/HTML2Markdown.Tests.csproj
你可以用以下 PowerShell (pwsh.exe) 命令快速建立一個 .NET 範例專案:
# 建立方案資料夾
mkdir HTML2Markdown
cd HTML2Markdown
# 初始化方案與 .gitignore
dotnet new sln
dotnet new gitignore
dotnet new editorconfig
# 初始化 Git 版控
git init -b main
git add .
git commit -m "Initial commit"
# 初始化主專案並加入 solution
mkdir src
cd src
dotnet new console -n HTML2Markdown
cd ..
dotnet sln add src/HTML2Markdown/HTML2Markdown.csproj
git add .
git commit -m "Add HTML2Markdown console app"
# 初始化 xunit 測試專案並加入 solution
mkdir tests
cd tests
dotnet new xunit -n HTML2Markdown.Tests
cd ..
dotnet sln add tests/HTML2Markdown.Tests/HTML2Markdown.Tests.csproj
git add .
git commit -m "Add HTML2Markdown.Tests"
# 初始化你的 .NET Local Tools 與 Husky 工具
dotnet new tool-manifest
dotnet tool install Husky
git add .
git commit -m "Add Husky tool manifest"
# 安裝 Husky hooks
dotnet husky install
# 設定 Husky 的 task-runner.json 來格式化程式碼
@'
{
"$schema": "https://alirezanet.github.io/Husky.Net/schema.json",
"tasks": [
{
"name": "dotnet-format",
"command": "bash",
"args": [ "-c", "dotnet", "format", "--include", "${staged}" ],
"include": ["**/*.cs", "**/*.vb"],
"windows": {
"command": "cmd",
"args": ["/c", "dotnet", "format", "--include", "${staged}" ]
}
}
]
}
'@ | Out-File -FilePath ".husky\task-runner.json" -Encoding UTF8
# 執行 Husky 預設工作 (dotnet format)
dotnet husky run
git add .
git commit -m "Install Husky hooks and configure task-runner.json"
# 將 .husky_installed 加入 .gitignore
echo '.husky_installed' | Out-File -FilePath ".gitignore" -Encoding UTF8 -Append
# 建立 Directory.Build.props
@'
<Project>
<PropertyGroup>
<!-- Husky 安裝 Flag 檔名稱 -->
<HuskyInstalledFlagFile>`$(SolutionDir)\.husky_installed</HuskyInstalledFlagFile>
</PropertyGroup>
</Project>
'@ | Out-File -FilePath "Directory.Build.props" -Encoding UTF8
# 建立 Directory.Build.targets
@'
<Project>
<Target Name="EnsureHuskyToolAndHooks"
BeforeTargets="Restore;Build"
Condition="!Exists('`$(HuskyInstalledFlagFile)')">
<Exec Command="dotnet tool restore"
StandardOutputImportance="Low"
StandardErrorImportance="High" />
<!-- 安裝 Husky hooks 並寫入 HuskyInstalledFlagFile -->
<Exec Command="dotnet husky install"
StandardOutputImportance="Low"
StandardErrorImportance="High"
WorkingDirectory="`$(SolutionDir)" />
<WriteLinesToFile File="`$(HuskyInstalledFlagFile)"
Lines="Husky hooks installed on `$(MSBuildThisFileFullPath)"
Overwrite="true" />
</Target>
</Project>
'@ | Out-File -FilePath "Directory.Build.targets" -Encoding UTF8
# 確認專案可以正常建置
dotnet build
git add .
git commit -m "Configure Directory.Build.props and Directory.Build.targets for Husky hooks auto-setup"
在 VS Code 裡面開發主要功能
安裝本專案所需 NuGet 套件,大概有以下幾套:
dotnet add package --project .\src\HTML2Markdown\HTML2Markdown.csproj HtmlAgilityPack
dotnet add package --project .\src\HTML2Markdown\HTML2Markdown.csproj HtmlAgilityPack.CssSelectors.NetCore
dotnet add package --project .\src\HTML2Markdown\HTML2Markdown.csproj ReverseMarkdown
dotnet add package --project .\src\HTML2Markdown\HTML2Markdown.csproj McMaster.Extensions.CommandLineUtils
完整的實作請見: https://github.com/doggy8088/HTML2Markdown
如果我要在本地執行並測試這個 CLI 工具,我可能會這樣執行:
cd src\HTML2Markdown
dotnet run -- --help
如果要實際執行,可以這樣跑:
dotnet run -- -i sample.html -o sample.md -s article
這時問題來了,我如果想在 VS Code 裡面偵錯,那設定的步驟為何?
在 VS Code 裡面建立啟動設定檔 .vscode/launch.json
基本上在 VS Code 裡面建立啟動偵錯很簡單,你可以直接按下 F5 鍵,然後選擇 C#,VS Code 就會自動進入偵錯模式。
或者,你可以手動建立一個 .vscode/launch.json 檔案,內容如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "C#: HTML2Markdown Debug",
"type": "dotnet",
"request": "launch",
"projectPath": "${workspaceFolder}/src/HTML2Markdown/HTML2Markdown.csproj",
}
]
}
這兩種啟動方式雖然簡單,但是 "type": "dotnet" 這種類型的啟動設定,並沒有辦法讓我們傳入命令列參數,那沒辦法指定傳入參數的話,我們不就很難偵錯不同參數的情境了嗎?
網路上可以找到的資料,大多都是比較複雜的方法,例如搭配 tasks.json 工作設定,讓進入偵錯模式時要事先建置之類的,這些方法雖然可行,但實在有點麻煩。
最終還是讓我找到了一個不錯的解決方案,你只要建立一個 src\HTML2Markdown\Properties\launchSettings.json 檔案,設定內容範例如下:
{
"profiles": {
"HTML2Markdown": {
"commandName": "Project",
"commandLineArgs": "-i g:\\sample.html -o g:\\sample.md -s article"
}
}
}
因為有個 commandLineArgs 屬性可以讓我們直接指定命令列參數。
如此一來,我們就可以在 VS Code 裡面直接按下 F5 鍵來啟動偵錯,並且能夠帶入我們想要的參數了!👍
相關連結