Пример #1
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="parsedArguments"></param>
 public ParsedArguments(ParsedArguments parsedArguments)
     : this(parsedArguments.Args)
 {
     RecognizedArguments = parsedArguments.RecognizedArguments;
     ArgumentWithOptions = parsedArguments.ArgumentWithOptions;
     UnRecognizedArguments = parsedArguments.UnRecognizedArguments;
 }
Пример #2
0
 public MergedParsedArguments(ParsedArguments first, ParsedArguments second)
     : base(first.Args)
 {
     _first = first;
     _second = second;
     RecognizedArguments = first.RecognizedArguments.Union(second.RecognizedArguments);
     ArgumentWithOptions = first.ArgumentWithOptions.Union(second.ArgumentWithOptions);
     UnRecognizedArguments = first.UnRecognizedArguments.Intersect(second.UnRecognizedArguments);
 }
        public IEnumerable<object> GetParametersForMethod(MethodInfo method,
                                                          ParsedArguments parsedArguments)
        {
            var parameterInfos = method.GetParameters();
            var parameters = new List<object>();

            foreach (var paramInfo in parameterInfos)
            {
                if (paramInfo.ParameterType.IsClass() && !paramInfo.ParameterType.IsFile())
                {
                    parameters.Add(CreateObjectFromArguments(parsedArguments, paramInfo));
                }
                else
                {
                    var recognizedArgument = parsedArguments.RecognizedArguments.FirstOrDefault(a => a.Matches(paramInfo));
                    parameters.Add(null == recognizedArgument
                                       ? paramInfo.DefaultValue
                                       : ConvertFrom(recognizedArgument, paramInfo.ParameterType));
                }
            }
            return parameters;
        }
Пример #4
0
        public ParsedMethod Parse(MethodInfo methodInfo, ParsedArguments parsedArguments)
        {
            var unMatchedRequiredArguments = parsedArguments.UnMatchedRequiredArguments();
            if (unMatchedRequiredArguments.Any())
            {
                throw new MissingArgumentException("Missing arguments")
                          {
                              Arguments = unMatchedRequiredArguments
                                  .Select(unmatched => new KeyValuePair<string, string>(unmatched.Argument.ToString(), unmatched.Argument.Help())).ToList()
                          };
            }

            var recognizedActionParameters = _turnParametersToArgumentWithOptions.GetParametersForMethod(methodInfo,
                            parsedArguments);

            return new ParsedMethod(parsedArguments)
                       {
                           RecognizedAction = methodInfo,
                           RecognizedActionParameters = recognizedActionParameters,
                           RecognizedClass = Type
                       };
        }
Пример #5
0
 public ParsedArguments ParseArgumentsAndMerge(ParsedArguments parsedArguments, Action<ParsedMethod> action=null)
 {
     var parsedMethod = Parse(parsedArguments.Args);
     if (action != null) action(parsedMethod);
     // Inferred ordinal arguments should not be recognized twice
     parsedArguments.RecognizedArguments = parsedArguments.RecognizedArguments
         .Where(argopts =>
             !parsedMethod.RecognizedArguments.Any(pargopt => pargopt.Index == argopts.Index && argopts.InferredOrdinal));
     var merged = parsedArguments.Merge(parsedMethod);
     if (!IgnoreGlobalUnMatchedParameters)
         merged.AssertFailOnUnMatched();
     return merged;
 }
Пример #6
0
 public ParsedArguments Merge(ParsedArguments args)
 {
     return Merge(this, args);
 }
Пример #7
0
 public static ParsedArguments Merge(ParsedArguments first, ParsedArguments second)
 {
     return new MergedParsedArguments(first, second);
 }
Пример #8
0
 public ParsedMethod(ParsedArguments parsedArguments)
     : base(parsedArguments)
 {
 }
 private object CreateObjectFromArguments(ParsedArguments parsedArguments, ParameterInfo paramInfo)
 {
     var obj = Activator.CreateInstance(paramInfo.ParameterType);
     foreach (
         PropertyInfo prop in
             paramInfo.ParameterType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
     {
         var recognizedArgument = parsedArguments.RecognizedArguments.First(a => a.Matches(prop));
         prop.SetValue(obj, ConvertFrom(recognizedArgument, prop.PropertyType), null);
     }
     return obj;
 }