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

问题描述

  • 我正在使用Visual Studio 2008和.NET 3.5为Powershell 2.0编写cmdlet
  • 该cmdlet需要3个参数.

我想要的cmdlet语法是这样的:

my intended grammar of the cmdlet is something like this:

cmdletname [foo|bar] p1, p2

  • 这是因为用户必须为"-foo"或"-bar"指定一个值,但不能同时给出两个值.
  • cmdletname -foo xxx -p1 hello  -p2 world
    cmdletname -bar yyy -p1 hello  -p2 world
    

    无效输入示例

    cmdletname -foo xxx -bar yyy -p1 hello  -p2 world
    

    我的问题

    • 我的问题是关于如何在powershell中执行此操作,以便它为我完成所有检查-或是否完全有可能.
    • 我知道我可以只对foo和bar使用两个可选参数,并且只需手动进行错误检查即可.这就是我目前实施的方式.
    • 或者,我对有关不同方法的建议很感兴趣.
    • MY QUESTION

      • My question is on how to do this in powershell so that it does all the checking for me - or if that is possible at all.
      • I know I can use just have two optional parameters for foo and bar and simply do the error checking manually. That's how I have it implemented currently.
      • Alternatively, I am interested in suggestions for different approaches.
      • 推荐答案

        以下是使用从 PowerShell社区扩展.顺便说一句,您可以浏览PSCX源代码

        Here's an example of using ParameterSetName taken from a cmdlet in the PowerShell Community Extensions. BTW, for ideas you can browse the PSCX source code.

        [Cmdlet(VerbsCommon.Set, PscxNouns.Clipboard,
                DefaultParameterSetName = ParamSetText)]
        [Description("Copies the item in the system clipboard.")]
        [RelatedLink(typeof(GetClipboardCommand))]
        [RelatedLink(typeof(OutClipboardCommand))]
        [RelatedLink(typeof(WriteClipboardCommand))]
        public class SetClipboardCommand : ClipboardCommandBase
        {
            ... fields elided
        
            const string ParamSetRtf = "Rtf";
            const string ParamSetHtml = "Html";
            const string ParamSetText = "Text";
            const string ParamSetFiles = "Files";
            const string ParamSetImage = "Image";
            .
            [AllowNull]
            [Parameter(ValueFromPipeline = true, ParameterSetName = ParamSetImage)]
            public Image Image { get; set; }
            .
            [AllowNull]
            [AllowEmptyCollection]
            [Parameter(ValueFromPipeline = true, ValueFromRemainingArguments = true,
                       ParameterSetName = ParamSetFiles)]
            public FileSystemInfo[] Files { get; set; }
            .
            [AllowNull]
            [AllowEmptyString]
            [Parameter(ValueFromPipeline = true, ValueFromRemainingArguments = true,
                       ParameterSetName = ParamSetText)]
            public string Text { get; set; }
            .
            [Parameter(ValueFromPipeline = true, ValueFromRemainingArguments = true,
                       ParameterSetName = ParamSetHtml)]
            public string Html { get; set; }
            .
            [Parameter(ValueFromPipeline = true, ValueFromRemainingArguments = true,
                       ParameterSetName = ParamSetRtf)]
            public string Rtf { get; set; }
            .
            protected override void ProcessRecord()
            {
                ...
            }
            .
            protected override void EndProcessing()
            {
                ExecuteWrite(delegate
                {
                    switch (ParameterSetName)
                    {
                        case ParamSetFiles:
                            if (Paths.Count == 0)
                                WinFormsClipboard.Clear();
                            else
                                WinFormsClipboard.SetFileDropList(_paths);
                            break;
        
                        case ParamSetImage:
                            if (Image == null)
                                WinFormsClipboard.Clear();
                            else
                                WinFormsClipboard.SetImage(_image);
                            break;
        
                        case ParamSetRtf:
                            SetTextContents(Rtf, TextDataFormat.Rtf);
                            break;
        
                        case ParamSetHtml:
                            SetTextContents(Html, TextDataFormat.Html);
                            break;
        
                        default:
                            SetTextContents(Text, TextDataFormat.UnicodeText);
                            break;
                    }
                });
            }
            ...
        }
        

        请注意,该cmdlet通常声明一个默认的ParameterSetName,以帮助PowerShell确定存在歧义时使用的默认"参数集.稍后,如果需要,您可以通过查询this.ParameterSetName来确定哪个参数集有效,就像上面EndProcessing()覆盖中的switch语句一样.

        Note that the cmdlet typically declares a default ParameterSetName that helps PowerShell determine the "default" parameter set to use when there is ambiguity. Later on, if needed, you can determine which parameter set is in force by querying this.ParameterSetName as the switch statement does above in the EndProcessing() override.

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

08-23 18:27