public virtual void BindModel(TModel model, IEnumerable <string> args)
        {
            var unnamedArgs = args.TakeWhile(s => IsSwitch(s) == false);

            EnsureCorrectNumberOfOrdinalArguments(unnamedArgs);

            for (var i = 0; i < OrdinalArguments.Count; i++)
            {
                MemberBindingDefinition <TModel> member = (MemberBindingDefinition <TModel>)GetMemberBindingDefinition(OrdinalArguments[i]);

                if (member == null)
                {
                    throw new BindingDefinitionException(String.Format("Member {0} in {1} does not have a BindingDefinition defined.", OrdinalArguments[i].Name, typeof(TModel).FullName));
                }

                if (OrdinalArguments.Count != i + 1)
                {
                    HandleInputs(new[] { unnamedArgs.Skip(i).First() }, model, member);
                }
                else
                {
                    //last ordinal, send remaining ordinal arguments
                    HandleInputs(unnamedArgs.Skip(i), model, member);
                }
            }

            var switchedArguments = args.Skip(unnamedArgs.Count());
            var namedMembers      = Members.Where(m => OrdinalArguments.Contains(m.Key) == false);

            foreach (var member in namedMembers)
            {
                //find a suitable switch
                var argument = switchedArguments.SkipWhile(a => a.StartsWith(SwitchDelimiter) == false || a.Length <= SwitchDelimiter.Length || member.Value.CanHandleSwitch(a.Substring(SwitchDelimiter.Length)) == false);

                if (argument.Any())
                {
                    //take all arguments until the next switch is found, skipping the first, which is the switch itself
                    var argumentValue = argument.Skip(1).TakeWhile(a => a.StartsWith(SwitchDelimiter) == false);

                    HandleInputs(argumentValue, model, member.Value);
                }
                else
                {
                    if (member.Value.Required == true)
                    {
                        throw new InvalidOperationException(String.Format(Properties.Resources.RequiredParameterNotProvidedMessage, member.Value.MemberInfo.Name));
                    }

                    //switch not provided
                    if (member.Value.DefaultValue != null)
                    {
                        member.Key.SetValue(model, member.Value.DefaultValue);
                    }
                }
            }
        }
Example #2
0
        public virtual void BindModel(TModel model, IEnumerable <string> args)
        {
            var unnamedArgs = args.TakeWhile(s => IsSwitch(s) == false);

            if (unnamedArgs.Count() != OrdinalArguments.Count)
            {
                throw new InvalidArgsFormatException(String.Format("Model requires exactly {0} arguments before the first switch; {1} was/were provided", OrdinalArguments.Count, unnamedArgs.Count()));
            }

            for (var i = 0; i < OrdinalArguments.Count; i++)
            {
                MemberBindingDefinition <TModel> member;

                if (Members.TryGetValue(OrdinalArguments[i], out member) == false)
                {
                    throw new BindingDefinitionException(String.Format("Member {0} in {1} does not have a BindingDefinition defined.", OrdinalArguments[i].Name, typeof(TModel).FullName));
                }


                HandleInputs(new[] { unnamedArgs.Skip(i).First() }, model, member);
            }

            var switchedArguments = args.Skip(unnamedArgs.Count());
            var namedMembers      = Members.Where(m => OrdinalArguments.Contains(m.Key) == false);

            foreach (var member in namedMembers)
            {
                //find a suitable switch, then skip 1 (to skip the switch itself)
                var argument = switchedArguments.SkipWhile(a => a.Length <= SwitchDelimiter.Length || member.Value.CanHandleSwitch(a.Substring(SwitchDelimiter.Length)) == false).Skip(1);

                if (argument.Any())
                {
                    //take all arguments until the next switch is found
                    var argumentValue = argument.TakeWhile(a => a.StartsWith(SwitchDelimiter) == false);

                    HandleInputs(argumentValue, model, member.Value);
                }
                else
                {
                    //switch not provided
                    if (member.Value.DefaultValue != null)
                    {
                        member.Key.SetValue(model, member.Value.DefaultValue);
                    }
                }
            }
        }