Пример #1
0
 public ArgumentContent getContent(string name, bool allowsNullValue)
 {
     foreach (ArgumentContent c in cnt)
     {
         if (c.key.Equals(name))
         {
             return(c);
         }
     }
     foreach (ArgumentPattern ptn in patterns)
     {
         if (ptn.key.Equals(name))
         {
             if (ptn.defaultValue == null)
             {
                 if (ptn.allowsMultiple)
                 {
                     ArgumentContent d = new ArgumentContent();
                     d.key = name;
                     return(d);
                 }
                 if (allowsNullValue)
                 {
                     return(null);
                 }
                 throw new NotSupportedException("Required argument not supplied: " + name);
             }
             ArgumentContent dummy = new ArgumentContent();
             dummy.key = name;
             dummy.contents.Add(ptn.defaultValue);
             return(dummy);
         }
     }
     throw new ArgumentException("Unbuilt argument requested.");
 }
Пример #2
0
        public ArgumentContent getContent(String str, String matchedTag, ArgumentBuilder bld)
        {
            String substr = str.Substring(bld.parserIndex + matchedTag.Length);
            int    end    = 0;
            bool   b      = false;

            void Loop()
            {
                for (; end < substr.Length; end++)
                {
                    char cc = substr[end];
                    switch (cc)
                    {
                    case '"':
                        b = !b;
                        break;

                    case '-':
                        if (!b)
                        {
                            return;
                        }
                        break;
                    }
                }
            }

            Loop();
            substr           = substr.Substring(0, end);
            bld.parserIndex += end + matchedTag.Length;
            String[]      cntArr = ArgumentBuilder.getSplitAtSpacesWithQuotationMarks(substr);
            List <string> cnt    = new List <string>();

            foreach (string s in cntArr)
            {
                string t = s.Trim();
                if (s.Length > 0)
                {
                    cnt.Add(t);
                }
            }

            if (cnt.Count > 1 && !allowsMultiple)
            {
                bld.defaultContent.contents.Add(cnt.GetRange(1, cnt.Count));
                cnt = cnt.GetRange(0, 1);
            }
            if (cnt.Count == 0 && type != ArgumentType.BOOLEAN)
            {
                throw new NotSupportedException("Argument " + key + " requires parameters.");
            }
            ArgumentContent c = new ArgumentContent();

            if (type == ArgumentType.BOOLEAN)
            {
                if (cnt.Count == 0)
                {
                    c.contents.Add(true);
                }
            }
            c.key = key;

            foreach (String ps in cnt)
            {
                string p;
                if (ps.StartsWith("\"") && ps.EndsWith("\"") && ps.Length > 1)
                {
                    p = ps.Substring(1, ps.Length - 2);
                }
                else
                {
                    p = ps;
                }

                switch (type)
                {
                case ArgumentType.FLOAT:
                    float flt;
                    try
                    {
                        flt = float.Parse(p);
                    }
                    catch (Exception ex)
                    {
                        throw new NotSupportedException(p + "is not a valid floating point parameter for argument " + key + ".");
                    }
                    c.contents.Add((float)flt);
                    break;

                case ArgumentType.INT:
                    int intv;
                    try
                    {
                        intv = int.Parse(p);
                    }
                    catch (Exception ex)
                    {
                        throw new NotSupportedException(p + " is not a valid integer parameter for argument " + key + ".");
                    }
                    c.contents.Add((int)intv);
                    break;

                case ArgumentType.STRING:
                    c.contents.Add(p);
                    break;

                case ArgumentType.BOOLEAN:
                    c.contents.Add(bool.Parse(p));
                    break;
                }
            }
            return(c);
        }
Пример #3
0
        public void parse(string[] args)
        {
            defaultContent = new ArgumentContent();
            StringBuilder comb = new StringBuilder();

            foreach (string a in args)
            {
                string a2 = a;
                if (!a2.StartsWith("-"))
                {
                    a2 = '"' + a2 + '"';
                }
                comb.Append(a2);
            }
            string str = comb.ToString().Trim();

            cnt = new List <ArgumentContent>();
            bool argStart = false;

            for (; parserIndex < str.Length; parserIndex++)
            {
                if (str[parserIndex] == '-')
                {
                    if (!argStart)
                    {
                        string[] defaultArgs = getSplitAtSpacesWithQuotationMarks(str.Substring(0, parserIndex));
                        foreach (string dflt in defaultArgs)
                        {
                            string t = dflt.Trim();
                            if (t.Length > 0)
                            {
                                defaultContent.contents.Add(t);
                            }
                        }
                    }
                    argStart = true;
                    bool matched = false;
                    foreach (ArgumentPattern p in patterns)
                    {
                        string matchStr = p.match(str, parserIndex);
                        if (matchStr != null)
                        {
                            cnt.Add(p.getContent(str, matchStr, this));
                            matched = true;
                            parserIndex--;
                            break;
                        }
                    }
                    if (!matched)
                    {
                        int lastIndex = str.IndexOf(' ', parserIndex);
                        if (lastIndex == -1)
                        {
                            lastIndex = str.Length;
                        }
                        throw new ArgumentException("Invalid argument: " + str.Substring(parserIndex, lastIndex - parserIndex));
                    }
                }
            }
            if (!argStart)
            {
                string[] defaultArgs = getSplitAtSpacesWithQuotationMarks(str);
                foreach (string dflt in defaultArgs)
                {
                    string t = dflt.Trim();
                    if (t.Length > 0)
                    {
                        defaultContent.contents.Add(t);
                    }
                }
            }
        }