Example #1
0
 /// <summary>
 ///     Returns the next arg
 /// </summary>
 /// <param name="request">The request.</param>
 /// <returns>The next arg</returns>
 /// <exception cref="ArgumentOutOfRangeException">
 ///     Max - Cannot get next arg because it is outside the range of consumable
 ///     values
 /// </exception>
 public static string Next(this ConsumptionRequest request)
 {
     if (request.Max < 2)
     {
         throw new ArgumentOutOfRangeException(nameof(request.Max),
                                               "Cannot get next arg because it is outside the range of consumable values");
     }
     return(request.Info.Args[request.Info.Index + 1]);
 }
Example #2
0
        /// <summary>
        ///     Gets the last arg allowed to be consumed
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>The last arg allowed to be consumed</returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static string Last(this ConsumptionRequest request)
        {
            var from = request.AllToBeConsumed().ToList();

            if (!from.Any())
            {
                throw new ArgumentOutOfRangeException();
            }
            return(from.Last());
        }
Example #3
0
 /// <summary>
 ///     Consumes the specified instance.
 /// </summary>
 /// <param name="instance">The instance.</param>
 /// <param name="request">The request.</param>
 /// <returns>ConsumptionResult.</returns>
 /// <exception cref="MissingValueException"></exception>
 public virtual ConsumptionResult Consume(object instance, ConsumptionRequest request)
 {
     try
     {
         if (request.Max < MinRequired)
         {
             throw new MissingValueException(Parser,
                                             $"Switch {this} expected to have at least {MinRequired} values, but was told it can only have {request.Max}. Are you sure you passed enough values to satisfy the switch?");
         }
         HasBeenConsumed = true;
         var values = request.AllToBeConsumed().Take(MaxAllowed).ToArray();
         ConsumeCallback(instance, values);
         return(new ConsumptionResult(request.Info, values.Length, this));
     }
     catch (ParseException pe)
     {
         return(new ConsumptionResult(pe));
     }
 }
Example #4
0
 /// <summary>
 ///     Gets the rest of the args allowed to be consumed
 /// </summary>
 /// <param name="request">The request.</param>
 /// <returns>The rest of the args allowed to be consumed</returns>
 public static IEnumerable <string> Rest(this ConsumptionRequest request) =>
 request.Info.Args.Skip(request.Info.Index + 1).Take(request.Max - 1);
Example #5
0
 /// <summary>
 ///     Determines whether the specified request has a second argument.
 /// </summary>
 /// <param name="request">The request.</param>
 /// <returns><c>true</c> if the specified request has a second argument; otherwise, <c>false</c>.</returns>
 public static bool HasNext(this ConsumptionRequest request) =>
 request.Info.Index + 1 < request.Info.Index + request.Max;
Example #6
0
 /// <summary>
 ///     Returns all args to be consumed
 /// </summary>
 /// <param name="request">The request.</param>
 /// <returns>Args to be consumed</returns>
 public static IEnumerable <string> AllToBeConsumed(this ConsumptionRequest request) =>
 request.Info.Args.Skip(request.Info.Index).Take(request.Max);
Example #7
0
        /// <summary>
        ///     Consumes the args and populate the provided instance
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="request">The request.</param>
        /// <returns>ConsumptionResult.</returns>
        public virtual ConsumptionResult Consume(object instance, ConsumptionRequest request)
        {
            var parameter = Parameters.First(x => x.CanConsume(instance, request.Info).Info != request.Info);

            return(parameter.Consume(instance, request));
        }