是否可以在PowerShell中实现子命令模式?类似的东西:
PS1
示例:Git,svn,Homebrew
一般架构是什么?将实际工作委托给脚本块的单个函数?每个子命令都在其自己的Get-Command
文件中隔离,该文件是主脚本的点源? PowerShell的各种元数据功能(例如[ $(xwininfo -id 0x60001d -all | awk '/Maximized/{print}' | wc -l) -eq 2 ] && echo Maximized
)是否能够“检查”子命令?
答案 0 :(得分:8)
我想到了这种模式并找到了两种方法。我没有找到 在我的实践中真正的应用,所以这项研究相当学术化。但 下面的脚本工作正常。
实现此模式的现有工具(以自己的方式)是 scoop
模式子命令实现了经典的命令行界面
app <command> [parameters]
此模式引入了一个提供命令的脚本app.ps1
而不是在脚本库中提供多个脚本或函数
模块。每个命令都是特殊子目录中的脚本,例如 ./命令
获取可用命令
app
调用命令
app c1 [parameters of Command\c1.ps1]
获取命令帮助
app c1 -? # works with splatting approach
app c1 -help # works with dynamic parameters
脚本 app.ps1 可能包含命令使用的常用功能。
优点:
-?
可以提供帮助(简短帮助)。缺点:
优点:
缺点:
-help
。<强> splat.ps1 强>
#requires -Version 3
param(
$Command
)
if (!$Command) {
foreach($_ in Get-ChildItem $PSScriptRoot\Command -Name) {
[System.IO.Path]::GetFileNameWithoutExtension($_)
}
return
}
& "$PSScriptRoot\Command\$Command.ps1" @args
<强> dynamic.ps1 强>
param(
[Parameter()]$Command,
[switch]$Help
)
dynamicparam {
${private:*pn} = 'Verbose', 'Debug', 'ErrorAction', 'WarningAction', 'ErrorVariable', 'WarningVariable', 'OutVariable', 'OutBuffer', 'PipelineVariable'
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Definition
$Command = $PSBoundParameters['Command']
if (!$Command) {return}
$_ = Get-Command -Name "$PSScriptRoot\Command\$Command.ps1" -CommandType ExternalScript -ErrorAction 1
if (!($_ = $_.Parameters) -or !$_.Count) {return}
${private:*r} = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
(${private:*a} = New-Object System.Collections.ObjectModel.Collection[Attribute]).Add((New-Object System.Management.Automation.ParameterAttribute))
foreach($_ in $_.Values) {
if (${*pn} -notcontains $_.Name) {
${*r}.Add($_.Name, (New-Object System.Management.Automation.RuntimeDefinedParameter $_.Name, $_.ParameterType, ${*a}))
}
}
${*r}
}
end {
if (!$Command) {
foreach($_ in Get-ChildItem $PSScriptRoot\Command -Name) {
[System.IO.Path]::GetFileNameWithoutExtension($_)
}
return
}
if ($Help) {
Get-Help "$PSScriptRoot\Command\$Command.ps1" -Full
return
}
$null = $PSBoundParameters.Remove('Command')
& "$PSScriptRoot\Command\$Command.ps1" @PSBoundParameters
}