简体   繁体   中英

Powershell binary module: Dynamic tab completion for Cmdlet parameter values

I'm writing a binary Powershell module in C# and I'd like to have a Cmdlet with a parameter that provides dynamic, run-time tab completion. However, I'm struggling to figure out how to do this in a binary module. Here's my attempt to get this working:

using System;
using System.Collections.ObjectModel;
using System.Management.Automation;

namespace DynamicParameterCmdlet
{

    [Cmdlet("Say", "Hello")]
    public class MyCmdlet : PSCmdlet
    {

        [Parameter, PSTypeName("string")]
        public RuntimeDefinedParameter Name { get; set; }

        public MyCmdlet() : base() {
            Collection<Attribute> attributes = new Collection<Attribute>() {
                new ParameterAttribute()
            };

            string[] allowedNames = NameProvider.GetAllowedNames();
            attributes.Add(new ValidateSetAttribute(allowedNames));
            Name = new RuntimeDefinedParameter("Name", typeof(string), attributes);
        }

        protected override void ProcessRecord()
        {
            string name = (string)Name.Value;
            WriteObject($"Hello, {Name}");
        }
    }

    public static class NameProvider
    {
        public static string[] GetAllowedNames()
        {
            // Hard-coded array here for simplicity but imagine in reality this
            // would vary at run-time
            return new string[] { "Alice", "Bob", "Charlie" };
        }
    }
}

This doesn't work. I don't get any tab completion functionality. I also get an error:

PS > Say-Hello -Name Alice
Say-Hello : Cannot bind parameter 'Name'. Cannot convert the "Alice" value of type "System.String" to type "System.Management.Automation.RuntimeDefinedParameter".
At line:1 char:17
+ Say-Hello -Name Alice
+                 ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Say-Hello], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,DynamicParameterCmdlet.MyCmdlet

I've found an article with an example of how to do it in a non-binary Powershell module. It seems in non-binary modules you include DynamicParam followed by statements that build and return a RuntimeParameterDictionary object. Based on this examplem I would at expect the equivalent in the PSCmdlet class, perhaps an overridable GetDynamicParameters() method or something similiar, just as there is an overridable BeginProcessing() method.

At this rate, binary modules are looking to be second-class citizens in the Powershell world. Surely there is a way to do this that I've missed?

Here is a way how you can implement custom argument completer in PowerShell v5:

Add-Type @‘
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Management.Automation;
    using System.Management.Automation.Language;
    [Cmdlet(VerbsDiagnostic.Test,"Completion")]
    public class TestCompletionCmdlet : PSCmdlet {
        private string name;
        [Parameter,ArgumentCompleter(typeof(NameCompleter))]
        public string Name {
            set {
                name=value;
            }
        }
        protected override void BeginProcessing() {
            WriteObject(string.Format("Hello, {0}", name));
        }
        private class NameCompleter : IArgumentCompleter {
            IEnumerable<CompletionResult> IArgumentCompleter.CompleteArgument(string commandName,
                                                                              string parameterName,
                                                                              string wordToComplete,
                                                                              CommandAst commandAst,
                                                                              IDictionary fakeBoundParameters) {
                return GetAllowedNames().
                       Where(new WildcardPattern(wordToComplete+"*",WildcardOptions.IgnoreCase).IsMatch).
                       Select(s => new CompletionResult(s));
            }
            private static string[] GetAllowedNames() {
                return new string[] { "Alice", "Bob", "Charlie" };
            }
        }
    }
’@ -PassThru|Select-Object -First 1 -ExpandProperty Assembly|Import-Module

In particular, you need:

  • Implement IArgumentCompleter interface. Class implementing this interface should have public default constructor.
  • Apply ArgumentCompleterAttribute attribute to property of field, used as cmdlet parameter. As parameter to attribute, you should pass IArgumentCompleter implementation.
  • In IArgumentCompleter.CompleteArgument you have wordToComplete parameter, so you can filter completion options by text, already inputted by user.

And to try it:

Test-Completion -Name Tab

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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