예제 #1
0
        public bool TryDivide(string args, out IParameters p)
        {
            p = new Parameters();
            string argStr = args.Trim();

            p.SimpleArgs.AddRange(argStr.Split(' '));
            if (argStr == "")
            {
                p = null;
                return(false);
            }

            var splitedParam = new List <string>();

            try
            {
                splitedParam.AddRange(argStr.Split(' '));
                foreach (var item in splitedParam)
                {
                    if (Quotes.Any(q => ContainsChar(q, item)))
                    {
                        throw new ArgumentException();
                    }
                }

                bool combined = true;
                foreach (var item in _quotesStr)
                {
                    for (int i = 0; i < splitedParam.Count - 1; i++)
                    {
                        string cur = splitedParam[i], next = splitedParam[i + 1];

                        if (cur.StartsWith(item) && !cur.EndsWith(item))
                        {
                            combined        = false;
                            splitedParam[i] = cur + " " + next;
                            splitedParam.Remove(next);
                            if (splitedParam[i].EndsWith(item))
                            {
                                combined = true;
                            }
                            i--;
                        }
                    }
                    if (!combined)
                    {
                        throw new ArgumentException("Expect '" + item + "'.");
                    }
                }

                string tmpKey           = null;
                bool   isLastKeyOrValue = false;

                splitedParam.Add(_cmdFlagStr);
                foreach (var item in splitedParam)
                {
                    string tmpValue = null;
                    if (item.StartsWith(_cmdFlagStr))
                    {
                        if (tmpKey != null)
                        {
                            p.Switches.Add(tmpKey);
                        }

                        tmpKey           = item.Remove(0, 1);
                        isLastKeyOrValue = true;
                    }
                    else
                    {
                        foreach (var q in Quotes)
                        {
                            tmpValue = tmpValue == null?item.Trim(q) : tmpValue.Trim(q);
                        }
                        if (!isLastKeyOrValue)
                        {
                            p.FreeArgs.Add(tmpValue);
                            //throw new ArgumentException("Expect key.");
                        }
                        else
                        {
                            p.Args.Add(tmpKey, tmpValue);
                            tmpKey           = null;
                            isLastKeyOrValue = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                p = null;
                return(false);
            }
            return(true);
        }