private ArgumentHandler ConstructArgumentHandler(MemberInfo info, Argument arg) { ArgumentHandler ai; var memberType = GetMemberType(info); var min = 0; var max = 0; var margs = info.GetCustomAttributes(typeof(MultipleArguments), true) as MultipleArguments[]; if (margs.Length == 1) { min = margs[0].Min; max = margs[0].Max; } if (memberType.IsArray) { ai = new ArrayArgumentHandler(this, info, memberType, min, max); } else if (isIList(memberType)) { ai = new IListArgumentHandler(this, info, memberType, min, max); } else if (memberType == typeof(bool) || memberType == typeof(Boolean) || memberType.IsSubclassOf(typeof(Boolean))) { var bargs = info.GetCustomAttributes(typeof(FlagArgument), true) as FlagArgument[]; ai = new FlagArgumentHandler(this, info, arg.OnCollision, arg.Required, bargs.Length != 0 ? bargs[0].WhenSet : true); } else if (info.GetCustomAttributes(typeof(CountedArgument), true).Length != 0) { ai = new CounterArgumentHandler(this, info, memberType, arg.Required); } else { ai = new PlainArgumentHandler(this, info, memberType, arg.OnCollision, arg.Required); } return ai; }
private ArgumentHandler ConstructArgumentHandler(MemberInfo info, Argument arg) { ArgumentHandler ai; var memberType = GetMemberType(info); var min = 0; var max = 0; var margs = info.GetCustomAttributes(typeof(MultipleArguments), true) as MultipleArguments[]; if (margs.Length == 1) { min = margs[0].Min; max = margs[0].Max; } if (memberType.IsArray) { ai = new ArrayArgumentHandler(this, info, memberType, min, max); } else if (isIList(memberType)) { ai = new IListArgumentHandler(this, info, memberType, min, max); } else if (memberType == typeof(bool) || memberType == typeof(Boolean) || memberType.IsSubclassOf(typeof(Boolean))) { var bargs = info.GetCustomAttributes(typeof(FlagArgument), true) as FlagArgument[]; ai = new FlagArgumentHandler(this, info, arg.OnCollision, arg.Required, bargs.Length != 0 ? bargs[0].WhenSet : true); } else if (info.GetCustomAttributes(typeof(CountedArgument), true).Length != 0) { ai = new CounterArgumentHandler(this, info, memberType, arg.Required); } else { ai = new PlainArgumentHandler(this, info, memberType, arg.OnCollision, arg.Required); } return(ai); }
private void Initialize() { Type me = GetType(); opts = me.GetCustomAttributes(typeof(GetOptOptions), true)[0] as GetOptOptions; BindingFlags flags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance; foreach (MemberInfo[] infoArray in new MemberInfo[][] { me.GetFields(flags), me.GetProperties(flags) }) { foreach (MemberInfo info in infoArray) { Parameters[] paramArgs = info.GetCustomAttributes(typeof(Parameters), true) as Parameters[]; if (paramArgs.Length == 1) { if (parameters != null || (info.MemberType != MemberTypes.Field && info.MemberType != MemberTypes.Property)) { throw new ProgrammingError("Duplicate declaration for parameters"); } Type type = getMemberType(info); if (type.IsArray) { parameters = new ArrayArgumentHandler(this, info, type, paramArgs[0].Min, paramArgs[0].Max); } else if (isIList(type)) { parameters = new IListArgumentHandler(this, info, type, paramArgs[0].Min, paramArgs[0].Max); } else { throw new ProgrammingError("parameters must be an array type or a IList implementation"); } handlers.Add(parameters); continue; } Argument[] args = info.GetCustomAttributes(typeof(Argument), true) as Argument[]; if (args.Length != 1) { continue; } if (opts.AcceptPrefixType == ArgumentPrefixType.None) { throw new ProgrammingError("You used Prefix=None, hence there are no arguments allowed!"); } Argument arg = args[0]; string name = arg.GetArg(); if (String.IsNullOrEmpty(name)) { name = info.Name; } if (opts.CaseType == ArgumentCaseType.Insensitive || opts.CaseType == ArgumentCaseType.OnlyLower) { name = name.ToLower(); } if (longs.ContainsKey(name)) { throw new ProgrammingError(String.Format("Duplicate argument {0}", name)); } ArgumentHandler ai; Type memberType = getMemberType(info); uint min = 0, max = 0; MultipleArguments[] margs = info.GetCustomAttributes(typeof(MultipleArguments), true) as MultipleArguments[]; if (margs.Length == 1) { min = margs[0].Min; max = margs[0].Max; } if (memberType.IsArray) { ai = new ArrayArgumentHandler(this, info, memberType, min, max); } else if (isIList(memberType)) { ai = new IListArgumentHandler(this, info, memberType, min, max); } else { if (memberType == typeof(bool) || memberType == typeof(Boolean) || memberType.IsSubclassOf(typeof(Boolean))) { FlagArgument[] bargs = info.GetCustomAttributes(typeof(FlagArgument), true) as FlagArgument[]; ai = new FlagArgumentHandler(this, info, arg.OnCollision, arg.Required, bargs.Length != 0 ? bargs[0].GetWhenSet() : true); } else if (info.GetCustomAttributes(typeof(Counted), true).Length != 0) { ai = new CounterArgumentHandler(this, info, memberType, arg.Required); } else { ai = new PlainArgumentHandler(this, info, memberType, arg.OnCollision, arg.Required); } } longs.Add(name, ai); handlers.Add(ai); foreach (ArgumentAlias alias in info.GetCustomAttributes(typeof(ArgumentAlias), true)) { string an = alias.GetAlias(); if (opts.CaseType == ArgumentCaseType.Insensitive || opts.CaseType == ArgumentCaseType.OnlyLower) { an = an.ToLower(); } if (longs.ContainsKey(an)) { throw new ProgrammingError(String.Format("Duplicate alias argument {0}", an)); } longs.Add(an, ai); } foreach (ShortArgument sa in info.GetCustomAttributes(typeof(ShortArgument), true)) { char an = sa.GetArg(); if (shorts.ContainsKey(an)) { throw new ProgrammingError(String.Format("Duplicate short argument {0}", an)); } shorts.Add(an, ai); } foreach (ShortArgumentAlias sa in info.GetCustomAttributes(typeof(ShortArgumentAlias), true)) { char an = sa.GetAlias(); if (shorts.ContainsKey(an)) { throw new ProgrammingError(String.Format("Duplicate short argument {0}", an)); } shorts.Add(an, ai); } } } }