我正在尝试寻找一个TOML https://toml.ioPowerShell 的文件解析器。
我也找不到任何有关它的信息PowerShell 画廊 https://www.powershellgallery.com/或在预安装的 PowerShell 函数中。
Indeed, 截至撰写本文时,似乎还没有电源外壳模块用于TOML https://toml.io/ parsing发表于PowerShell 画廊:
-
搜索“TOML” https://www.powershellgallery.com/packages?q=toml截至撰写本文时,尚未产生相关匹配项。
然而,有一个.NET包可用 in the NuGet 画廊:
-
Tomlyn https://www.nuget.org/packages/Tomlyn/,其源代码存储库是here https://github.com/toml-lang/toml.
While you can从 PowerShell 使用 NuGet 包,从 PowerShell Core 7.2.2 开始,这样做并不简单, 很遗憾。
样品使用:
# Determine the package's local installation location.
# If it isn't installed, install it first, in the current user's scope.
while (-not ($installDir = (Get-Package -ErrorAction Ignore -ProviderName NuGet Tomlyn).Source)) {
$null = Install-Package -Scope CurrentUser -ErrorAction Stop -ProviderName NuGet Tomlyn
}
# Load the package's assembly into the session.
Add-Type -ErrorAction Stop -LiteralPath (Join-Path $installDir '../lib/netstandard2.0/Tomlyn.dll')
# Define a sample TOML string to parse.
$tomlStr = @'
global = "this is a string"
# This is a comment of a table
[my_table]
key = 1 # Comment a key
value = true
list = [4, 5, 6]
'@
# Parse the TOML string into an object mod)el (nested dictionaries).
$tomlTable = [Tomlyn.Toml]::ToModel($tomlStr)
# Output the '[my_table]' section's 'list' value.
# -> 4, 5, 6
# IMPORTANT: Use ['<key>'] syntax; .<key> syntax does NOT work.
$tomlTable['my_table']['list']
Note:
-
使用字典类型,PowerShellusually允许互换使用索引语法(例如['my_table']
) 和点符号 , 通过.
,成员访问运算符(例如.my_table
).
-
然而,点表示法是not支持类型的字典[Tomlyn.Model.Table]
,例如返回[Tomlyn.Toml]::ToModel()
,大概是因为该类型仅实现generic IDictionary`2
接口,而不是它的非通用对应物,IDictionary
.
- See GitHub 问题 #17190 https://github.com/PowerShell/PowerShell/issues/17190进行讨论。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)