Exemple #1
0
        /// <summary>
        /// Parses the command line arguments.
        /// </summary>
        /// <param name="args">The args.</param>
        public void ParseArguments(string[] args)
        {
            var showHelp = false;

            var files = new List <string>();

            var               configParams          = new List <ConfigParam>();
            var               styleParams           = new List <ConfigParam>();
            string            webDocumentationUrl   = null;
            NetworkCredential webDocumentationLogin = null;

            var additionalSearchDirectories = new List <string>();

            var options = new OptionSet()
            {
                "Copyright (c) 2010-2013 SharpDoc - Alexandre Mutel",
                "Usage: SharpDoc [options]* [--config file.xml | Assembly1.dll Assembly1.xml...]*",
                "Documentation generator for .Net languages",
                "",
                "options:",
                { "c|config=", "Configuration file", opt => Config = Config.Load(opt, TopicLoader) },

                {
                    "D=", "Define a template parameter with an (optional) value.",
                    (param, value) =>
                    {
                        if (param == null)
                        {
                            throw new OptionException("Missing parameter name for option -D.", "-D");
                        }
                        configParams.Add(new ConfigParam(param, value));
                    }
                },
                {
                    "S=", "Define a style parameter with a (optional) value.",
                    (style, value) =>
                    {
                        if (style == null)
                        {
                            throw new OptionException("Missing parameter name/value for option -S.", "-S");
                        }
                        styleParams.Add(new ConfigParam(style, value));
                    }
                },
                { "d|style-dir=", "Add a style directory", opt => Config.StyleDirectories.Add(opt) },
                { "s|style=", "Specify the style to use [default: Standard]", opt => Config.StyleNames.Add(opt) },
                { "o|output=", "Specify the output directory [default: Output]", opt => Config.OutputDirectory = opt },
                { "r|searchdir=", "Add search directory in order to load source assemblies", additionalSearchDirectories.Add },
                { "w|webdoc=", "Url of the extern documentation site [with the protocol to use, ex: http(s)://...]",
                  (protocol, domain) =>
                  {
                      if (protocol == null || domain == null)
                      {
                          throw new OptionException("Missing parameter web site home page url for option -w.", "-w");
                      }
                      webDocumentationUrl = WebDocumentation.BuildWebDocumentationUrl(protocol, domain);
                  } },
                { "wL|webdocLogin="******"(optional) Authentification file for the extern documentation site (first line: username, second line: password)",
                  opt =>
                  {
                      if (opt == null)
                      {
                          throw new OptionException("Missing parameter web site auth file for option -wL.", "-wL");
                      }
                      if (!File.Exists(opt))
                      {
                          throw new OptionException("Auth config file doesn't exist.", "-wL");
                      }
                      var lines = File.ReadAllLines(opt);
                      if (lines.Length < 2)
                      {
                          throw new OptionException("Invalid auth config file, should be one line for username, one line for password", "-wL");
                      }
                      webDocumentationLogin = new NetworkCredential(lines[0], lines[1]);
                  } },
                "",
                { "h|help", "Show this message and exit", opt => showHelp = opt != null },
                "",
                "[Assembly1.dll Assembly1.xml...] Source files, if a config file is not specified, load source assembly and xml from the specified list of files",
                // default
                { "<>", opt => files.AddRange(opt.Split(' ', '\t')) },
            };

            try
            {
                options.Parse(args);

                StyleManager.Init(Config);
            }
            catch (OptionException e)
            {
                UsageError(e.Message);
            }

            if (showHelp)
            {
                options.WriteOptionDescriptions(Console.Out);
                StyleManager.WriteAvailaibleStyles(Console.Out);
                throw new FatalException();
            }

            // Copy config params from command line to current config
            Config.Parameters.AddRange(configParams);
            Config.StyleParameters.AddRange(styleParams);

            // Override webdoc url from commmand line parameters
            if (webDocumentationUrl != null)
            {
                Config.WebDocumentationUrl = webDocumentationUrl;
            }
            if (webDocumentationLogin != null)
            {
                Config.WebDocumentationLogin = webDocumentationLogin;
            }

            // Add files from command line
            if (files.Count > 0)
            {
                ConfigSourceGroup group = null;

                foreach (var file in files)
                {
                    if (group == null)
                    {
                        group = new ConfigSourceGroup {
                            MergeGroup = "default"
                        };
                        Config.Groups.Add(group);
                    }

                    var configSource = new ConfigSource();
                    var ext          = Path.GetExtension(file);
                    if (ext != null && ext.ToLower() == ".xml")
                    {
                        configSource.DocumentationPath = file;
                    }
                    else
                    {
                        configSource.AssemblyPath = file;
                    }

                    group.Sources.Add(configSource);
                }
            }

            if (Config.Groups.Count == 0 && Config.RootTopic == null)
            {
                UsageError("At least one option is missing. Either a valid config file (-config) or a direct list of assembly/xml files must be specified");
            }

            // Add global search directories
            foreach (var group in Config.Groups)
            {
                group.SearchDirectories.AddRange(additionalSearchDirectories);
            }

            // Add default style Standard if none is defined
            if (Config.StyleNames.Count == 0)
            {
                Config.StyleNames.Add("Standard");
            }

            // Verify the validity of the style
            foreach (var styleName in Config.StyleNames)
            {
                if (!StyleManager.StyleExist(styleName))
                {
                    UsageError("Style [{0}] does not exist. Use --help to have a list of available styles.", styleName);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Parses the command line arguments.
        /// </summary>
        /// <param name="args">The args.</param>
        public void ParseArguments(string[] args)
        {
            var showHelp = false;

            var files = new List <string>();

            var options = new OptionSet()
            {
                "Copyright (c) 2010-2013 SharpDX - Alexandre Mutel",
                "Usage: SharpDoc [options]* [--config file.xml | Assembly1.dll Assembly1.xml...]*",
                "Documentation generator for .Net languages",
                "",
                "options:",
                { "c|config=", "Configuration file", opt => Config = Config.Load(opt) },

                {
                    "D=", "Define a template parameter with an (optional) value.",
                    (param, value) =>
                    {
                        if (param == null)
                        {
                            throw new OptionException("Missing parameter name for option -D.", "-D");
                        }
                        Config.Parameters.Add(new ConfigParam(param, value));
                    }
                },
                {
                    "S=", "Define a style parameter with a (optional) value.",
                    (style, value) =>
                    {
                        if (style == null)
                        {
                            throw new OptionException("Missing parameter name/value for option -S.", "-S");
                        }
                        Config.StyleParameters.Add(new ConfigParam(style, value));
                    }
                },
                { "d|style-dir=", "Add a style directory", opt => Config.StyleDirectories.Add(opt) },
                { "s|style=", "Specify the style to use [default: Standard]", opt => Config.StyleNames.Add(opt) },
                { "o|output=", "Specify the output directory [default: Output]", opt => Config.OutputDirectory = opt },
                { "r|references=", "Add reference assemblies in order to load source assemblies", opt => Config.References.Add(opt) },
                "",
                { "h|help", "Show this message and exit", opt => showHelp = opt != null },
                "",
                "[Assembly1.dll Assembly1.xml...] Source files, if a config file is not specified, load source assembly and xml from the specified list of files",
                // default
                { "<>", opt => files.AddRange(opt.Split(' ', '\t')) },
            };

            try
            {
                options.Parse(args);

                StyleManager.Init(Config);
            }
            catch (OptionException e)
            {
                UsageError(e.Message);
            }

            if (showHelp)
            {
                options.WriteOptionDescriptions(Console.Out);
                StyleManager.WriteAvailaibleStyles(Console.Out);
                Environment.Exit(0);
            }

            // Add files from command line
            if (files.Count > 0)
            {
                foreach (var file in files)
                {
                    var configSource = new ConfigSource();
                    var ext          = Path.GetExtension(file);
                    if (ext != null && ext.ToLower() == ".xml")
                    {
                        configSource.DocumentationPath = file;
                    }
                    else
                    {
                        configSource.AssemblyPath = file;
                    }

                    Config.Sources.Add(configSource);
                }
            }

            if (Config.Sources.Count == 0)
            {
                UsageError("At least one option is missing. Either a valid config file (-config) or a direct list of assembly/xml files must be specified");
            }

            // Add default style Standard if none is defined
            if (Config.StyleNames.Count == 0)
            {
                Config.StyleNames.Add("Standard");
            }

            // Verify the validity of the style
            foreach (var styleName in Config.StyleNames)
            {
                if (!StyleManager.StyleExist(styleName))
                {
                    UsageError("Style [{0}] does not exist. Use --help to have a list of available styles.", styleName);
                }
            }
        }