接受清扫器参数的 PowerShell 函数

PowerShell function that takes in sweeper arguments

我正在尝试做这样的事情:

function myfunction(...arguments) {
   iex (&some_command ...arguments)
)
}

这是一些非常粗糙的伪代码,我基本上是在制作一个别名系统。你做了一个别名,它会喷出一些代码。也就是说,你给它 command_name,然后它现在是一个你可以调用的函数,并提供你想要的任意数量的参数。

示例:

function ss(...args) {
    iex(&starship ...args)
}

然后,我可以做 ss whatever arguments i want,这相当于 starship whateverarguments i want

有点迷路了! 任何帮助深表感谢。谢谢 :D

声明一个数组参数并应用ValueFromRemainingArguments参数标志,然后splat执行内部命令时的数组:

function myFunction {
  param(
    [Parameter(ValueFromRemainingArguments = $true)]
    [string[]]$Arguments
  )

  & Write-Host @Arguments
}
PS ~> myFunction whatever arguments i want
whatever arguments i want

正如多个用户评论的那样,您可能想利用 PowerShell 的别名功能:

New-Alias myFunction Write-Host

PowerShell 会自动将目标命令的参数绑定复制到别名,因此如果目标命令接受尾随参数,别名也将如此。有关别名的详细信息,请参阅 the about_Aliases help topic