Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
PseudoParameters.cs247 linesDownload Raw Back to engine
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections.Generic;5using System.Collections.ObjectModel;6 7namespace System.Management.Automation8{9    /// <summary>10    /// Represents a parameter declaration that can be constructed at runtime.11    /// </summary>12    /// <remarks>13    /// Instances of <see cref="RuntimeDefinedParameterDictionary"/>14    /// should be returned to cmdlet implementations of15    /// <see cref="IDynamicParameters.GetDynamicParameters"/>.16    ///17    /// It is permitted to subclass <see cref="RuntimeDefinedParameter"/>18    /// but there is no established scenario for doing this, nor has it been tested.19    /// </remarks>20    /// <seealso cref="RuntimeDefinedParameterDictionary"/>21    /// <seealso cref="IDynamicParameters"/>22    /// <seealso cref="IDynamicParameters.GetDynamicParameters"/>23    public class RuntimeDefinedParameter24    {25        /// <summary>26        /// Constructs a runtime-defined parameter instance.27        /// </summary>28        public RuntimeDefinedParameter()29        {30        }31 32        /// <summary>33        /// Constructs a new instance of a runtime-defined parameter using the specified parameters.34        /// </summary>35        /// <param name="name">36        /// The name of the parameter. This cannot be null or empty.37        /// </param>38        /// <param name="parameterType">39        /// The type of the parameter value. Arguments will be coerced to this type before binding.40        /// This parameter cannot be null.41        /// </param>42        /// <param name="attributes">43        /// Any parameter attributes that should be on the parameter. This can be any of the44        /// parameter attributes including but not limited to Validate*Attribute, ExpandWildcardAttribute, etc.45        /// </param>46        /// <exception cref="ArgumentException">47        /// If <paramref name="name"/> is null or empty.48        /// </exception>49        /// <exception cref="ArgumentNullException">50        /// If <paramref name="parameterType"/> is null.51        /// </exception>52        public RuntimeDefinedParameter(string name, Type parameterType, Collection<Attribute> attributes)53        {54            if (string.IsNullOrEmpty(name))55            {56                throw PSTraceSource.NewArgumentException(nameof(name));57            }58 59            if (parameterType == null)60            {61                throw PSTraceSource.NewArgumentNullException(nameof(parameterType));62            }63 64            _name = name;65            _parameterType = parameterType;66 67            if (attributes != null)68            {69                Attributes = attributes;70            }71        }72 73        /// <summary>74        /// Gets or sets the name of the parameter.75        /// </summary>76        /// <exception cref="ArgumentException">77        /// If <paramref name="value"/> is null or empty on set.78        /// </exception>79        public string Name80        {81            get82            {83                return _name;84            }85 86            set87            {88                if (string.IsNullOrEmpty(value))89                {90                    throw PSTraceSource.NewArgumentException("name");91                }92 93                _name = value;94            }95        }96 97        private string _name = string.Empty;98 99        /// <summary>100        /// Gets or sets the type of the parameter.101        /// </summary>102        /// <remarks>103        /// Arguments will be coerced to this type before being bound.104        /// </remarks>105        /// <exception cref="ArgumentNullException">106        /// If <paramref name="value"/> is null.107        /// </exception>108        public Type ParameterType109        {110            get111            {112                return _parameterType;113            }114 115            set116            {117                if (value == null)118                {119                    throw PSTraceSource.NewArgumentNullException("value");120                }121 122                _parameterType = value;123            }124        }125 126        private Type _parameterType;127 128        /// <summary>129        /// Gets or sets the value of the parameter.130        /// </summary>131        /// <remarks>132        /// If the value is set prior to parameter binding, the value will be133        /// reset before each pipeline object is processed.134        /// </remarks>135        public object Value136        {137            get138            {139                return _value;140            }141 142            set143            {144                this.IsSet = true;145                _value = value;146            }147        }148 149        private object _value;150 151        /// <summary>152        /// Gets or sets whether this parameter value has been set.153        /// </summary>154        public bool IsSet { get; set; }155 156        /// <summary>157        /// Gets or sets the attribute collection that describes the parameter.158        /// </summary>159        /// <remarks>160        /// This can be any attribute that can be applied to a normal parameter.161        /// </remarks>162        public Collection<Attribute> Attributes { get; } = new Collection<Attribute>();163 164        /// <summary>165        /// Check if the parameter is disabled due to the associated experimental feature.166        /// </summary>167        internal bool IsDisabled()168        {169            bool hasParameterAttribute = false;170            bool hasEnabledParamAttribute = false;171            bool hasSeenExpAttribute = false;172 173            foreach (Attribute attr in Attributes)174            {175                if (!hasSeenExpAttribute && attr is ExperimentalAttribute expAttribute)176                {177                    if (expAttribute.ToHide)178                    {179                        return true;180                    }181 182                    hasSeenExpAttribute = true;183                }184                else if (attr is ParameterAttribute paramAttribute)185                {186                    hasParameterAttribute = true;187                    if (paramAttribute.ToHide)188                    {189                        continue;190                    }191 192                    hasEnabledParamAttribute = true;193                }194            }195 196            // If one or more parameter attributes are declared but none is enabled,197            // then we consider the parameter is disabled.198            return hasParameterAttribute && !hasEnabledParamAttribute;199        }200    }201 202    /// <summary>203    /// Represents a collection of runtime-defined parameters that are keyed based on the name204    /// of the parameter.205    /// </summary>206    /// <remarks>207    /// Instances of <see cref="RuntimeDefinedParameterDictionary"/>208    /// should be returned to cmdlet implementations of209    /// <see cref="IDynamicParameters.GetDynamicParameters"/>.210    ///211    /// It is permitted to subclass <see cref="RuntimeDefinedParameterDictionary"/>212    /// but there is no established scenario for doing this, nor has it been tested.213    /// </remarks>214    /// <seealso cref="RuntimeDefinedParameter"/>215    /// <seealso cref="IDynamicParameters"/>216    /// <seealso cref="IDynamicParameters.GetDynamicParameters"/>217    public class RuntimeDefinedParameterDictionary : Dictionary<string, RuntimeDefinedParameter>218    {219        /// <summary>220        /// Constructs a new instance of a runtime-defined parameter dictionary.221        /// </summary>222        public RuntimeDefinedParameterDictionary()223            : base(StringComparer.OrdinalIgnoreCase)224        {225        }226 227        /// <summary>228        /// Gets or sets the help file that documents these parameters.229        /// </summary>230        public string HelpFile231        {232            get { return _helpFile; }233 234            set { _helpFile = string.IsNullOrEmpty(value) ? string.Empty : value; }235        }236 237        private string _helpFile = string.Empty;238 239        /// <summary>240        /// Gets or sets private data associated with the runtime-defined parameters.241        /// </summary>242        public object Data { get; set; }243 244        internal static readonly RuntimeDefinedParameter[] EmptyParameterArray = Array.Empty<RuntimeDefinedParameter>();245    }246}247