Can you splat positional arguments in PowerShell?

╄→гoц情女王★ 提交于 2019-12-08 03:30:45

问题


Does PowerShell support splatting of positional arguments as opposed to named parameters?


回答1:


PowerShell's argument splatting (see Get-Help about_Splatting) offers two fundamental choices:

  • splatting via a hashtable: works for named arguments only
  • splatting via an array-like value: works for positional arguments only - except in non-advanced functions that pass all [unbound] arguments through to another command via @Args, in which case named arguments are recognized too (i.e., by splatting the automatic $Args array containing all unbound arguments).

Note: This dichotomy applies when passing arguments to PowerShell cmdlets / functions (with declared parameters), whereas external programs perform their own argument parsing, which may or may not interpret the set of arguments passed as named.[1]

That said, you can combine either form with regular, individual argument passing - using any combination of individual positional arguments, individual named arguments, hashtable-splatting, and array-splatting.

In both cases, the source data structure must be:

  • stored in a variable beforehand.

  • referenced with sigil @ instead of $.

Note: A future enhancement, detailed in this RFC, will bring the ability to splat expressions directly, without the need for an intermediate variable, though as of PowerShell Core 7 it is unclear when this will be implemented.


Examples:

# Positional binding via *array*. 
# Note that a function's / script block's parameters are by default positional.
PS> $posArgs = 'one', 'two'; & { param($foo, $bar) "`$foo: $foo"; "`$bar: $bar" } @posArgs
$foo: one
$bar: two


# Named binding via *hashtable*
PS> $namedArgs=@{bar='two';foo='one'}; & { param($foo, $bar) "`$foo: $foo"; "`$bar: $bar" } @namedArgs
$foo: one
$bar: two

# *Combining* hashtable splatting with a regular, positional argument
PS> $namedArgs=@{bar='two'}; & { param($foo, $bar) "`$foo: $foo"; "`$bar: $bar" } @namedArgs one
$foo: one
$bar: two     

[1] Splatting with external programs:

Generally, you do not need splatting when you call external programs, because:

  • You can pass arrays as-is (with the usual $ sigil)

    • The only exception is if you wanted to include %--, the stop-parsing symbol (see Get-Help about_Parsing, in the array of arguments; you do need to use the @ sigil in that event.

    • Use the individual array elements to satisfy the external program's syntax requirements, including its named arguments, if any
      (e.g., $args = '/c', 'ver'; cmd $args to execute cmd /c ver).

  • The way hashtable splats are translated into command-line tokens may or may not be recognized by external programs:

    • Specifically, a hashtable entry with key <paramName> and value <value> is translated into a single argument formatted as -<paramName>:<value> - a format that not too many external command-line utilities recognize.


来源:https://stackoverflow.com/questions/51219038/can-you-splat-positional-arguments-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!