1 // Part of the mdoc(7) suite of tools.
3 using System.Collections.Generic;
7 namespace Mono.Documentation {
10 private static bool debug;
12 private static void Main (string[] args)
20 Console.Error.WriteLine ("mdoc: {0}", e.ToString ());
23 Console.Error.WriteLine ("mdoc: {0}", e.Message);
25 Console.Error.WriteLine ("See `mdoc help' for more information.");
29 Dictionary<string, Action<List<string>>> subcommands;
31 private void Run (string[] args)
33 subcommands = new Dictionary<string, Action<List<string>>> () {
34 { "assemble", Assemble },
35 { "export-html", ExportHtml },
36 { "export-msxdoc", ExportMSXDoc},
39 { "validate", Validate },
42 bool showVersion = false;
43 bool showHelp = false;
44 var p = new OptionSet () {
45 { "version", v => showVersion = v != null },
46 { "debug", v => debug = v != null },
47 { "h|?|help", v => showHelp = v != null },
50 List<string> extra = p.Parse (args);
53 Console.WriteLine ("mdoc 0.1.0");
56 if (extra.Count == 0) {
63 GetCommand (extra [0]) (extra);
66 private Action<List<string>> GetCommand (string command)
68 Action<List<string>> h;
69 if (!subcommands.TryGetValue (command, out h)) {
70 Error ("Unknown command: {0}.", command);
75 private void Help (List<string> args)
77 if (args != null && args.Count > 1) {
78 for (int i = 1; i < args.Count; ++i) {
79 GetCommand (args [i]) (new List<string>(){args [i], "--help"});
84 "usage: mdoc COMMAND [OPTIONS]\n" +
85 "Use `mdoc help COMMAND' for help on a specific command.\n" +
87 "Available commands:\n\n " +
88 string.Join ("\n ", subcommands.Keys.OrderBy (v => v).ToArray()) +
90 "mdoc is a tool for documentation management.\n" +
91 "For additional information, see http://www.mono-project.com/"
95 private static void Error (string format, params object[] args)
97 throw new Exception (string.Format (format, args));
100 private List<string> Parse (OptionSet p, List<string> args, string command,
101 string prototype, string description, ref bool showHelp)
103 List<string> extra = null;
105 extra = p.Parse (args.Skip (1));
107 if (args == null || showHelp) {
108 Console.WriteLine ("usage: mdoc {0} {1}",
109 args == null ? command : args [0], prototype);
110 Console.WriteLine ();
111 Console.WriteLine (description);
112 Console.WriteLine ();
113 Console.WriteLine ("Available Options:");
114 p.WriteOptionDescriptions (Console.Out);
120 private void Assemble (List<string> args)
122 string[] validFormats = {
131 var formats = new Dictionary<string, List<string>> ();
132 string prefix = "tree";
133 bool showHelp = false;
134 string format = "ecma";
135 var p = new OptionSet () {
137 "The documentation {FORMAT} used in DIRECTORIES. " +
138 "Valid formats include:\n " +
139 string.Join ("\n ", validFormats) + "\n" +
140 "If not specified, the default format is `ecma'.",
142 if (Array.IndexOf (validFormats, v) < 0)
143 Error ("Invalid documentation format: {0}.", v);
147 "Provides the output file prefix; the files {PREFIX}.zip and " +
148 "{PREFIX}.tree will be created.\n" +
149 "If not specified, `tree' is the default PREFIX.",
152 "Show this message and exit.",
153 v => showHelp = v != null },
154 { "<>", v => AddFormat (formats, format, v) },
156 List<string> extra = Parse (p, args, "assemble",
157 "[OPTIONS]+ DIRECTORIES",
158 "Assemble documentation within DIRECTORIES for use within the monodoc browser.",
162 MDocAssembler.Run (prefix, formats);
165 private static void AddFormat (Dictionary<string, List<string>> d, string format, string file)
168 Error ("No format specified.");
170 if (!d.TryGetValue (format, out l)) {
171 l = new List<string> ();
177 private void ExportHtml (List<string> args)
179 var opts = new MDocToHtmlConverterOptions ();
180 bool showHelp = false;
181 var p = new OptionSet () {
183 "The file {EXTENSION} to use for created files. "+
184 "This defaults to \"html\".",
187 "An XSLT {FILE} to use to generate the created " +
188 "files. If not specified, uses the template generated by --dump-template.",
189 v => opts.template = v },
190 { "default-template",
191 "Writes the default XSLT to stdout.",
192 v => opts.dumptemplate = v != null },
194 "The {DIRECTORY} to place the generated files and directories.",
195 v => opts.dest = v },
197 "Show this message and exit.",
198 v => showHelp = v != null },
200 List<string> extra = Parse (p, args, "export-html",
201 "[OPTIONS]+ DIRECTORIES",
202 "Export mdoc documentation within DIRECTORIES to HTML.", ref showHelp);
205 if (extra.Count == 0)
206 MDocToHtmlConverter.Run (opts);
207 foreach (var source in extra) {
208 opts.source = source;
209 MDocToHtmlConverter.Run (opts);
213 private void ExportMSXDoc (List<string> args)
216 bool showHelp = false;
217 var p = new OptionSet () {
219 "The XML {FILE} to generate.\n" +
220 "If not specified, will create a set of files in the curent directory " +
221 "based on the //AssemblyInfo/AssemblyName values within the documentation.\n" +
222 "Use '-' to write to standard output.",
225 "Show this message and exit.",
226 v => showHelp = v != null },
228 List<string> directories = Parse (p, args, "export-slashdoc",
229 "[OPTIONS]+ DIRECTORIES",
230 "Export mdoc(5) documentation within DIRECTORIES into \n" +
231 "Microsoft XML Documentation format files.",
233 if (directories == null)
235 MDocToMSXDocConverter.Run (file, directories);
238 private void Update (List<string> args)
240 var opts = new MDocUpdaterOptions {
243 show_exceptions = debug,
246 bool showHelp = false;
247 var types = new List<string> ();
248 var p = new OptionSet () {
250 "Root {DIRECTORY} to generate/update documentation.",
251 v => opts.path = v },
253 "Import documentation from {FILE}.",
254 v => opts.import = v },
256 "Delete removed members from the XML files.",
257 v => opts.delete = v != null },
259 "Manually specify the assembly version that new members were added in.",
260 v => opts.since = v },
262 "Only update documentation for {TYPE}.",
263 v => types.Add (v) },
265 "Show this message and exit.",
266 v => showHelp = v != null },
268 List<string> extra = Parse (p, args, "update",
269 "[OPTIONS]+ ASSEMBLIES",
270 "Create or update documentation from ASSEMBLIES.", ref showHelp);
273 if (extra.Count == 0)
274 Error ("No assemblies specified.");
275 opts.assembly = extra.ToArray ();
277 opts.type = types.ToArray ();
278 MDocUpdater.Run (opts);
281 private void Validate (List<string> args)
283 string[] validFormats = {
286 string format = "ecma";
287 bool showHelp = false;
288 var p = new OptionSet () {
290 "The documentation {0:FORMAT} used within PATHS. " +
291 "Valid formats include:\n " +
292 string.Join ("\n ", validFormats) + "\n" +
293 "If no format provided, `ecma' is used.",
296 "Show this message and exit.",
297 v => showHelp = v != null },
299 List<string> extra = Parse (p, args, "validate",
301 "Validate PATHS against the specified format schema.", ref showHelp);
304 if (Array.IndexOf (validFormats, format) < 0)
305 Error ("Invalid documentation format: {0}.", format);
306 MDocValidator.Run (format, extra);