Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
ParameterInfo.cs147 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    /// Provides information about a cmdlet parameter for a particular parameter set.11    /// </summary>12    public class CommandParameterInfo13    {14        #region ctor15 16        /// <summary>17        /// Constructs the parameter info using the specified aliases, attributes, and18        /// parameter set metadata.19        /// </summary>20        /// <param name="parameter">21        /// The parameter metadata to retrieve the parameter information from.22        /// </param>23        /// <param name="parameterSetFlag">24        /// The parameter set flag to get the parameter information from.25        /// </param>26        /// <exception cref="ArgumentNullException">27        /// If <paramref name="parameter"/> is null.28        /// </exception>29        internal CommandParameterInfo(30            CompiledCommandParameter parameter,31            uint parameterSetFlag)32        {33            if (parameter == null)34            {35                throw PSTraceSource.NewArgumentNullException(nameof(parameter));36            }37 38            Name = parameter.Name;39            ParameterType = parameter.Type;40            IsDynamic = parameter.IsDynamic;41            Aliases = new ReadOnlyCollection<string>(parameter.Aliases);42 43            SetAttributes(parameter.CompiledAttributes);44            SetParameterSetData(parameter.GetParameterSetData(parameterSetFlag));45        }46 47        #endregion ctor48 49        #region public members50 51        /// <summary>52        /// Gets the name of the parameter.53        /// </summary>54        public string Name { get; } = string.Empty;55 56        /// <summary>57        /// Gets the type of the parameter.58        /// </summary>59        public Type ParameterType { get; }60 61        /// <summary>62        /// Gets whether or not the parameter is a dynamic parameter.63        /// </summary>64        /// <remarks>65        /// True if the parameter is dynamic, or false otherwise.66        /// </remarks>67        public bool IsMandatory { get; private set; }68 69        /// <summary>70        /// Gets whether or not the parameter is mandatory.71        /// </summary>72        /// <remarks>73        /// True if the parameter is mandatory, or false otherwise.74        /// </remarks>75        public bool IsDynamic { get; }76 77        /// <summary>78        /// Gets the position in which the parameter can be specified on the command line79        /// if not named. If the returned value is int.MinValue then the parameter must be named.80        /// </summary>81        public int Position { get; private set; } = int.MinValue;82 83        /// <summary>84        /// Gets whether the parameter can take values from the incoming pipeline object.85        /// </summary>86        public bool ValueFromPipeline { get; private set; }87 88        /// <summary>89        /// Gets whether the parameter can take values from a property inn the incoming90        /// pipeline object with the same name as the parameter.91        /// </summary>92        public bool ValueFromPipelineByPropertyName { get; private set; }93 94        /// <summary>95        /// Gets whether the parameter will take any argument that isn't bound to another parameter.96        /// </summary>97        public bool ValueFromRemainingArguments { get; private set; }98 99        /// <summary>100        /// Gets the help message for this parameter.101        /// </summary>102        public string HelpMessage { get; private set; } = string.Empty;103 104        /// <summary>105        /// Gets the aliases by which this parameter can be referenced.106        /// </summary>107        public ReadOnlyCollection<string> Aliases { get; }108 109        /// <summary>110        /// Gets the attributes that are specified on the parameter.111        /// </summary>112        public ReadOnlyCollection<Attribute> Attributes { get; private set; }113 114        #endregion public members115 116        #region private members117 118        private void SetAttributes(IList<Attribute> attributeMetadata)119        {120            Diagnostics.Assert(121                attributeMetadata != null,122                "The compiled attribute collection should never be null");123 124            Collection<Attribute> processedAttributes = new Collection<Attribute>();125 126            foreach (var attribute in attributeMetadata)127            {128                processedAttributes.Add(attribute);129            }130 131            Attributes = new ReadOnlyCollection<Attribute>(processedAttributes);132        }133 134        private void SetParameterSetData(ParameterSetSpecificMetadata parameterMetadata)135        {136            IsMandatory = parameterMetadata.IsMandatory;137            Position = parameterMetadata.Position;138            ValueFromPipeline = parameterMetadata.valueFromPipeline;139            ValueFromPipelineByPropertyName = parameterMetadata.valueFromPipelineByPropertyName;140            ValueFromRemainingArguments = parameterMetadata.ValueFromRemainingArguments;141            HelpMessage = parameterMetadata.HelpMessage;142        }143 144        #endregion private members145    }146}147