1 // Part of the mdoc(7) suite of tools.
3 using System.Collections.Generic;
4 using System.Diagnostics;
10 namespace Mono.Documentation {
14 private static bool debug;
16 private static void Main (string[] args)
24 Console.Error.WriteLine ("mdoc: {0}", e.ToString ());
27 Console.Error.WriteLine ("mdoc: {0}", e.Message);
29 Console.Error.WriteLine ("See `mdoc help' for more information.");
30 Environment.ExitCode = 1;
36 internal Dictionary<string, MDocCommand> subcommands;
38 private void Run (string[] args)
40 subcommands = new Dictionary<string, MDocCommand> () {
41 { "assemble", new MDocAssembler () },
42 { "dump-tree", new MDocTreeDumper () },
43 { "export-html", new MDocToHtmlConverter () },
44 { "export-html-webdoc", new MDocExportWebdocHtml () },
45 { "export-msxdoc", new MDocToMSXDocConverter () },
46 { "help", new MDocHelpCommand (this) },
47 { "update", new MDocUpdater () },
48 { "update-ecma-xml", new MDocUpdateEcmaXml () },
49 { "validate", new MDocValidator () },
52 bool showVersion = false;
53 bool showHelp = false;
54 var p = new OptionSet () {
55 { "version", v => showVersion = v != null },
56 { "v:", (int? v) => verbosity = v.HasValue ? v.Value : verbosity+1 },
57 { "debug", v => debug = v != null },
58 { "h|?|help", v => showHelp = v != null },
59 new ResponseFileSource (),
62 var extra = p.Parse (args);
65 Console.WriteLine ("mdoc {0}", Consts.MonoVersion);
68 if (extra.Count == 0) {
69 Console.WriteLine ("Use `mdoc help' for usage.");
77 new MsidocToMsxdocConverter ().Run (extra);
80 GetCommand (extra [0]).Run (extra);
85 internal MDocCommand GetCommand (string command)
88 if (!subcommands.TryGetValue (command, out h)) {
89 Error ("Unknown command: {0}.", command);
91 h.TraceLevel = (TraceLevel) verbosity;
92 h.DebugOutput = debug;
96 private static void Error (string format, params object[] args)
98 throw new Exception (string.Format (format, args));
102 public abstract class MDocCommand {
104 public TraceLevel TraceLevel { get; set; }
105 public bool DebugOutput { get; set; }
107 public abstract void Run (IEnumerable<string> args);
109 protected List<string> Parse (OptionSet p, IEnumerable<string> args,
110 string command, string prototype, string description)
112 bool showHelp = false;
114 "Show this message and exit.",
115 v => showHelp = v != null );
117 List<string> extra = null;
119 extra = p.Parse (args.Skip (1));
121 if (args == null || showHelp) {
122 Console.WriteLine ("usage: mdoc {0} {1}",
123 args == null ? command : args.First(), prototype);
124 Console.WriteLine ();
125 Console.WriteLine (description);
126 Console.WriteLine ();
127 Console.WriteLine ("Available Options:");
128 p.WriteOptionDescriptions (Console.Out);
134 public void Error (string format, params object[] args)
136 throw new Exception (string.Format (format, args));
139 public void Message (TraceLevel level, string format, params object[] args)
141 if ((int) level > (int) TraceLevel)
143 if (level == TraceLevel.Error)
144 Console.Error.WriteLine (format, args);
146 Console.WriteLine (format, args);
150 class MDocHelpCommand : MDocCommand {
154 public MDocHelpCommand (MDoc instance)
156 this.instance = instance;
159 public override void Run (IEnumerable<string> args)
161 if (args != null && args.Count() > 1) {
162 foreach (var arg in args.Skip (1)) {
163 instance.GetCommand (arg).Run (new string[]{arg, "--help"});
167 Message (TraceLevel.Warning,
168 "usage: mdoc COMMAND [OPTIONS]\n" +
169 "Use `mdoc help COMMAND' for help on a specific command.\n" +
171 "Available commands:\n\n " +
172 string.Join ("\n ", instance.subcommands.Keys.OrderBy (v => v).ToArray()) +
174 "mdoc is a tool for documentation management.\n" +
175 "For additional information, see http://www.mono-project.com/"