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 () },
50 { "index", new MDocIndex () },
53 bool showVersion = false;
54 bool showHelp = false;
55 var p = new OptionSet () {
56 { "version", v => showVersion = v != null },
57 { "v:", (int? v) => verbosity = v.HasValue ? v.Value : verbosity+1 },
58 { "debug", v => debug = v != null },
59 { "h|?|help", v => showHelp = v != null },
60 new ResponseFileSource (),
63 var extra = p.Parse (args);
66 Console.WriteLine ("mdoc {0}", Consts.MonoVersion);
69 if (extra.Count == 0) {
70 Console.WriteLine ("Use `mdoc help' for usage.");
78 new MsidocToMsxdocConverter ().Run (extra);
81 GetCommand (extra [0]).Run (extra);
86 internal MDocCommand GetCommand (string command)
89 if (!subcommands.TryGetValue (command, out h)) {
90 Error ("Unknown command: {0}.", command);
92 h.TraceLevel = (TraceLevel) verbosity;
93 h.DebugOutput = debug;
97 private static void Error (string format, params object[] args)
99 throw new Exception (string.Format (format, args));
103 public abstract class MDocCommand {
105 public TraceLevel TraceLevel { get; set; }
106 public bool DebugOutput { get; set; }
108 public abstract void Run (IEnumerable<string> args);
110 protected List<string> Parse (OptionSet p, IEnumerable<string> args,
111 string command, string prototype, string description)
113 bool showHelp = false;
115 "Show this message and exit.",
116 v => showHelp = v != null );
118 List<string> extra = null;
120 extra = p.Parse (args.Skip (1));
122 if (args == null || showHelp) {
123 Console.WriteLine ("usage: mdoc {0} {1}",
124 args == null ? command : args.First(), prototype);
125 Console.WriteLine ();
126 Console.WriteLine (description);
127 Console.WriteLine ();
128 Console.WriteLine ("Available Options:");
129 p.WriteOptionDescriptions (Console.Out);
135 public void Error (string format, params object[] args)
137 throw new Exception (string.Format (format, args));
140 public void Message (TraceLevel level, string format, params object[] args)
142 if ((int) level > (int) TraceLevel)
144 if (level == TraceLevel.Error)
145 Console.Error.WriteLine (format, args);
147 Console.WriteLine (format, args);
151 class MDocHelpCommand : MDocCommand {
155 public MDocHelpCommand (MDoc instance)
157 this.instance = instance;
160 public override void Run (IEnumerable<string> args)
162 if (args != null && args.Count() > 1) {
163 foreach (var arg in args.Skip (1)) {
164 instance.GetCommand (arg).Run (new string[]{arg, "--help"});
168 Message (TraceLevel.Warning,
169 "usage: mdoc COMMAND [OPTIONS]\n" +
170 "Use `mdoc help COMMAND' for help on a specific command.\n" +
172 "Available commands:\n\n " +
173 string.Join ("\n ", instance.subcommands.Keys.OrderBy (v => v).ToArray()) +
175 "mdoc is a tool for documentation management.\n" +
176 "For additional information, see http://www.mono-project.com/"