1 // Part of the mdoc(7) suite of tools.
3 using System.Collections.Generic;
4 using System.Diagnostics;
8 namespace Mono.Documentation {
12 private static bool debug;
14 private static void Main (string[] args)
22 Console.Error.WriteLine ("mdoc: {0}", e.ToString ());
25 Console.Error.WriteLine ("mdoc: {0}", e.Message);
27 Console.Error.WriteLine ("See `mdoc help' for more information.");
28 Environment.ExitCode = 1;
34 internal Dictionary<string, MDocCommand> subcommands;
36 private void Run (string[] args)
38 subcommands = new Dictionary<string, MDocCommand> () {
39 { "assemble", new MDocAssembler () },
40 { "dump-tree", new MDocTreeDumper () },
41 { "export-html", new MDocToHtmlConverter () },
42 { "export-html-webdoc", new MDocExportWebdocHtml () },
43 { "export-msxdoc", new MDocToMSXDocConverter () },
44 { "help", new MDocHelpCommand (this) },
45 { "update", new MDocUpdater () },
46 { "update-ecma-xml", new MDocUpdateEcmaXml () },
47 { "validate", new MDocValidator () },
50 bool showVersion = false;
51 bool showHelp = false;
52 var p = new OptionSet () {
53 { "version", v => showVersion = v != null },
54 { "v:", (int? v) => verbosity = v.HasValue ? v.Value : verbosity+1 },
55 { "debug", v => debug = v != null },
56 { "h|?|help", v => showHelp = v != null },
59 List<string> extra = p.Parse (args);
62 Console.WriteLine ("mdoc {0}", Consts.MonoVersion);
65 if (extra.Count == 0) {
66 Console.WriteLine ("Use `mdoc help' for usage.");
72 GetCommand (extra [0]).Run (extra);
75 internal MDocCommand GetCommand (string command)
78 if (!subcommands.TryGetValue (command, out h)) {
79 Error ("Unknown command: {0}.", command);
81 h.TraceLevel = (TraceLevel) verbosity;
82 h.DebugOutput = debug;
86 private static void Error (string format, params object[] args)
88 throw new Exception (string.Format (format, args));
92 public abstract class MDocCommand {
94 public TraceLevel TraceLevel { get; set; }
95 public bool DebugOutput { get; set; }
97 public abstract void Run (IEnumerable<string> args);
99 protected List<string> Parse (OptionSet p, IEnumerable<string> args,
100 string command, string prototype, string description)
102 bool showHelp = false;
104 "Show this message and exit.",
105 v => showHelp = v != null );
107 List<string> extra = null;
109 extra = p.Parse (args.Skip (1));
111 if (args == null || showHelp) {
112 Console.WriteLine ("usage: mdoc {0} {1}",
113 args == null ? command : args.First(), prototype);
114 Console.WriteLine ();
115 Console.WriteLine (description);
116 Console.WriteLine ();
117 Console.WriteLine ("Available Options:");
118 p.WriteOptionDescriptions (Console.Out);
124 public void Error (string format, params object[] args)
126 throw new Exception (string.Format (format, args));
129 public void Message (TraceLevel level, string format, params object[] args)
131 if ((int) level > (int) TraceLevel)
133 if (level == TraceLevel.Error)
134 Console.Error.WriteLine (format, args);
136 Console.WriteLine (format, args);
140 class MDocHelpCommand : MDocCommand {
144 public MDocHelpCommand (MDoc instance)
146 this.instance = instance;
149 public override void Run (IEnumerable<string> args)
151 if (args != null && args.Count() > 1) {
152 foreach (var arg in args.Skip (1)) {
153 instance.GetCommand (arg).Run (new string[]{arg, "--help"});
157 Message (TraceLevel.Warning,
158 "usage: mdoc COMMAND [OPTIONS]\n" +
159 "Use `mdoc help COMMAND' for help on a specific command.\n" +
161 "Available commands:\n\n " +
162 string.Join ("\n ", instance.subcommands.Keys.OrderBy (v => v).ToArray()) +
164 "mdoc is a tool for documentation management.\n" +
165 "For additional information, see http://www.mono-project.com/"