Ejemplo n.º 1
0
        internal static Collection <CommandParameterSetInfo> GetParameterMetadata(CommandMetadata metadata, MergedCommandParameterMetadata parameterMetadata)
        {
            Collection <CommandParameterSetInfo> result = new Collection <CommandParameterSetInfo>();

            if (parameterMetadata != null)
            {
                if (parameterMetadata.ParameterSetCount == 0)
                {
                    const string parameterSetName = ParameterAttribute.AllParameterSets;

                    result.Add(
                        new CommandParameterSetInfo(
                            parameterSetName,
                            false,
                            uint.MaxValue,
                            parameterMetadata));
                }
                else
                {
                    int parameterSetCount = parameterMetadata.ParameterSetCount;
                    for (int index = 0; index < parameterSetCount; ++index)
                    {
                        uint currentFlagPosition = (uint)0x1 << index;

                        // Get the parameter set name
                        string parameterSetName = parameterMetadata.GetParameterSetName(currentFlagPosition);

                        // Is the parameter set the default?
                        bool isDefaultParameterSet = (currentFlagPosition & metadata.DefaultParameterSetFlag) != 0;

                        result.Add(
                            new CommandParameterSetInfo(
                                parameterSetName,
                                isDefaultParameterSet,
                                currentFlagPosition,
                                parameterMetadata));
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        internal static Collection <CommandParameterSetInfo> GetParameterMetadata(System.Management.Automation.CommandMetadata metadata, MergedCommandParameterMetadata parameterMetadata)
        {
            Collection <CommandParameterSetInfo> collection = new Collection <CommandParameterSetInfo>();

            if (parameterMetadata != null)
            {
                if (parameterMetadata.ParameterSetCount == 0)
                {
                    collection.Add(new CommandParameterSetInfo("__AllParameterSets", false, int.MaxValue, parameterMetadata));
                    return(collection);
                }
                int parameterSetCount = parameterMetadata.ParameterSetCount;
                for (int i = 0; i < parameterSetCount; i++)
                {
                    int    parameterSet          = ((int)1) << i;
                    string parameterSetName      = parameterMetadata.GetParameterSetName(parameterSet);
                    bool   isDefaultParameterSet = (parameterSet & metadata.DefaultParameterSetFlag) != 0;
                    collection.Add(new CommandParameterSetInfo(parameterSetName, isDefaultParameterSet, parameterSet, parameterMetadata));
                }
            }
            return(collection);
        }
        private void ThrowAmbiguousParameterSetException(uint parameterSetFlags, MergedCommandParameterMetadata bindableParameters)
        {
            ParameterBindingException bindingException =
                new ParameterBindingException(
                    ErrorCategory.InvalidArgument,
                    this.Command.MyInvocation,
                    null,
                    null,
                    null,
                    null,
                    ParameterBinderStrings.AmbiguousParameterSet,
                    "AmbiguousParameterSet");


            // Trace the parameter sets still active
            uint currentParameterSet = 1;

            while (parameterSetFlags != 0)
            {
                uint currentParameterSetActive = parameterSetFlags & 0x1;

                if (currentParameterSetActive == 1)
                {
                    string parameterSetName = bindableParameters.GetParameterSetName(currentParameterSet);
                    if (!String.IsNullOrEmpty(parameterSetName))
                    {
                        ParameterBinderBase.bindingTracer.WriteLine("Remaining valid parameter set: {0}", parameterSetName);
                    }
                }

                parameterSetFlags >>= 1;
                currentParameterSet <<= 1;
            }

            if (!DefaultParameterBindingInUse)
            {
                throw bindingException;
            }
            else
            {
                ThrowElaboratedBindingException(bindingException);
            }
        }
        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;
        }
 private void ThrowAmbiguousParameterSetException(int parameterSetFlags, MergedCommandParameterMetadata bindableParameters)
 {
     ParameterBindingException pbex = new ParameterBindingException(ErrorCategory.InvalidArgument, this.Command.MyInvocation, null, null, null, null, "ParameterBinderStrings", "AmbiguousParameterSet", new object[0]);
     for (int i = 1; parameterSetFlags != 0; i = i << 1)
     {
         int num2 = parameterSetFlags & 1;
         if (num2 == 1)
         {
             string parameterSetName = bindableParameters.GetParameterSetName(i);
             if (!string.IsNullOrEmpty(parameterSetName))
             {
                 ParameterBinderBase.bindingTracer.WriteLine("Remaining valid parameter set: {0}", new object[] { parameterSetName });
             }
         }
         parameterSetFlags = parameterSetFlags >> 1;
     }
     if (!base.DefaultParameterBindingInUse)
     {
         throw pbex;
     }
     base.ThrowElaboratedBindingException(pbex);
 }
Ejemplo n.º 6
0
        internal static Collection<CommandParameterSetInfo> GetParameterMetadata(CommandMetadata metadata, MergedCommandParameterMetadata parameterMetadata)
        {
            Collection<CommandParameterSetInfo> result = new Collection<CommandParameterSetInfo>();

            if (parameterMetadata != null)
            {
                if (parameterMetadata.ParameterSetCount == 0)
                {
                    const string parameterSetName = ParameterAttribute.AllParameterSets;

                    result.Add(
                        new CommandParameterSetInfo(
                            parameterSetName,
                            false,
                            uint.MaxValue,
                            parameterMetadata));
                }
                else
                {
                    int parameterSetCount = parameterMetadata.ParameterSetCount;
                    for (int index = 0; index < parameterSetCount; ++index)
                    {
                        uint currentFlagPosition = (uint)0x1 << index;

                        // Get the parameter set name
                        string parameterSetName = parameterMetadata.GetParameterSetName(currentFlagPosition);

                        // Is the parameter set the default?
                        bool isDefaultParameterSet = (currentFlagPosition & metadata.DefaultParameterSetFlag) != 0;

                        result.Add(
                            new CommandParameterSetInfo(
                                parameterSetName,
                                isDefaultParameterSet,
                                currentFlagPosition,
                                parameterMetadata));
                    }
                }
            }

            return result;
        } // GetParameterMetadata
Ejemplo n.º 7
0
 internal static Collection<CommandParameterSetInfo> GetParameterMetadata(System.Management.Automation.CommandMetadata metadata, MergedCommandParameterMetadata parameterMetadata)
 {
     Collection<CommandParameterSetInfo> collection = new Collection<CommandParameterSetInfo>();
     if (parameterMetadata != null)
     {
         if (parameterMetadata.ParameterSetCount == 0)
         {
             collection.Add(new CommandParameterSetInfo("__AllParameterSets", false, int.MaxValue, parameterMetadata));
             return collection;
         }
         int parameterSetCount = parameterMetadata.ParameterSetCount;
         for (int i = 0; i < parameterSetCount; i++)
         {
             int parameterSet = ((int) 1) << i;
             string parameterSetName = parameterMetadata.GetParameterSetName(parameterSet);
             bool isDefaultParameterSet = (parameterSet & metadata.DefaultParameterSetFlag) != 0;
             collection.Add(new CommandParameterSetInfo(parameterSetName, isDefaultParameterSet, parameterSet, parameterMetadata));
         }
     }
     return collection;
 }