Exemplo n.º 1
0
        public object Parse()
        {
            FieldInfo[] fields         = argumentsType.GetFields();
            object      argumentObject = Activator.CreateInstance(argumentsType);

            foreach (FieldInfo field in fields)
            {
                object[] attributes = field.GetCustomAttributes(typeof(CommandLineAttribute), true);
                if (attributes.Length > 0)
                {
                    object attrObject         = attributes[0];
                    CommandLineAttribute attr = (CommandLineAttribute)attrObject;
                    object argumentValue      = GetArgument(attrObject, attr.Name, true);
                    if (argumentValue != null)
                    {
                        SetValue(field, argumentObject, argumentValue);
                    }
                    else if (!attr.Optional)
                    {
                        throw new CommandLineArgumentException(string.Format("Expected argument '{0}'", attr.Name));
                    }
                }
            }
            return(argumentObject);
        }
Exemplo n.º 2
0
        private string GetUsage(CommandLineAttribute attr)
        {
            string usage = " ";

            if (attr is CommandLineValueAttribute)
            {
                CommandLineValueAttribute cattr = (CommandLineValueAttribute)attr;
                if (cattr.Place != -1)
                {
                    return(" <" + attr.Name + ">");
                }
            }
            if (attr.Optional)
            {
                usage += "[";
            }
            usage += "/" + attr.Name;
            if (attr.AlternateName != null)
            {
                usage += "|/" + attr.AlternateName;
            }
            if (attr is CommandLineValueAttribute)
            {
                usage += " <value>";
            }
            if (attr.Optional)
            {
                usage += "]";
            }
            return(usage);
        }
Exemplo n.º 3
0
        private object GetArgument(object attrObject, string name, bool alternate)
        {
            object argumentValue      = null;
            CommandLineAttribute attr = (CommandLineAttribute)attrObject;

            if (attrObject is CommandLineValueAttribute)
            {
                CommandLineValueAttribute cattr = (CommandLineValueAttribute)attr;
                if (cattr.Place != -1)
                {
                    if (cattr.Place <= arguments.Length && !arguments[cattr.Place - 1].StartsWith("/") && !arguments[cattr.Place - 1].StartsWith("-"))
                    {
                        return(arguments[cattr.Place - 1]);
                    }
                    else
                    {
                        if (!cattr.Optional)
                        {
                            throw new CommandLineArgumentException("Missing parameters");
                        }
                    }
                }
                else
                {
                    argumentValue = GetArgumentValue(name);
                }
            }
            else
            {
                int index = GetArgumentIndex(name);
                if (attrObject is CommandLineFlagAttribute && index != -1)
                {
                    argumentValue = !arguments[index].EndsWith("-");
                }
            }
            if (argumentValue == null && alternate && attr.AlternateName != null)
            {
                return(GetArgument(attrObject, attr.AlternateName, false));
            }
            else
            {
                return(argumentValue);
            }
        }
Exemplo n.º 4
0
        private string GetDescription(CommandLineAttribute attr)
        {
            string description = "\r\n";

            if (attr is CommandLineValueAttribute)
            {
                CommandLineValueAttribute cattr = (CommandLineValueAttribute)attr;
                if (cattr.Place != -1)
                {
                    description = "\r\n<" + attr.Name + ">";
                    return(description.PadRight(30) + attr.Description);
                }
            }
            description += "/" + attr.Name;
            if (attr is CommandLineValueAttribute)
            {
                description += " <value>";
            }
            description  = description.PadRight(30);
            description += attr.Description;
            return(description);
        }
Exemplo n.º 5
0
 private string GetUsage(CommandLineAttribute attr)
 {
     string usage = " ";
     if (attr is CommandLineValueAttribute)
     {
         CommandLineValueAttribute cattr = (CommandLineValueAttribute) attr;
         if (cattr.Place != -1)
             return " <" + attr.Name + ">";
     }
     if (attr.Optional)
         usage += "[";
     usage += "/" + attr.Name;
     if (attr.AlternateName != null)
         usage += "|/" + attr.AlternateName;
     if (attr is CommandLineValueAttribute)
         usage += " <value>";
     if (attr.Optional)
         usage += "]";
     return usage;
 }
Exemplo n.º 6
0
 private string GetDescription(CommandLineAttribute attr)
 {
     string description = "\r\n";
     if (attr is CommandLineValueAttribute)
     {
         CommandLineValueAttribute cattr = (CommandLineValueAttribute) attr;
         if (cattr.Place != -1)
         {
             description = "\r\n<" + attr.Name + ">";
             return description.PadRight(30) + attr.Description;
         }
     }
     description += "/" + attr.Name;
     if (attr is CommandLineValueAttribute)
         description += " <value>";
     description = description.PadRight(30);
     description += attr.Description;
     return description;
 }