Ejemplo n.º 1
0
        public static void GenerateHelp(Type argsType, TextWriter writer)
        {
            writer.WriteLine("Usage:");
            var typeInfo = new CommandLineArgumentsParserTypeInfo(argsType);

            if (typeInfo.DefaultProperty != null)
            {
                writer.Write("[{0}]", typeInfo.DefaultProperty.Property.Name);
            }
            foreach (var prop in typeInfo.Properties)
            {
                if (prop == typeInfo.DefaultProperty)
                {
                    continue;
                }
                writer.Write(" ");
                if (prop.SyntaxAttribute != null && prop.SyntaxAttribute.Format != null)
                {
                    if (prop.SyntaxAttribute.Format.Contains("{0}"))
                    {
                        writer.Write(prop.SyntaxAttribute.Format, prop.Property.Name);
                    }
                    else
                    {
                        writer.Write("[{0} ({1})]", prop.SyntaxAttribute.Format, prop.Property.Name);
                    }
                }
            }
            writer.WriteLine();
        }
Ejemplo n.º 2
0
        public static string Generate(object target)
        {
            var sb       = new StringBuilder();
            var typeInfo = new CommandLineArgumentsParserTypeInfo(target.GetType());

            if (typeInfo.DefaultProperty != null)
            {
                var value = typeInfo.DefaultProperty.Property.GetValue(target, null);
                sb.AppendFormat("\"{0}\" ", value);
            }
            foreach (var prop in typeInfo.Properties)
            {
                if (prop == typeInfo.DefaultProperty || prop.SyntaxFormat.IsNullOrEmpty())
                {
                    continue;
                }
                var value = prop.Property.GetValue(target, null);
                if (value != null)
                {
                    if (!prop.SyntaxFormatHasParameter && value is bool)
                    {
                        if ((bool)value == true)
                        {
                            sb.AppendFormat(prop.SyntaxFormat);
                            sb.Append(" ");
                        }
                    }
                    else
                    {
                        string sValue;
                        if (prop.NeedsQuote)
                        {
                            sValue = String.Format("\"{0}\"", value);
                        }
                        else
                        {
                            sValue = String.Format("{0}", value);
                        }
                        sb.AppendFormat(prop.SyntaxFormat, sValue);
                        sb.Append(" ");
                    }
                }
            }
            return(sb.ToString());
        }
Ejemplo n.º 3
0
 internal ToolArgsParser(object target)
 {
     Target         = target;
     TargetTypeInfo = new CommandLineArgumentsParserTypeInfo(Target.GetType());
 }