Esempio n. 1
0
        public static void Startup(IList <string> args)
        {
            Arguments = new SimpleArguments(args);

            // enable trace with v parameter
            if (Arguments.GetFlag("v"))
            {
                Trace.Listeners.Add(new ConsoleTraceListener(true));
            }
        }
        /// <summary>
        /// Read set of parsed SimpleArguments into the properties or fields in a POCO
        /// </summary>
        /// <param name="arguments">SimpleArguments objects with source values</param>
        /// <param name="value"destination POCO to set values in></param>
        public static void ArgumentsToObject(SimpleArguments arguments, object value)
        {
            char[] commaSplit = new char[] { ',' };

            Type t = value.GetType();

            FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.Instance);

            foreach (FieldInfo field in fields)
            {
                MemberInfo m = field;

                try
                {
                    if (field.FieldType == boolType)
                    {
                        field.SetValue(value, arguments.GetFlag(field.Name));
                    }
                    else
                    {
                        string valueString = arguments.GetValue(field.Name);

                        if (valueString != null)
                        {
                            field.SetValue(value, Convert.ChangeType(valueString, field.FieldType));
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new ArgumentException("ERROR: exception occurred whilst parsing argument " + field.Name, ex);
                }
            }

            PropertyInfo[] properties = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo property in properties)
            {
                try
                {
                    if (property.PropertyType == boolType)
                    {
                        // handle a normal bool, if the flag was passed, set it to true, if not, false
                        property.SetValue(value, arguments.GetFlag(property.Name), null);
                    }
                    else if (property.PropertyType.BaseType.FullName == "System.Array")
                    {
                        // handle comma separated arrays
                        string valueString = arguments.GetValue(property.Name);

                        if (valueString != null)
                        {
                            string[] valueStringArray = valueString.Split(commaSplit);

                            int arrayLength = valueStringArray.Length;

                            Type elementType = property.PropertyType.GetElementType();

                            // build the array
                            Array y = Array.CreateInstance(elementType, arrayLength);

                            for (int i = 0; i < arrayLength; i++)
                            {
                                // set an element
                                y.SetValue(Convert.ChangeType(valueStringArray[i], elementType), i);
                            }

                            // put the array into the property
                            property.SetValue(value, y, null);
                        }
                    }
                    else
                    {
                        string valueString = arguments.GetValue(property.Name);

                        if (valueString != null)
                        {
                            property.SetValue(value, Convert.ChangeType(valueString, property.PropertyType), null);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new ArgumentException("ERROR: exception occurred whilst parsing argument " + property.Name, ex);
                }
            }
        }
        /// <summary>
        /// Read a set of command line arguments into the properties or fields in a POCO
        /// </summary>
        /// <param name="args">source list of command line arguments, like the array from Main()</param>
        /// <param name="value">destination POCO to set values in</param>
        public static void ArgumentsToObject(IList <string> args, object value)
        {
            SimpleArguments arguments = new SimpleArguments(args);

            ArgumentsToObject(arguments, value);
        }