Powershell 非位置,可选参数 [英] Powershell non-positional, optional params

查看:24
本文介绍了Powershell 非位置,可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 powershell (2.0) 脚本,该脚本将接受遵循此基本模式的参数:

I'm trying to create a powershell (2.0) script that will accept arguments that follow this basic pattern:

.{script name} [options] PATH

其中 options 是任意数量的可选参数 - 按照 '-v' 的行来考虑详细信息.PATH 参数将只是最后传入的任何参数,并且是强制性的.可以在没有选项且只有一个参数的情况下调用脚本,并且该参数将被假定为路径.我在设置仅包含可选参数但也是非位置参数的参数列表时遇到问题.

Where options are any number of optional parameters - think along the lines of '-v' for verbose. The PATH argument will simply be whatever argument is passed in last, and is mandatory. One could call the script with no options and only one argument, and that argument would be assumed to be the Path. I'm having trouble setting up a parameters list that contains only optional parameters but is also non-positional.

这个快速脚本演示了我遇到的问题:

This quick script demonstrates the problem I am having:

#param test script
Param(
    $firstArg,
    $secondArg,
    [switch]$thirdArg,
    [Parameter(ValueFromRemainingArguments = $true)]
    $remainingArgs)

write-host "first arg is $firstArg"
write-host "second arg is $secondArg"
write-host "third arg is $thirdArg"
write-host "remaining: $remainingArgs"

当这样调用时:

.param-test.ps1 firstValue secondValue

脚本输出:

first arg is firstValue
second arg is secondValue
third arg is False
remaining:

我正在尝试创建的行为将使两个参数都落在可选参数中并最终出现在剩余Args 变量中.

The behavior I am trying to create would have both arguments fall through the optional params and end up in the remainingArgs variable.

这个问题/答案 有用地提供了一种实现所需行为的方法,但它似乎只有在至少有一个强制参数时才有效,并且只有当它出现在所有其他参数之前.

This question/answer helpfully provided a way to achieve the desired behavior, but it only seems to work if there is at least one mandatory parameter, and only if it comes before all of the other arguments.

我可以通过强制 firstArg 并将位置指定为 0 来演示此行为:

I can demonstrate this behavior by making firstArg mandatory and specifying a position of 0:

#param test script
Param(
    [Parameter(Mandatory=$true, Position = 0)]
    $firstArg,
    $secondArg,
    [switch]$thirdArg,
    [Parameter(ValueFromRemainingArguments = $true)]
    $remainingArgs)

    write-host "first arg is $firstArg"
    write-host "second arg is $secondArg"
    write-host "third arg is $thirdArg"
    write-host "remaining: $remainingArgs"

使用与之前相同的输入运行:

Run with the same input as before:

.param-test.ps1 firstValue secondValue

输出如下:

first arg is firstValue
second arg is
third arg is False
remaining: secondValue

第一个强制参数被赋值,剩下的所有东西都一直掉下去.

The first, mandatory argument is assigned, and everything left falls all the way through.

问题是这样的:如何设置一个参数列表,使得 所有 参数都是可选的,而 没有一个 是位置参数?

The question is this: How can I set up a params list such that all of the params are optional, and none of them is positional?

推荐答案

这个怎么样?

function test
{
   param(
      [string] $One,

      [string] $Two,

      [Parameter(Mandatory = $true, Position = 0)]
      [string] $Three
   )

   "One = [$one]  Two = [$two]  Three = [$three]"
}

OneTwo 是可选的,只能按名称指定.三个是强制性的,可以不提供名称.

One and Two are optional, and may only be specified by name. Three is mandatory, and may be provided without a name.

这些工作:

test 'foo'
    One = []  Two = []  Three = [foo]
test -One 'foo' 'bar'
    One = [foo]  Two = []  Three = [bar]
test 'foo' -Two 'bar'
    One = []  Two = [bar]  Three = [foo]

这将失败:

test 'foo' 'bar'
test : A positional parameter cannot be found that accepts argument 'bar'.
At line:1 char:1
+ test 'foo' 'bar'
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [test], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,test

这并没有强制你的强制参数放在最后,或者它没有被命名.但它允许你想要的基本使用模式.

This doesn't enforce that your mandatory arg is placed last, or that it's not named. But it allows for the basic usage pattern you want.

它也不允许 $Three 中有多个值.这可能是你想要的.但是,如果您想将多个非命名参数视为 $Three 的一部分,则添加 ValueFromRemainingArguments 属性.

It also does not allow for more than one value in $Three. This might be what you want. But, if you want to treat multiple non-named params as being part of $Three, then add the ValueFromRemainingArguments attribute.

function test
{
   param(
      [string] $One,

      [string] $Two,

      [Parameter(Mandatory = $true, Position = 0, ValueFromRemainingArguments = $true)]
      [string] $Three
   )

   "One = [$one]  Two = [$two]  Three = [$three]"
}

现在这样的工作:

test -one 'foo' 'bar' 'baz'
  One = [foo]  Two = []  Three = [bar baz]

甚至

test 'foo' -one 'bar' 'baz'
    One = [bar]  Two = []  Three = [foo baz]

这篇关于Powershell 非位置,可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆