Skip to content

DynamicOption PowerShell

Jianyun edited this page Feb 25, 2016 · 9 revisions

DynamicOption class

public class DynamicOption {
    public OptionCategory Category { get; set; }
    public OptionType ExpectedType{ get; set; }
    public string Name { get; set; }
    public bool IsRequired { get; set; }
    public IEnumerable<string> PermittedValues{get; set;}
}

Dynamic Options are specified for a given category:

Value Purpose
Category The operation that the dynamic option is for (see [[OptionCategory enum
Type the data type for the option (see [[OptionType enum
Name the option name. This is exposed to the end user.
IsRequired indicates that this option is required for this operation. If not present, the host should not proceed.
PermittedValues a collection of possible values the user is permitted to specify.
The following example shows how to define a dynamic option.  "-Destination" is required in Install-Package.
     public void GetDynamicOptions(string category, Request request)
        {
            if (request == null){
                throw new ArgumentNullException("request");
            }

            switch ((category ?? string.Empty).ToLowerInvariant()) {
                case "install":
                    request.YieldDynamicOption("Destination", Constants.OptionType.Folder, true);
                    break;  
                case "package":
                    // put any options used when searching for packages
                    request.YieldDynamicOption("FilterOnTag", Constants.OptionType.StringArray, false);
                    break;
                default:
                    request.Warning(Resources.Messages.UnkownCategory, PackageProviderName, "GetDynamicOptions", category);
                    break;
            }
        }

Please see Remarks here for retrieving dynamic option parameters.