Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
ParameterSetSpecificMetadata.cs215 linesDownload Raw Back to engine
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4namespace System.Management.Automation5{6    internal class ParameterSetSpecificMetadata7    {8        #region ctor9 10        /// <summary>11        /// Constructs an instance of the ParameterSetSpecificMetadata using the instance of the attribute12        /// that is specified.13        /// </summary>14        /// <param name="attribute">15        /// The attribute to be compiled.16        /// </param>17        /// <exception cref="ArgumentNullException">18        /// If <paramref name="attribute"/> is null.19        /// </exception>20        internal ParameterSetSpecificMetadata(ParameterAttribute attribute)21        {22            if (attribute == null)23            {24                throw PSTraceSource.NewArgumentNullException(nameof(attribute));25            }26 27            _attribute = attribute;28            IsMandatory = attribute.Mandatory;29            Position = attribute.Position;30            ValueFromRemainingArguments = attribute.ValueFromRemainingArguments;31            this.valueFromPipeline = attribute.ValueFromPipeline;32            this.valueFromPipelineByPropertyName = attribute.ValueFromPipelineByPropertyName;33            HelpMessage = attribute.HelpMessage;34            HelpMessageBaseName = attribute.HelpMessageBaseName;35            HelpMessageResourceId = attribute.HelpMessageResourceId;36        }37 38        internal ParameterSetSpecificMetadata(39            bool isMandatory,40            int position,41            bool valueFromRemainingArguments,42            bool valueFromPipeline,43            bool valueFromPipelineByPropertyName,44            string helpMessageBaseName,45            string helpMessageResourceId,46            string helpMessage)47        {48            IsMandatory = isMandatory;49            Position = position;50            ValueFromRemainingArguments = valueFromRemainingArguments;51            this.valueFromPipeline = valueFromPipeline;52            this.valueFromPipelineByPropertyName = valueFromPipelineByPropertyName;53            HelpMessageBaseName = helpMessageBaseName;54            HelpMessageResourceId = helpMessageResourceId;55            HelpMessage = helpMessage;56        }57 58        #endregion ctor59 60        /// <summary>61        /// Returns true if the parameter is mandatory for this parameterset, false otherwise.62        /// </summary>63        /// <value></value>64        internal bool IsMandatory { get; }65 66        /// <summary>67        /// If the parameter is allowed to be positional for this parameter set, this returns68        /// the position it is allowed to be in. If it is not positional, this returns int.MinValue.69        /// </summary>70        /// <value></value>71        internal int Position { get; } = int.MinValue;72 73        /// <summary>74        /// Returns true if the parameter is positional for this parameter set, or false otherwise.75        /// </summary>76        internal bool IsPositional77        {78            get79            {80                return Position != int.MinValue;81            }82        }83 84        /// <summary>85        /// Returns true if this parameter takes all the remaining unbound arguments that were specified,86        /// or false otherwise.87        /// </summary>88        /// <value></value>89        internal bool ValueFromRemainingArguments { get; }90 91        internal bool valueFromPipeline;92        /// <summary>93        /// Specifies that this parameter can take values from the incoming pipeline object.94        /// </summary>95        internal bool ValueFromPipeline96        {97            get98            {99                return valueFromPipeline;100            }101        }102 103        internal bool valueFromPipelineByPropertyName;104        /// <summary>105        /// Specifies that this parameter can take values from a property un the incoming106        /// pipeline object with the same name as the parameter.107        /// </summary>108        internal bool ValueFromPipelineByPropertyName109        {110            get111            {112                return valueFromPipelineByPropertyName;113            }114        }115 116        /// <summary>117        /// A short description for this parameter, suitable for presentation as a tool tip.118        /// </summary>119        internal string HelpMessage { get; }120 121        /// <summary>122        /// The base name of the resource for a help message.123        /// </summary>124        internal string HelpMessageBaseName { get; }125 126        /// <summary>127        /// The Id of the resource for a help message.128        /// </summary>129        internal string HelpMessageResourceId { get; } = null;130 131        /// <summary>132        /// Gets or sets the value that tells whether this parameter set133        /// data is for the "all" parameter set.134        /// </summary>135        internal bool IsInAllSets { get; set; }136 137        /// <summary>138        /// Gets the parameter set flag that represents the parameter set139        /// that this data is valid for.140        /// </summary>141        internal uint ParameterSetFlag { get; set; }142 143        /// <summary>144        /// If HelpMessageBaseName and HelpMessageResourceId are set, the help info is145        /// loaded from the resource indicated by HelpMessageBaseName and HelpMessageResourceId.146        /// If that fails and HelpMessage is set, the help info is set to HelpMessage; otherwise,147        /// the exception that is thrown when loading the resource is thrown.148        /// If both HelpMessageBaseName and HelpMessageResourceId are not set, the help info is149        /// set to HelpMessage.150        /// </summary>151        /// <returns>152        /// Help info about the parameter153        /// </returns>154        /// <exception cref="InvalidOperationException">155        /// If the value of the specified resource is not a string and156        ///     HelpMessage is not set.157        /// </exception>158        /// <exception cref="ArgumentException">159        /// If only one of HelpMessageBaseName and HelpMessageResourceId is set160        ///     OR if no usable resources have been found, and161        ///     there are no neutral culture resources and HelpMessage is not set.162        /// </exception>163        internal string GetHelpMessage(Cmdlet cmdlet)164        {165            string helpInfo = null;166            bool isHelpMsgSet = !string.IsNullOrEmpty(HelpMessage);167            bool isHelpMsgBaseNameSet = !string.IsNullOrEmpty(HelpMessageBaseName);168            bool isHelpMsgResIdSet = !string.IsNullOrEmpty(HelpMessageResourceId);169 170            if (isHelpMsgBaseNameSet ^ isHelpMsgResIdSet)171            {172                throw PSTraceSource.NewArgumentException(isHelpMsgBaseNameSet ? "HelpMessageResourceId" : "HelpMessageBaseName");173            }174 175            if (isHelpMsgBaseNameSet && isHelpMsgResIdSet)176            {177                try178                {179                    helpInfo = cmdlet.GetResourceString(HelpMessageBaseName, HelpMessageResourceId);180                }181                catch (ArgumentException)182                {183                    if (isHelpMsgSet)184                    {185                        helpInfo = HelpMessage;186                    }187                    else188                    {189                        throw;190                    }191                }192                catch (InvalidOperationException)193                {194                    if (isHelpMsgSet)195                    {196                        helpInfo = HelpMessage;197                    }198                    else199                    {200                        throw;201                    }202                }203            }204            else if (isHelpMsgSet)205            {206                helpInfo = HelpMessage;207            }208 209            return helpInfo;210        }211 212        private readonly ParameterAttribute _attribute;213    }214}215