Exemple #1
0
        //Iterate through each switch in the list of allowed switches.
        private void ValidateSwitches(int mode)
        {
            foreach (KeyValuePair <SwitchAttribute, SwitchMethodInfo> kvp in _switches)
            {
                //Get the switch parameter sets for the given switch.
                SpecifiedSwitchParameters specifiedParameters = _specifiedSwitches.FindBySwitchNames(
                    kvp.Key.Name, kvp.Key.ShortName, !kvp.Key.CaseSensitive);

                //If the switch is specified on the command-line, check the number of it's
                //occurences. If they do not match the number specified by it's Switch attribute,
                //throw an exception.
                if (specifiedParameters != null)
                {
                    if (specifiedParameters.Count < kvp.Key.MinOccurences)
                    {
                        throw new CommandLineException(CommandLineException.Codes.TooFewSwitches,
                                                       CommandLineException.Messages.TooFewSwitches, kvp.Key.MinOccurences, kvp.Key.Name);
                    }

                    if (specifiedParameters.Count > kvp.Key.MaxOccurences)
                    {
                        throw new CommandLineException(CommandLineException.Codes.TooManySwitches,
                                                       CommandLineException.Messages.TooManySwitches, kvp.Key.MaxOccurences, kvp.Key.Name);
                    }
                }

                //Check if a switch usage is specified for the switch in the current mode. If not,
                //simply ignore it and continue with the iteration.
                SwitchUsageAttribute switchUsageAttribute = kvp.Value.FindByModeUsageByMode(mode);
                if (switchUsageAttribute == null)
                {
                    continue;
                }

                //Check if the iterated switch is actually specified in the current command-line by
                //looking in the _specifiedSwitches field. Check this against it's mode usage
                //specification for the current mode, and if there a problem, throw an exception.
                bool switchSpecified = specifiedParameters != null;
                if (switchUsageAttribute.SwitchUsage == SwitchUsage.Mandatory && !switchSpecified)
                {
                    throw new CommandLineException(CommandLineException.Codes.MandatorySwitchAbsent,
                                                   CommandLineException.Messages.MandatorySwitchAbsent, kvp.Key.Name);
                }
                if (switchUsageAttribute.SwitchUsage == SwitchUsage.NotAllowed && switchSpecified)
                {
                    throw new CommandLineException(CommandLineException.Codes.NonRequiredSwitchPresent,
                                                   CommandLineException.Messages.NonRequiredSwitchPresent, kvp.Key.Name);
                }
            }
        }
Exemple #2
0
        //Iterate through the command line arguments, parse for switches and cache any switches and
        //parameters in local fields.
        private void ParseCommandLine()
        {
            Regex switchRegex = new Regex(@"^[\-\/]([\w\?]+)");
            Regex paramRegex  = new Regex(@"([\s\S\w][^,]*)");

            ArgumentType previousType = ArgumentType.NotSet;
            ArgumentType currentType  = ArgumentType.NotSet;

            foreach (string arg in _args)
            {
                this.VerifyCommandLineGrouping(previousType, currentType);

                previousType = currentType;

                Match switchMatch = switchRegex.Match(arg);
                if (!switchMatch.Success)
                {
                    currentType = ArgumentType.Parameter;
                    _specifiedParameters.Add(arg);
                    continue;
                }

                currentType = ArgumentType.Switch;

                string switchName = switchMatch.Groups[1].Value;
                SpecifiedSwitchParameters specifiedSwitchParameters;
                if (!_specifiedSwitches.TryGetValue(switchName, out specifiedSwitchParameters))
                {
                    specifiedSwitchParameters = new SpecifiedSwitchParameters();
                    _specifiedSwitches.Add(switchName, specifiedSwitchParameters);
                }

                List <string> switchParameters = new List <string>();
                specifiedSwitchParameters.Add(switchParameters);

                //If no switch parameters are specified, stop processing
                if (arg.Length == switchName.Length + 1)
                {
                    continue;
                }
                ;

                if (arg[switchName.Length + 1] != ':')
                {
                    throw new Exception("Invalid switch parameter separator");
                }

                MatchCollection parameterMatches = paramRegex.Matches(arg, switchMatch.Length + 1);
                foreach (Match parameterMatch in parameterMatches)
                {
                    string value = parameterMatch.Groups[1].Value;
                    if (value.StartsWith(","))
                    {
                        value = value.Remove(0, 1);
                    }
                    switchParameters.Add(value);
                }
            }

            this.VerifyCommandLineGrouping(previousType, currentType);
        }