[runtime] Disable some tests in full-aot mode which cannot be AOTed because of type...
[mono.git] / mcs / tools / mdoc / Mono.Documentation / mdoc.cs
1 // Part of the mdoc(7) suite of tools.
2 using System;
3 using System.Collections.Generic;
4 using System.Diagnostics;
5 using System.IO;
6 using System.Linq;
7 using System.Text;
8 using Mono.Options;
9
10 namespace Mono.Documentation {
11
12         class MDoc {
13
14                 private static bool debug;
15
16                 private static void Main (string[] args)
17                 {
18                         MDoc d = new MDoc ();
19                         try {
20                                 d.Run (args);
21                         }
22                         catch (Exception e) {
23                                 if (debug) {
24                                         Console.Error.WriteLine ("mdoc: {0}", e.ToString ());
25                                 }
26                                 else {
27                                         Console.Error.WriteLine ("mdoc: {0}", e.Message);
28                                 }
29                                 Console.Error.WriteLine ("See `mdoc help' for more information or use --debug to diagnose.");
30                                 Environment.ExitCode = 1;
31                         }
32                 }
33
34                 int verbosity = 2;
35
36                 internal Dictionary<string, MDocCommand> subcommands;
37
38                 private void Run (string[] args)
39                 {
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 () },
51                                 { "preserve",         new MDocPreserve () }
52                         };
53
54                         bool showVersion = false;
55                         bool showHelp    = false;
56                         var p = new OptionSet () {
57                                 { "version",  v => showVersion = v != null },
58                                 { "v:",       (int? v) => verbosity = v.HasValue ? v.Value : verbosity+1 },
59                                 { "debug",    v => debug = v != null },
60                                 { "h|?|help", v => showHelp = v != null },
61                                 new ResponseFileSource (),
62                         };
63
64                         var extra = p.Parse (args);
65
66                         if (showVersion) {
67                                 Console.WriteLine ("mdoc {0}", Consts.MonoVersion);
68                                 return;
69                         }
70                         if (extra.Count == 0) {
71                                 Console.WriteLine ("Use `mdoc help' for usage.");
72                                 return;
73                         }
74                         if (showHelp) {
75                                 extra.Add ("--help");
76                         }
77                         switch (extra [0]) {
78                                 case "x-msitomsx":
79                                         new MsidocToMsxdocConverter ().Run (extra);
80                                         break;
81                                 default: 
82                                         GetCommand (extra [0]).Run (extra);
83                                         break;
84                         }
85                 }
86
87                 internal MDocCommand GetCommand (string command)
88                 {
89                         MDocCommand h;
90                         if (!subcommands.TryGetValue (command, out h)) {
91                                 Error ("Unknown command: {0}.", command);
92                         }
93                         h.TraceLevel  = (TraceLevel) verbosity;
94                         h.DebugOutput = debug;
95                         return h;
96                 }
97
98                 private static void Error (string format, params object[] args)
99                 {
100                         throw new Exception (string.Format (format, args));
101                 }
102         }
103
104         public abstract class MDocCommand {
105
106                 public TraceLevel TraceLevel { get; set; }
107                 public bool DebugOutput { get; set; }
108
109                 public abstract void Run (IEnumerable<string> args);
110
111                 protected List<string> Parse (OptionSet p, IEnumerable<string> args, 
112                                 string command, string prototype, string description)
113                 {
114                         bool showHelp = false;
115                         p.Add ("h|?|help", 
116                                         "Show this message and exit.", 
117                                         v => showHelp = v != null );
118
119                         List<string> extra = null;
120                         if (args != null) {
121                                 extra = p.Parse (args.Skip (1));
122                         }
123                         if (args == null || showHelp) {
124                                 Console.WriteLine ("usage: mdoc {0} {1}", 
125                                                 args == null ? command : args.First(), prototype);
126                                 Console.WriteLine ();
127                                 Console.WriteLine (description);
128                                 Console.WriteLine ();
129                                 Console.WriteLine ("Available Options:");
130                                 p.WriteOptionDescriptions (Console.Out);
131                                 return null;
132                         }
133                         return extra;
134                 }
135
136                 public void Error (string format, params object[] args)
137                 {
138                         throw new Exception (string.Format (format, args));
139                 }
140
141                 public void Message (TraceLevel level, string format, params object[] args)
142                 {
143                         if ((int) level > (int) TraceLevel)
144                                 return;
145                         if (level == TraceLevel.Error)
146                                 Console.Error.WriteLine (format, args);
147                         else
148                                 Console.WriteLine (format, args);
149                 }
150         }
151
152         class MDocHelpCommand : MDocCommand {
153
154                 MDoc instance;
155
156                 public MDocHelpCommand (MDoc instance)
157                 {
158                         this.instance = instance;
159                 }
160
161                 public override void Run (IEnumerable<string> args)
162                 {
163                         if (args != null && args.Count() > 1) {
164                                 foreach (var arg in args.Skip (1)) {
165                                         instance.GetCommand (arg).Run (new string[]{arg, "--help"});
166                                 }
167                                 return;
168                         }
169                         Message (TraceLevel.Warning, 
170                                 "usage: mdoc COMMAND [OPTIONS]\n" +
171                                 "Use `mdoc help COMMAND' for help on a specific command.\n" +
172                                 "\n" + 
173                                 "Available commands:\n\n   " +
174                                 string.Join ("\n   ", instance.subcommands.Keys.OrderBy (v => v).ToArray()) +
175                                 "\n\n" + 
176                                 "mdoc is a tool for documentation management.\n" +
177                                 "For additional information, see http://www.mono-project.com/"
178                         );
179                 }
180         }
181 }
182