在PowerShell中实现子命令模式

时间:2015-06-11 22:21:32

标签: powershell powershell-v3.0

是否可以在PowerShell中实现子命令模式?类似的东西:

PS1

示例:Git,svn,Homebrew

一般架构是什么?将实际工作委托给脚本块的单个函数?每个子命令都在其自己的Get-Command文件中隔离,该文件是主脚本的点源? PowerShell的各种元数据功能(例如[ $(xwininfo -id 0x60001d -all | awk '/Maximized/{print}' | wc -l) -eq 2 ] && echo Maximized )是否能够“检查”子命令?

1 个答案:

答案 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 可能包含命令使用的常用功能。

splat.ps1 (就像这样 app.ps1 ) - 带splatting的模式

优点:

  • 最低代码和开销。
  • 位置参数有效。
  • -?可以提供帮助(简短帮助)。

缺点:

  • PowerShell v3 +,splatting在v2中很有趣。

dynamic.ps1 (如此 app.ps1 ) - 带有动态参数的模式

优点:

  • PowerShell v2 +。
  • TabExpansion适用于参数。

缺点:

  • 更多代码,更多运行时工作。
  • 仅命名参数。
  • 帮助-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
}
相关问题