PowerShell中没有位置,可选PARAMS [英] Powershell non-positional, optional params

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

问题描述

我试图创造一个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

如果选项是任意数量的可选参数 - 沿着-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:

我的行为的尝试的创建将有两个参数告吹可选PARAMS和在remainingArgs变量结束。

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

<一个href=\"http://stackoverflow.com/questions/2795582/how-to-specify-a-non-positional-parameter-in-a-powershell-script/2795683\">This问题/答案提供有益的方式来实现所期望的行为,但似乎只工作,如果有至​​少有一个强制参数,并且仅当它涉及的的所有其他参数。

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

的输出如下所示:

The output is as follows:

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.

问题是这样的:我怎样才能建立一个PARAMS列表,这样的所有的的PARAMS都是可选的,而的人是位置

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]"
}

一个两个是可选的,并且只能通过名称来指定。 是强制性的,并且可能没有名称来提供。

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

这不的执行的,你必须ARG被放在最后,或者说,它没有命名。但它的允许的你想要的基本的使用模式。

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.

它也不会允许在多个值$三。这可能是你想要的。但是,如果你想治疗多发性非命名PARAMS作为是一部分 $三,然后添加 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中没有位置,可选PARAMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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