简体   繁体   English

如何为 C# 命令行开关提供默认值

[英]How to provide default value for SwitchParameter for C# commandlets

Following is the commandlet and I need it to be operate able by providing the switch 'Uninstall' or 'install' switch.以下是命令行开关,我需要通过提供“卸载”或“安装”开关来操作它。 By default 'install' is present.默认情况下存在“安装”。

    [Cmdlet(VerbsLifecycle.Install, "MyPackage")]
    [UsedImplicitly]
    public class InstallMyPackageCommand : PSCmdlet
    {            
        [UsedImplicitly]
        [Parameter(Position = 0, Mandatory = false, ParameterSetName = "Uninstall")]
        public SwitchParameter Uninstall { get; set; } = !SwitchParameter.Present;

        [UsedImplicitly]
        [ValidateNotNullOrEmpty]
        [ValidateNotNull]
        [Parameter(Position = 1, Mandatory = true)]
        public string Path { get; set; }

        protected override void BeginProcessing()
        {
            base.BeginProcessing();
        }
    }

But above code is not recognizing not provided switch as 'Uninstall' and tries to bind the file path with first switch parameter which is entirely not the file path.但是上面的代码没有将未提供的开关识别为“卸载”,并尝试将文件路径与第一个开关参数绑定,该参数完全不是文件路径。 Even the first parameter is not manadadatory and default value is present.甚至第一个参数也不是强制性的,并且存在默认值。

Following error i receive when I issue the command "Install-MyPackage "C:\Projects\MsiPackages"当我发出命令"Install-MyPackage "C:\Projects\MsiPackages"时收到以下错误

 Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter'

required by parameter 'Uninstall'.参数“卸载”需要。

Can any body let me know it's solution.任何机构都可以让我知道它的解决方案。

I think it's ok to privide the default value for SwitchParameter as:我认为可以将 SwitchParameter 的默认值设置为:

  public SwitchParameter Install { get; set; } = SwitchParameter.Present;

While your own answer technically does answer your question relating to how to set a default value for a switch parameter.虽然您自己的答案在技术上确实回答了有关如何为开关参数设置默认值的问题。 Using it in the way you describe is not recommended for a few reasons.出于几个原因,不建议以您描述的方式使用它。

Functions with verbs like Install are supposed to be paired with their associated opposite (where it makes sense).带有像 Install 这样的动词的函数应该与它们相关的反义词配对(在有意义的地方)。 Users will expect to run Install-MyPackage <blah> followed by Uninstall-MyPackage <blah> rather than Install-MyPackage <blah> -Uninstall .用户将期望运行Install-MyPackage <blah>后跟Uninstall-MyPackage <blah>而不是Install-MyPackage <blah> -Uninstall For reference, here's the list of Approved Verbs for Powershell .作为参考,这里是Powershell 的已批准动词列表。

Switch parameters in general are assumed to be "off" by default and "on" only when specified.开关参数一般默认为“off”,仅在指定时为“on”。 While it's possible to specify a switch parameter with an explicit value like -MySwitch:$false , a large percentage of users don't know this.虽然可以使用-MySwitch:$false之类的显式值指定开关参数,但大部分用户不知道这一点。 It's also not really discoverable without reviewing the full Get-Help output for the function.如果不查看 function 的完整Get-Help output,也不会真正发现它。

In short, please don't do this.简而言之,请不要这样做。 Your users will thank you.您的用户会感谢您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM