コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="out_type">The type that will be parsed</param>
        /// <param name="question">The question that will be presented to the console</param>
        /// <param name="default">The default value if input is empty</param>
        /// <param name="default_text">The text that explains the default object</param>
        /// <param name="validate">The validator object</param>
        /// <returns></returns>
        public static object AskInput(Type @out_type, string question, object @default = null, string @default_text = null, Func <object, bool> validate = null)
        {
_reprint:
            Console.Write("[INPUT] ", Color.DarkGray);
            Console.Write(question, Color.White);
            Console.WriteLine($"{(question.Last() == ' ' ? "" : " ")}(default:[{@default_text ?? "Null"}])", Color.Aqua);

            Console.Write(">", Color.DarkGray);
            Console.ResetColor();
            var input = Console.ReadLine()?.ExpandEscaped() ?? "";

            if (string.IsNullOrEmpty(input))
            {
                if (validate == null)
                {
                    return(@default);
                }
                if (validate(@default))
                {
                    return(@default);
                }
                else
                {
                    Error("Invalid Input and Default Value.");
                    goto _reprint;
                }
            }

            var @out = MapToType(input, @out_type);

            if (@out is Exception && (@out as Exception).Message == "__ERROR")
            {
                //invalid cast!
                Consoler.Error($"Failed casting to '{out_type.Name}' type.");
                goto _reprint;
            }

            if (validate != null)
            {
                if (!validate(@out))
                {
                    Error("Invalid Input");
                    goto _reprint;
                }
            }
            return(@out);
        }
コード例 #2
0
        public static Exception CaptureCommand(bool cls = false, bool print = false)
        {
            if (cls)
            {
                Console.Clear();
            }
            if (print)
            {
                PrintMethods();
            }
            goto _skipbeep;

_retry:
            SystemSounds.Beep.Play();
_skipbeep:
            Console.Write(">", Color.DarkGray);
            Console.ResetColor();
            var l = ReadLine().Trim();

            if (string.IsNullOrEmpty(l))
            {
                goto _retry;
            }

            var splet = l.Split("\t", StringSplitOptions.RemoveEmptyEntries);

            var commandid = splet[0];

            if (commandid.IsNumeric() == false)
            {
                goto _retry;
            }
            var        args = splet.Skip(1).ToArray();
            MethodInfo act;

            try {
                act = Actions[Convert.ToInt32(commandid)];
            } catch {
                goto _retry;
            }
            List <object> collected = null;
            var           @params   = act.GetParameters();

            if (@params.Length != args.Length || @params.All(p => p.ParameterType == typeof(string)) == false)
            {
                //check if has ConsoleParam
                if (@params.All(p => p.GetCustomAttributes(typeof(ConsoleParamAttribute), false).Any()) == false) //validate that all params has this option
                {
                    goto _retry;
                }
                collected = new List <object>();
                foreach (var para in @params)
                {
                    var attr = para.GetCustomAttributes(typeof(ConsoleParamAttribute), false).FirstOrDefault() as ConsoleParamAttribute;
                    if (attr == null)
                    {
                        goto _retry;
                    }

                    var val = attr.Validator == null ? (Func <object, bool>)null : attr.Validator.Validate;
                    if (para.ParameterType.IsEnum)
                    {
                        collected.Add(Consoler.AskEnum(para.ParameterType, attr.Question, (Enum)attr.Default, val));
                    }
                    else
                    {
                        collected.Add(Consoler.AskInput(para.ParameterType, attr.Question, attr.Default, attr.Default?.ToString(), val));
                    }
                }
                if (@params.Length != collected.Count)
                {
                    Consoler.Error("Something wen't wrong during parameters collecting.");
                    goto _retry;
                }
            }

            try {
                var ret = act.Invoke(null, collected?.ToArray() ?? args.Cast <object>().ToArray());
                if (ret != null)
                {
                    Console.WriteLine(ret, Color.CornflowerBlue);
                }
            } catch (Exception e) {
                return(e);
            }
            return(null);
        }