예제 #1
0
        private void PushOption(OptionMember member, ref Dictionary <string, OptionMember> longDict, ref Dictionary <char, OptionMember> shortDict)
        {
            string longName;

            if (string.IsNullOrEmpty(member.option.longName))
            {
                longName = member.name;
            }
            else
            {
                longName = member.option.longName;
            }

            longDict.Add(longName, member);

            if (member.option.shortName != '\0')
            {
                shortDict.Add(member.option.shortName, member);
            }
        }
예제 #2
0
        public void Parse(string[] args, Type type, ref object obj)
        {
            var longDict  = new Dictionary <string, OptionMember>();
            var shortDict = new Dictionary <char, OptionMember>();

            var arguments = GetArguments(args);

            GetOptionMemeberDictionary(type, ref longDict, ref shortDict);

            for (int i = 0; i < arguments.Count; i++)
            {
                var argument = arguments[i];

                OptionMember member = null;

                if (argument.key.Length == 1) //只有一个字符,猜测为短参数
                {
                    char key = argument.key[0];

                    shortDict.TryGetValue(key, out member);
                }

                if (member == null)
                {
                    longDict.TryGetValue(argument.key, out member);
                }

                if (member != null)
                {
                    if (member.option.ignoreEmpty && string.IsNullOrEmpty(argument.value))
                    {
                        //忽略空参数
                        continue;
                    }

                    member.SetValue(obj, argument.value);
                }
            }
        }