Beispiel #1
0
        public static long GetOptionLong(this Ast.OptionsList optionsList, string name, ScriptRunner runner,
                                         ScriptEnv env, long defaultValue, long?minValue = null, long?maxValue = null)
        {
            var  num        = optionsList.GetOption <long>(name, runner, env, defaultValue);
            bool outOfRange =
                (minValue.HasValue && num < minValue.Value) ||
                (maxValue.HasValue && num > maxValue.Value);

            if (outOfRange)
            {
                if (minValue.HasValue && !maxValue.HasValue)
                {
                    throw new Exception($"{name} must be an integer ≥ {minValue.Value}.");
                }
                else if (minValue.HasValue && maxValue.HasValue)
                {
                    throw new Exception($"{name} must be an integer between {minValue.Value} and {maxValue.Value}.");
                }
                else if (!minValue.HasValue && maxValue.HasValue)
                {
                    throw new Exception($"{name} must be an integer ≤ {maxValue.Value}.");
                }
            }
            return(num);
        }
Beispiel #2
0
 public static IEnumerable <string> GetOptionKeys(this Ast.OptionsList optionsList)
 {
     if (optionsList != null)
     {
         return(optionsList.Options.Keys.Select(x => x.ToUpper()));
     }
     else
     {
         return(new string[0]);
     }
 }
Beispiel #3
0
 public static T GetOption <T>(this Ast.OptionsList optionsList, string name, ScriptRunner runner, ScriptEnv env,
                               T defaultValue)
 {
     Ast.Expr valueExpr;
     if (optionsList != null && optionsList.Options.TryGetValue(name.ToLower(), out valueExpr))
     {
         return(runner.EvaluateExpr <T>(valueExpr, env));
     }
     else
     {
         return(defaultValue);
     }
 }
Beispiel #4
0
        public static bool GetOptionBool(this Ast.OptionsList optionsList, string name, ScriptRunner runner,
                                         ScriptEnv env, bool defaultValue)
        {
            long num = optionsList.GetOption <long>(name, runner, env, defaultValue ? 1 : 0);

            if (num != 0 && num != 1)
            {
                throw new Exception($"{name} must be 0 or 1.");
            }
            else
            {
                return(num == 1);
            }
        }
Beispiel #5
0
        public static int GetOptionInt(this Ast.OptionsList optionsList, string name, ScriptRunner runner,
                                       ScriptEnv env, int defaultValue, int?minValue = null, int?maxValue = null)
        {
            var longValue = GetOptionLong(optionsList, name, runner, env, defaultValue, minValue, maxValue);

            if (longValue < int.MinValue || longValue > int.MaxValue)
            {
                throw new Exception($"{name} must be a 32-bit integer.");
            }
            else
            {
                return((int)longValue);
            }
        }
Beispiel #6
0
        private Ast.OptionsList ParseOptionsList(TokenQueue q)   // or null
        {
            var n = new Ast.OptionsList {
                SourceToken = q.SourceToken
            };

            if (!q.TakeMaybe("("))
            {
                return(null);
            }
            do
            {
                var key = Check(q, ParseIdentifier(q)).ToLower();
                q.Take(":");
                var value = ParseExpr(q);
                n.Options[key] = value;
            } while (q.TakeMaybe(","));
            q.Take(")");
            return(n);
        }
Beispiel #7
0
        public static Encoding GetOptionEncoding(this Ast.OptionsList optionsList, string name, ScriptRunner runner,
                                                 ScriptEnv env)
        {
            var encodingNum = optionsList.GetOptionInt(name, runner, env, 0);

            if (encodingNum == 0)
            {
                return(null);
            }
            else if (encodingNum < 0 || encodingNum > 65535)
            {
                throw new Exception($"{name} must be between 0 and 65535.");
            }
            else
            {
                Encoding encoding = null;
                try {
                    encoding = Encoding.GetEncoding(encodingNum);
                } catch (ArgumentOutOfRangeException) {
                    // invalid codepage
                } catch (ArgumentException) {
                    // invalid codepage
                } catch (NotSupportedException) {
                    // invalid codepage
                }

                if (encoding == null)
                {
                    throw new Exception($"The code page {encodingNum} is invalid or is not supported on this system.");
                }
                else
                {
                    return(encoding);
                }
            }
        }