Esempio n. 1
0
        private ParsingResult ProcessSwitch(SwitchDescriptor[] S, ref List <string> Param)
        {
            var PR = new ParsingResult();

            int SwRequired = S.Where(f => f.Required).Count(),
                SwPossible = S.Count();

            string First = Param.First();
            string Switch = First.Before(SwitchArgumentSeperator, First), SwitchValue = First.After(SwitchArgumentSeperator);

            // get switch
            var Sw = GetSwitch(S, Switch);

            // and validate possible
            var SwitchValueResult = ValidateSwitchValue(Sw, SwitchValue);

            // if success
            if (SwitchValueResult.Success)
            {
                var SwitchArguments = new List <SwitchArgumentResult>();
                var SwResult        = new SwitchResult(Sw.SwitchText, Sw.ShortSwitch, Sw.SwitchValueType, SwitchValue);

                // remove switch ...
                //Param.RemoveAt(0);

                // check arguments .. switch arguments are always required
                for (int j = 0; j < Sw.SwitchArguments?.Length; j++)
                {
                    First = Skip(ref Param);
                    if (TestSwitch(S, First.Before(SwitchArgumentSeperator, First)))
                    {
                        return(WithResult(PR, "switch argument missing"));
                    }
                    else
                    {
                        SwitchArguments.Add(new SwitchArgumentResult(Sw.SwitchArguments[j].Argument, First));
                    }
                }
                SwResult.SwitchArguments.AddRange(SwitchArguments);

                // add to result ..
                if (Sw.Required)
                {
                    SwRequired--;
                }
                SwPossible--;

                PR.Switches.Add(SwResult);
            }
            else
            {
                return(WithResult(PR, SwitchValueResult));
            }

            //// final checks
            //if (SwPossible < 0)
            //    return WithResult(PR, "too many switches");

            //if (SwRequired > 0)
            //    return WithResult(PR, "required switch missing");

            return(PR);
        }
Esempio n. 2
0
        /// <summary>
        /// parse common switches an remove them from <paramref name="Param"/>
        /// </summary>
        /// <param name="S">list of <see cref="SwitchDescriptor"/>to search in
        /// <param name="Param">parameters to parse</param>
        /// <returns>success and data if pass</returns>
        private ParsingResult ParseCommonSwitches(SwitchDescriptor[] S, ref List <string> Param)
        {
            var ParsingResult = WithSuccess();

            int SwRequired = S.Where(f => f.Required).Count(),
                SwPossible = S.Count();

            int i = 0;

            while (i < Param.Count())
            {
                string Switch = Param[i].Before(SwitchArgumentSeperator, Param[i]), SwitchValue = Param[i].After(SwitchArgumentSeperator);

                if (TestSwitch(S, Switch))
                {
                    var Sw = GetSwitch(S, Switch);
                    var SwitchValueResult = ValidateSwitchValue(Sw, SwitchValue);
                    if (SwitchValueResult.Success)
                    {
                        var SwitchArguments = new List <SwitchArgumentResult>();
                        var SwResult        = new SwitchResult(Sw.SwitchText, Sw.ShortSwitch, Sw.SwitchValueType, SwitchValue);

                        // remove switch ...

                        // check arguments .. switch arguments are always required
                        for (int j = 0; j < Sw.SwitchArguments?.Length; j++)
                        {
                            Param.RemoveAt(i);
                            if (TestSwitch(S, Param[i].Before(SwitchArgumentSeperator, Param[i])))
                            {
                                return(WithResult(ParsingResult, "switch argument missing"));
                            }
                            else
                            {
                                SwitchArguments.Add(new SwitchArgumentResult(Sw.SwitchArguments[j].Argument, Param[i]));
                            }
                        }

                        Param.RemoveAt(i);

                        SwResult.SwitchArguments.AddRange(SwitchArguments);

                        // add to result ..
                        if (Sw.Required)
                        {
                            SwRequired--;
                        }
                        SwPossible--;

                        ParsingResult.Switches.Add(SwResult);
                    }
                    else
                    {
                        return(WithResult(ParsingResult, SwitchValueResult));
                    }
                }
                else
                {
                    i++;
                }
            }

            // final checks
            if (SwPossible < 0)
            {
                return(WithResult(ParsingResult, "too many common switches"));
            }

            if (SwRequired > 0)
            {
                return(WithResult(ParsingResult, "required common switches missing"));
            }

            return(ParsingResult);
        }