コード例 #1
0
        internal static int ResolveParameterSetAmbiguityBasedOnMandatoryParameters(
            Dictionary<String, MergedCompiledCommandParameter> boundParameters,
            ICollection<MergedCompiledCommandParameter> unboundParameters,
            MergedCommandParameterMetadata bindableParameters,
            ref uint _currentParameterSetFlag,
            Cmdlet command
            )
        {
            uint remainingParameterSetsWithNoMandatoryUnboundParameters = _currentParameterSetFlag;

            IEnumerable<ParameterSetSpecificMetadata> allParameterSetMetadatas = boundParameters.Values
                .Concat(unboundParameters)
                .SelectMany(p => p.Parameter.ParameterSetData.Values);
            uint allParameterSetFlags = 0;
            foreach (ParameterSetSpecificMetadata parameterSetMetadata in allParameterSetMetadatas)
            {
                allParameterSetFlags |= parameterSetMetadata.ParameterSetFlag;
            }
            remainingParameterSetsWithNoMandatoryUnboundParameters &= allParameterSetFlags;

            Diagnostics.Assert(
                ValidParameterSetCount(remainingParameterSetsWithNoMandatoryUnboundParameters) > 1,
                "This method should only be called when there is an ambiguity wrt parameter sets");

            IEnumerable<ParameterSetSpecificMetadata> parameterSetMetadatasForUnboundMandatoryParameters = unboundParameters
                .SelectMany(p => p.Parameter.ParameterSetData.Values)
                .Where(p => p.IsMandatory);
            foreach (ParameterSetSpecificMetadata parameterSetMetadata in parameterSetMetadatasForUnboundMandatoryParameters)
            {
                remainingParameterSetsWithNoMandatoryUnboundParameters &= (~parameterSetMetadata.ParameterSetFlag);
            }

            int finalParameterSetCount = ValidParameterSetCount(remainingParameterSetsWithNoMandatoryUnboundParameters);
            if (finalParameterSetCount == 1)
            {
                _currentParameterSetFlag = remainingParameterSetsWithNoMandatoryUnboundParameters;

                if (command != null)
                {
                    string currentParameterSetName = bindableParameters.GetParameterSetName(_currentParameterSetFlag);
                    command.SetParameterSetName(currentParameterSetName);
                }
                return finalParameterSetCount;
            }
            return -1;
        }