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.");
33 internal Dictionary<string, MDocCommand> subcommands;
35 private void Run (string[] args)
37 subcommands = new Dictionary<string, MDocCommand> () {
38 { "assemble", new MDocAssembler () },
39 { "dump-tree", new MDocTreeDumper () },
40 { "export-html", new MDocToHtmlConverter () },
41 { "export-msxdoc", new MDocToMSXDocConverter () },
42 { "help", new MDocHelpCommand (this) },
43 { "update", new MDocUpdater () },
44 { "validate", new MDocValidator () },
47 bool showVersion = false;
48 bool showHelp = false;
49 var p = new OptionSet () {
50 { "version", v => showVersion = v != null },
51 { "v:", (int? v) => verbosity = v.HasValue ? v.Value : verbosity+1 },
52 { "debug", v => debug = v != null },
53 { "h|?|help", v => showHelp = v != null },
56 List<string> extra = p.Parse (args);
59 Console.WriteLine ("mdoc 0.1.0");
62 if (extra.Count == 0) {
63 new MDocHelpCommand (this).Run (null);
68 GetCommand (extra [0]).Run (extra);
71 internal MDocCommand GetCommand (string command)
74 if (!subcommands.TryGetValue (command, out h)) {
75 Error ("Unknown command: {0}.", command);
77 h.TraceLevel = (TraceLevel) verbosity;
78 h.DebugOutput = debug;
82 private static void Error (string format, params object[] args)
84 throw new Exception (string.Format (format, args));
88 public abstract class MDocCommand {
90 public TraceLevel TraceLevel { get; set; }
91 public bool DebugOutput { get; set; }
93 public abstract void Run (IEnumerable<string> args);
95 protected List<string> Parse (OptionSet p, IEnumerable<string> args,
96 string command, string prototype, string description)
98 bool showHelp = false;
100 "Show this message and exit.",
101 v => showHelp = v != null );
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.First(), prototype);
110 Console.WriteLine ();
111 Console.WriteLine (description);
112 Console.WriteLine ();
113 Console.WriteLine ("Available Options:");
114 p.WriteOptionDescriptions (Console.Out);
120 public void Error (string format, params object[] args)
122 throw new Exception (string.Format (format, args));
125 public void Message (TraceLevel level, string format, params object[] args)
127 if ((int) level > (int) TraceLevel)
129 if (level == TraceLevel.Error)
130 Console.Error.WriteLine (format, args);
132 Console.WriteLine (format, args);
136 class MDocHelpCommand : MDocCommand {
140 public MDocHelpCommand (MDoc instance)
142 this.instance = instance;
145 public override void Run (IEnumerable<string> args)
147 if (args != null && args.Count() > 1) {
148 foreach (var arg in args.Skip (1)) {
149 instance.GetCommand (arg).Run (new string[]{arg, "--help"});
153 Message (TraceLevel.Warning,
154 "usage: mdoc COMMAND [OPTIONS]\n" +
155 "Use `mdoc help COMMAND' for help on a specific command.\n" +
157 "Available commands:\n\n " +
158 string.Join ("\n ", instance.subcommands.Keys.OrderBy (v => v).ToArray()) +
160 "mdoc is a tool for documentation management.\n" +
161 "For additional information, see http://www.mono-project.com/"