private static string ApplyTransform(CommandLineOptions options, string xml)
 {
     XmlWriterSettings settings = new XmlWriterSettings();
     settings.ConformanceLevel = ConformanceLevel.Auto;
     settings.CheckCharacters = false;
     
     StringBuilder sb = new StringBuilder();
     using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
     using (TextWriter writer = new StringWriter(sb))
     {
         XslCompiledTransform xslt = new XslCompiledTransform();
         string xsltTemplate = Path.ChangeExtension(options.Template, "xslt");
         if (!File.Exists(xsltTemplate))
         {
             string localXslt = InputFileLoader.CombinePathFromAppRoot(xsltTemplate);
             if (File.Exists(localXslt))
                 xsltTemplate = localXslt;
         }
         try
         {
             xslt.Load(xsltTemplate);
         }
         catch (Exception ex)
         {
             throw new InvalidOperationException("Unable to load tranform: " + options.Template, ex);
         }
         options.XsltOptions.RemoveParam("defaultNamespace", "");
         if (options.DefaultNamespace != null)
         {
             options.XsltOptions.AddParam("defaultNamespace", "", options.DefaultNamespace);
         }
         xslt.Transform(reader, options.XsltOptions, writer);
     }
     return sb.ToString();
 }
        public static CommandLineOptions Parse(TextWriter messageOutput, params string[] args)
        {
            CommandLineOptions options = new CommandLineOptions(messageOutput);

            string key, value;
            for (int i = 0; i < args.Length; i++)
            {
                string arg = args[i].Trim();
                if (arg.StartsWith("-o:"))
                {
                    if (!string.IsNullOrEmpty(options.OutPath)) options.ShowHelp = true;
                    options.OutPath = arg.Substring(3).Trim();
                }
                else if (arg.StartsWith("-p:"))
                {
                    Split(arg.Substring(3), out key, out value);
                    options.XsltOptions.AddParam(key, "", value ?? "true");
                }
                else if (arg.StartsWith("-t:"))
                {
                    options.Template = arg.Substring(3).Trim();
                }
                else if (arg.StartsWith("-ns:"))
                {
                    options.DefaultNamespace = arg.Substring(4).Trim();
                }
                else if (arg == "/?" || arg == "-h")
                {
                    options.ShowHelp = true;
                }
                else if (arg == "-q") // quiet
                {
                    options.ShowLogo = false;
                }
                else if (arg == "-d")
                {
                    options.Arguments.Add("--include_imports");
                }
                else if (arg.StartsWith("-i:"))
                {
                    options.InPaths.Add(arg.Substring(3).Trim());
                }
                else if (arg == "-writeErrors")
                {
                    options.WriteErrorsToFile = true;
                }
                else if (arg.StartsWith("-w:"))
                {
                    options.WorkingDirectory = arg.Substring(3).Trim();
                }
                else if(arg.StartsWith("-s:"))
                {
                    options.Arguments.Add(string.Format("--proto_path={0}", arg.Substring(3).Trim()));
                }
                else
                {
                    options.ShowHelp = true;
                }
            }
            if (options.InPaths.Count == 0)
            {
                options.ShowHelp = (string) options.XsltOptions.GetParam("help", "") != "true";
            }
            return options;

        }
        private static string LoadFilesAsXml(CommandLineOptions options)
        {
            FileDescriptorSet set = new FileDescriptorSet();

            foreach (string inPath in options.InPaths)
            {
                InputFileLoader.Merge(set, inPath, options.ErrorWriter, options.Arguments.ToArray());
            }

            XmlSerializer xser = new XmlSerializer(typeof(FileDescriptorSet));
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = "  ";
            settings.NewLineHandling = NewLineHandling.Entitize;
            StringBuilder sb = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(sb, settings))
            {
                xser.Serialize(writer, set);
            }
            return sb.ToString();
        }