コード例 #1
0
ファイル: HelpBuilder.cs プロジェクト: ruibm/Danio
        private static string GetArgInstanceUsage(int maxCharactersPerLine, ArgInstance instance)
        {
            StringBuilder builder = new StringBuilder();
            List<string> names = new List<string>();
            if (instance.ShortName != null)
            {
                builder.AppendFormat("  -" + instance.ShortName + ", ");
            }
            else
            {
                builder.AppendFormat("      ");
            }

            if (instance.Name != null)
            {
                names.Add("--" + instance.Name);
            }

            // No need to print the FullName as all variables must have a Name.
            // It would also make the usage string quite verbose.
            builder.AppendFormat(
                "{0, -" + HelpStringStartIndex.ToString() + "} {1}",
                string.Join(", ", names),
                instance.Arg.Help);

            if (instance.Arg.AllowedValues != null)
            {
                builder.AppendFormat(" (AllowedValues=[{0}])", string.Join(", ", instance.Arg.AllowedValues));
            }
            else if (instance.Field.FieldType.Equals(typeof(bool)))
            {
                builder.Append(" (AllowedValues=[True, False])");
            }
            else if (instance.Field.FieldType.IsEnum)
            {
                Array enumValues = Enum.GetValues(instance.Field.FieldType);
                List<string> stringValues = new List<string>();
                for (int i = 0; i < enumValues.Length; ++i)
                {
                    stringValues.Add(enumValues.GetValue(i).ToString());
                }
                builder.AppendFormat(" (AllowedValues=[{0}])", string.Join(", ", stringValues));
            }

            if (instance.DefaultValue != null)
            {
                if (instance.DefaultValue.GetType().IsArray)
                {
                    builder.AppendFormat(" (DefaultValue=[{0}])", string.Join(", ", ((Array)instance.DefaultValue).Cast<object>()));
                }
                else
                {
                    builder.AppendFormat(" (DefaultValue=[{0}])", instance.DefaultValue);
                }
            }
            return GetInMultipleLines(builder, maxCharactersPerLine); ;
        }
コード例 #2
0
ファイル: ArgAssignerTest.cs プロジェクト: ruibm/Danio
 public FindResult GetFindResult(params string[] mandatoryFields)
 {
     HashSet<string> mandatoryFieldsSet = new HashSet<string>(mandatoryFields);
     List<ArgInstance> argInstances = new List<ArgInstance>();
     foreach (FieldInfo fieldInfo in GetType().GetFields())
     {
         ArgAttribute attribute = new ArgAttribute("helpmessage");
         if (mandatoryFieldsSet.Contains(fieldInfo.Name))
         {
             attribute.IsMandatory = true;
         }
         ArgInstance argInstance = new ArgInstance(attribute, fieldInfo, GetType(), this);
         argInstances.Add(argInstance);
     }
     return new FindResult(argInstances, ExecutionLog.CreateErrorLog(), ExecutionLog.CreateWarningLog());
 }
コード例 #3
0
ファイル: ArgFinder.cs プロジェクト: ruibm/Danio
        public FindResult FindAllArgs(Assembly rootAssembly)
        {
            Stack<Assembly> assembliesToScan = new Stack<Assembly>();
            HashSet<string> scannedAssemblies = new HashSet<string>();
            List<ArgInstance> args = new List<ArgInstance>();
            ExecutionLog errorLog = ExecutionLog.CreateErrorLog();
            ExecutionLog warningLog = ExecutionLog.CreateWarningLog();

            assembliesToScan.Push(rootAssembly);
            while (assembliesToScan.Count > 0)
            {
                Assembly currentAssembly = assembliesToScan.Pop();
                scannedAssemblies.Add(currentAssembly.FullName);
                Log.Info("Scanning Assembly [{0}].", currentAssembly.FullName);

                foreach (AssemblyName childName in currentAssembly.GetReferencedAssemblies())
                {
                    try
                    {
                        Assembly child = Assembly.Load(childName);
                        if (!scannedAssemblies.Contains(child.FullName))
                        {
                            assembliesToScan.Push(child);
                        }
                    }
                    catch (FileLoadException e)
                    {
                        warningLog.Add("Unable to load DLL [{0}] with exception: [{1}]", childName, e);
                    }
                    catch (FileNotFoundException e)
                    {
                        warningLog.Add("Could not find DLL [{0}] with exception: [{1}]", childName, e);
                    }
                    catch (BadImageFormatException e)
                    {
                        warningLog.Add("Unable to load DLL [{0}] with exception: [{1}]", childName, e);
                    }
                }

                Type[] currentAssemblyTypes = null;
                try
                {
                    currentAssemblyTypes = currentAssembly.GetTypes();
                }
                catch (ReflectionTypeLoadException e)
                {
                    warningLog.Add("Unable to retrieve types for DLL [{0}] with exception: [{1}]", currentAssembly.FullName, e);
                    continue;
                }
                Assertions.IsNotNull(currentAssemblyTypes, "currentAssemblyTypes cannot be null.");

                foreach (Type type in currentAssemblyTypes)
                {
                    foreach (FieldInfo field in type.GetFields(FieldBindingFlags))
                    {
                        ArgAttribute arg = field.GetCustomAttribute<ArgAttribute>();
                        if (arg != null)
                        {
                            object defaultValue = field.GetValue(type);
                            ArgInstance argInstance = new ArgInstance(arg, field, type, type, defaultValue);
                            args.Add(argInstance);
                            if (argInstance.Arg.AllowedValues != null &&
                                argInstance.Arg.AllowedValues.GetType().Equals(field.FieldType))
                            {
                                errorLog.Add(
                                    "The Type [{0}] of AllowedValues for Arg [{1}] does not match its original Type [{2}].",
                                    argInstance.Arg.AllowedValues.GetType().Name,
                                    argInstance.FullName,
                                    field.FieldType.Name);
                            }
                        }
                    }
                }
            }

            args = ClearDuplicates(args);
            return new FindResult(args, errorLog, warningLog);
        }
コード例 #4
0
ファイル: ArgAssigner.cs プロジェクト: ruibm/Danio
 private void SetArgInstanceValueOrAddError(ArgInstance instance, List<string> values, ExecutionLog errorLog)
 {
     Type fieldType = instance.Field.FieldType;
     if (fieldType.IsArray)
     {
         instance.Field.SetValue(instance.ParentObject, CreateFieldValueForArray(fieldType, values));
     }
     else
     {
         if (values.Count == 1)
         {
             instance.Field.SetValue(instance.ParentObject, CreateFieldValue(fieldType, values[0]));
         }
         else
         {
             errorLog.Add("Multiple values passed for argument [{0}]. Values are [{1}].",
                 instance.FullName,
                 string.Join(", ", values));
         }
     }
 }