Exemplo n.º 1
0
        public ArgumentCheckResult Check(string arg)
        {
            if (!arg.StartsWith(SwitchToString() + Switch + SeparatorToString()))
            {
                return(ArgumentCheckResult.Failed(this, CheckResult.Wrong, ""));
            }

            if (IsSet)
            {
                if (!IsSeveralTimes)
                {
                    return(ArgumentCheckResult.Failed(this, CheckResult.IsAlreadySet, "Is already set"));
                }
            }

            String sub = arg.Substring(arg.IndexOf(SeparatorToString()) + 1);

            if (RegularExpression != null)
            {
                if (!RegularExpression.Match(sub).Success)
                {
                    return(ArgumentCheckResult.Failed(this, CheckResult.Fatal, "The arguments structure is wrong (Regular Expression)!"));
                }
            }

            IsSet = true;
            valueList.Add((T)(sub as IConvertible).ToType(typeof(T), null));
            return(ArgumentCheckResult.OK());
            //switch (BaseType)
            //{
            //    case ArgumentBaseType.String:
            //        IsSet = true;
            //        valueList.Add(sub);
            //        return ArgumentCheckResult.OK();
            //    case ArgumentBaseType.Integer:
            //        int i = 0;
            //        if (!Int32.TryParse(sub, out i))
            //            return ArgumentCheckResult.Failed(this, CheckResult.Fatal, "No integer (e. g. 1234)");
            //        valueList.Add(i);
            //        IsSet = true;
            //        return ArgumentCheckResult.OK();
            //    case ArgumentBaseType.Decimal:
            //        double d = 0;
            //        if (!Double.TryParse(sub, out d))
            //            return ArgumentCheckResult.Failed(this, CheckResult.Fatal, "No double (e. g. 1.234)");
            //        valueList.Add(d);
            //        IsSet = true;
            //        return ArgumentCheckResult.OK();
            //    default:
            //        throw new NotImplementedException();
            //}
        }
Exemplo n.º 2
0
        public ArgumentCheckResult Check(string arg)
        {
            if (arg.Equals(SwitchToString() + Switch))
            {
                if (IsSet)
                {
                    return(ArgumentCheckResult.Failed(this, CheckResult.IsAlreadySet, "Is already set"));
                }

                IsSet = true;
                return(ArgumentCheckResult.OK());
            }

            return(ArgumentCheckResult.Failed(this, CheckResult.Wrong, ""));
        }
Exemplo n.º 3
0
        private EvaluationResult EvaluateArgumentsInternal(params String[] args)
        {
            lastError = "";

            HelpArgument.Reset();
            //Reset all argument definitions
            foreach (IArgument defArg in ArgumentList)
            {
                defArg.Reset();
            }

            //Check each argument
            foreach (String arg in args)
            {
                if (HelpArgument.Check(arg).Result == CheckResult.OK)
                {
                    PrintHelpPage(false);
                    return(EvaluationResult.Help); // Go out, no error, but cannot work
                }

                bool find = false;
                foreach (IArgument defArg in ArgumentList)
                {
                    ArgumentCheckResult result = defArg.Check(arg);
                    if ((result.Result == CheckResult.IsAlreadySet) ||
                        (result.Result == CheckResult.Fatal))
                    {
                        lastError = result.GetError();
                        return(EvaluationResult.Failed);
                    }
                    if (result.Result == CheckResult.OK)
                    {
                        find = true;
                        break;
                    }
                }

                if (!AllowUnknownArgs)
                {
                    if (!find)
                    {
                        lastError = "Argument <" + arg + "> is unknown!";
                        return(EvaluationResult.Failed);
                    }
                }
            }

            //Evaluate defined args
            int activeLevel = Int32.MinValue; //For argument order

            foreach (IArgument defArg in ArgumentList)
            {
                //False argument order
                if (defArg.Level < activeLevel)
                {
                    lastError = "False argument order";
                    return(EvaluationResult.Failed);
                }
                activeLevel = defArg.Level;

                //Is not set?
                if (!defArg.IsSet)
                {
                    //And is not optional?
                    if (!defArg.IsOptional)
                    {
                        lastError = "Arguments are missing";
                        return(EvaluationResult.Failed);
                    }
                }
            }

            return(EvaluationResult.OK);
        }
Exemplo n.º 4
0
        public ArgumentCheckResult Check(string arg)
        {
            //Safe the switch(ed content) arguments
            if (arg.StartsWith("-") || arg.StartsWith("+") ||
                arg.StartsWith("/") || arg.StartsWith("--"))
            {
                return(ArgumentCheckResult.Failed(this, CheckResult.Wrong, ""));
            }

            if (RegularExpression != null)
            {
                if (!RegularExpression.Match(arg).Success)
                {
                    return(ArgumentCheckResult.Failed(this, CheckResult.Fatal, "The arguments structure is wrong (Regular Expression)!"));
                }
            }

            if (IsSet)
            {
                if (!IsSeveralTimes)
                {
                    return(ArgumentCheckResult.Failed(this, CheckResult.IsAlreadySet, "Is already set"));
                }
            }

            valueList.Add((T)(arg as IConvertible).ToType(typeof(T), null));
            IsSet = true;
            return(ArgumentCheckResult.OK());

            //switch (BaseType)
            //{
            //    case ArgumentBaseType.String:
            //        if (IsSet)
            //            if (!IsSeveralTimes)
            //                return ArgumentCheckResult.Failed(this, CheckResult.IsAlreadySet, "Is already set");

            //        valueList.Add(arg);
            //        IsSet = true;
            //        return ArgumentCheckResult.OK();
            //    case ArgumentBaseType.Integer:
            //        int i;
            //        if (!Int32.TryParse(arg, out i))
            //            return ArgumentCheckResult.Failed(this, CheckResult.Fatal, "No integer (e. g. 1234)");

            //        if (IsSet)
            //            if (!IsSeveralTimes)
            //                return ArgumentCheckResult.Failed(this, CheckResult.IsAlreadySet, "Is already set");

            //        valueList.Add(i);
            //        IsSet = true;
            //        return ArgumentCheckResult.OK();
            //    case ArgumentBaseType.Decimal:
            //        double d;
            //        if (!Double.TryParse(arg, out d))
            //            return ArgumentCheckResult.Failed(this, CheckResult.Fatal, "No double (e. g. 1.234)");

            //        if (IsSet)
            //            if (!IsSeveralTimes)
            //                return ArgumentCheckResult.Failed(this, CheckResult.IsAlreadySet, "Is already set");

            //        valueList.Add(d);
            //        IsSet = true;
            //        return ArgumentCheckResult.OK();
            //    default:
            //        throw new NotImplementedException();
            //}
        }