示例#1
0
        public CommandDispatcher(Spook cli)
        {
            _cli = cli;
            RegisterCommandHandler <string>((args, canConsumeAll) =>
            {
                return(CommandToken.ReadString(args, canConsumeAll));
            });

            RegisterCommandHandler <string[]>((args, canConsumeAll) =>
            {
                if (canConsumeAll)
                {
                    var ret = CommandToken.ToString(args);
                    args.Clear();
                    return(ret.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries));
                }
                else
                {
                    return(CommandToken.ReadString(args, false).Split(',', ' '));
                }
            });

            RegisterCommandHandler <string, byte>(false, (str) => byte.Parse(str));
            RegisterCommandHandler <string, bool>(false, (str) => str == "1" || str == "yes" || str == "y" || bool.Parse(str));
            RegisterCommandHandler <string, ushort>(false, (str) => ushort.Parse(str));
            RegisterCommandHandler <string, uint>(false, (str) => uint.Parse(str));

            RegisterCommandHandler <string, JObject>((str) => JObject.Parse(str));
            //RegisterCommandHandler<JObject, JArray>((obj) => (JArray)obj);

            RegisterCommand(this);
        }
示例#2
0
        private bool TryProcessValue(Type parameterType, List <CommandToken> args, bool canConsumeAll, out object value)
        {
            if (args.Count > 0)
            {
                if (_handlers.TryGetValue(parameterType, out var handler))
                {
                    value = handler(args, canConsumeAll);
                    return(true);
                }

                if (parameterType.IsEnum)
                {
                    var arg = CommandToken.ReadString(args, canConsumeAll);
                    value = Enum.Parse(parameterType, arg.Trim(), true);
                    return(true);
                }
            }

            value = null;
            return(false);
        }