互斥开关参数

时间:2020-07-05 17:52:52

标签: powershell

该函数具有一个必需的参数-Path和其他两个互斥的开关。这不是真正的功能,而是MRE(最小可复制示例)。默认操作是将文件复制到已知位置,然后将其删除。

Do-TheFile [-Path] <String[]> [[-Copy] | [-Remove]]
    -Path = filename is mandatory
    -CopyOnly = only copy the file, cannot be used with -Remove
    -RemoveOnly = only remove the file, cannot be used with -Copy

这是当前代码。

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

    ,[Parameter(Mandatory=$false, ParameterSetName='CopyOnly')]
    [switch]$CopyOnly

    ,[Parameter(Mandatory=$false ,ParameterSetName='RemoveOnly')]
    [switch]$RemoveOnly
)

控制台允许我同时指定-CopyOnly和-RemoveOnly。我的期望是,控制台不允许我同时输入-CopyOnly和-RemoveOnly,因为它们位于不同的ParameterSets中。如何指定这些ParameterSet,以便-Copy和-Remove互斥?

PS C:\src\t> Do-TheFile -Path t.txt -CopyOnly -RemoveOnly
Do-TheFile: Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided.

1 个答案:

答案 0 :(得分:0)

在这里同意其他人。

您的代码可以正常工作,就像使用IntelliSense时编写的那样,但是PowerShell不会阻止您键入其他有效的开关/变量/属性名称(在consolehost,ISE,VSCode,Visual Studio等中),并不意味着仅仅因为您同时输入了两者即可。

为什么每次都只想使用一个选项时,要进行两个开关。 只需使用简单的验证集即可。

Function Test-MyFunctionTest
{
    [cmdletbinding()]
    param 
    (
        [Parameter(Mandatory = $true,  Position = 0)]
        [string[]]$Path,
        [Parameter(Mandatory)][ValidateSet('CopyOnly', 'RemoveOnly')]
        [string]$FileAction
    )

}

# Results
<#
Test-MyFunctionTest -Path $PWD -FileAction CopyOnly
Test-MyFunctionTest -Path $PWD -FileAction RemoveOnly
#>

否则,正如您所发现的,您必须自己编写代码。例如:

Function Test-MyFunctionTestAgain
{
    [cmdletbinding()]
    param 
    (
        [Parameter(Mandatory=$true, Position=0)]
        [string[]]$Path,
        [switch]$RemoveOnly
    )

    If($RemoveOnly.IsPresent)
    {'Do the remove action'}
    Else {'Do the copy action'}
}

Test-MyFunctionTestAgain -Path $PWD
# Results
<#
Do the copy action
#>

Test-MyFunctionTestAgain -Path $PWD -RemoveOnly
# Results
<#
Do the remove action
#>

更新

至此...

”“我同意这是可行的。尽管如此,默认操作(使用 没有开关)同时复制和删除。”

...然后这个...

Function Test-MyFunctionTestMore
{
    [cmdletbinding()]
    param 
    (
        [Parameter(Mandatory = $true,  Position = 0)]
        [string[]]$Path,
        [Parameter(Mandatory = $false)][ValidateSet('CopyAndRemove', 'CopyOnly', 'RemoveOnly')]
        [string]$FileAction = 'CopyAndRemove'
    )

    Switch ($FileAction)
    {
        CopyAndRemove {'Do the copy and remove action'}
        CopyOnly      {'Do the copy only action'}
        RemoveOnly    {'Do the remove only action'}
    }
}


Test-MyFunctionTestMore -Path $PWD
# Results
<#
Do the copy and remove action
#>
Test-MyFunctionTestMore -Path $PWD -FileAction CopyOnly
# Results
<#
Do the copy only action
#>
Test-MyFunctionTestMore -Path $PWD -FileAction RemoveOnly
# Results
<#
Do the remove only action
#>

或者这样,如果您真的很想换个开关;-} ...

Function Test-MyFunctionTestSwitch
{
    [cmdletbinding()]
    param 
    (
        [Parameter(Mandatory=$true, Position=0)]
        [string[]]$Path,
        [Parameter(Mandatory = $false)][ValidateSet('CopyAndRemove', 'CopyOnly', 'RemoveOnly')]
        [string]$FileAction = 'CopyAndRemove',
        [switch]$RemoveOnly
    )

    If($RemoveOnly.IsPresent)
    {
        $FileAction = 'RemoveOnly'
        'Do the remove only action'
    }
    ElseIf ($FileAction -eq 'CopyOnly')
    {'Do the copy only action'}
    Else{'Do the copy and remove action'}
}

Test-MyFunctionTestSwitch -Path $PWD
# Results
<#
Do the copy and remove action
#>

Test-MyFunctionTestSwitch -Path $PWD -FileAction CopyOnly
# Results
<#
Do the copy only action
#>

Test-MyFunctionTestSwitch -Path $PWD -RemoveOnly
# Results
<#
Do the remove only action
#>

最后要注意的是:

尝试模拟其他一些工具的动作,或者期望PowerShell本地模拟其他一些工具的动作,参数等,真的不是期望。

如果您认为PowerShell应该具有特定功能,则可以选择submit it to the PowerShell team,使其被其他人投票以进行工作/包含,或者由于PowerShell是开源的,您可以对其进行工具化并提交审查/批准提交。

相关问题