예제 #1
0
        public static string[] GetHelpText()
        {
            T obj = new T();

            List <string> helpLines = new List <string>();

            Dictionary <string, PropArg> propArgs = PropArgMatcher <T> .MatchPropertirsAndArguments(obj);

            foreach (var propArg in propArgs)
            {
                string line = string.Empty;

                if (!propArg.Value.Argument.Required)
                {
                    line += "[";
                }

                line += "-" + propArg.Key + " <";

                if (propArg.Value.Property.PropertyType == typeof(int))
                {
                    line += "int";
                }
                else if (propArg.Value.Property.PropertyType == typeof(float))
                {
                    line += "float";
                }
                else if (propArg.Value.Property.PropertyType == typeof(decimal))
                {
                    line += "decimal";
                }
                else if (propArg.Value.Property.PropertyType == typeof(string))
                {
                    line += "string";
                }
                else if (propArg.Value.Property.PropertyType == typeof(bool))
                {
                    line += "bool";
                }

                line += ">";

                if (propArg.Value.Property.GetValue(obj) != null)
                {
                    line += " (" + propArg.Value.Property.GetValue(obj).ToString() + ")";
                }

                if (!propArg.Value.Argument.Required)
                {
                    line += "]";
                }

                line = line.PadRight(40);

                line += propArg.Value.Argument.Description;

                helpLines.Add(line);
            }

            return(helpLines.ToArray());
        }
예제 #2
0
        public static T Parse(string[] args, string argumentNameSwitch = ArgumentNameSwitch)
        {
            var obj = new T();

            Dictionary <string, PropArg> propArgs = PropArgMatcher <T> .MatchPropertirsAndArguments(obj);

            int requiredCount = propArgs.Count(propArg => propArg.Value.Argument.Required);

            int foundCounter    = 0;
            int requiredCounter = 0;

            List <string> errors = new List <string>();

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].StartsWith(argumentNameSwitch)) // named argument
                {
                    string propName = args[i].Substring(1);

                    if (propArgs.ContainsKey(propName))
                    {
                        var prop = propArgs[propName].Property;

                        if (prop.PropertyType != typeof(bool) && i < args.Length - 1)
                        {
                            string argVal = args[i + 1];

                            if (SetValue(prop, obj, argVal, ref requiredCounter))
                            {
                                foundCounter++;
                                i++;
                                continue;
                            }
                            else
                            {
                                errors.Add($"Error assigning value of {argVal} to property {propName}");
                            }
                        }
                        else if (prop.PropertyType == typeof(bool))
                        {
                            // peek next entry
                            string val = (i < args.Length - 1 && !args[i + 1].StartsWith(argumentNameSwitch)) ? args[i + 1] : "true";
                            if (SetValue(prop, obj, val, ref requiredCounter))
                            {
                                foundCounter++;
                                if (i < args.Length - 1 && !args[i + 1].StartsWith(argumentNameSwitch))
                                {
                                    i++;
                                }
                            }
                            else
                            {
                                errors.Add($"Error assigning value of {val} to property {propName}");
                            }

                            continue;
                        }
                    }
                    else
                    {
                        errors.Add($"Unknown named property {args[i].Substring(1)}");
                    }
                }
                else // position argument
                {
                    var prop = propArgs.ElementAt(foundCounter).Value.Property;

                    if (SetValue(prop, obj, args[i], ref requiredCounter))
                    {
                        foundCounter++;
                    }
                    else
                    {
                        errors.Add($"Error assigning value of {args[i]} to property {args[i].Substring(1)}");
                    }
                }
            }

            if (requiredCounter != requiredCount)
            {
                throw new Exception("Error parsing argument: not all required arguments found.");
            }

            if (errors.Count() > 0)
            {
                throw new Exception("Error parsing arguments: not all properties could be set\n" + string.Join("\n", errors));
            }

            return(obj);
        }