[mdoc] Response file cleanup.
[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.");
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                         };
51
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 (),
60                         };
61
62                         var extra = p.Parse (args);
63
64                         if (showVersion) {
65                                 Console.WriteLine ("mdoc {0}", Consts.MonoVersion);
66                                 return;
67                         }
68                         if (extra.Count == 0) {
69                                 Console.WriteLine ("Use `mdoc help' for usage.");
70                                 return;
71                         }
72                         if (showHelp) {
73                                 extra.Add ("--help");
74                         }
75                         switch (extra [0]) {
76                                 case "x-msitomsx":
77                                         new MsidocToMsxdocConverter ().Run (extra);
78                                         break;
79                                 default: 
80                                         GetCommand (extra [0]).Run (extra);
81                                         break;
82                         }
83                 }
84
85                 internal MDocCommand GetCommand (string command)
86                 {
87                         MDocCommand h;
88                         if (!subcommands.TryGetValue (command, out h)) {
89                                 Error ("Unknown command: {0}.", command);
90                         }
91                         h.TraceLevel  = (TraceLevel) verbosity;
92                         h.DebugOutput = debug;
93                         return h;
94                 }
95
96                 private static void Error (string format, params object[] args)
97                 {
98                         throw new Exception (string.Format (format, args));
99                 }
100         }
101
102         public abstract class MDocCommand {
103
104                 public TraceLevel TraceLevel { get; set; }
105                 public bool DebugOutput { get; set; }
106
107                 public abstract void Run (IEnumerable<string> args);
108
109                 protected List<string> Parse (OptionSet p, IEnumerable<string> args, 
110                                 string command, string prototype, string description)
111                 {
112                         bool showHelp = false;
113                         p.Add ("h|?|help", 
114                                         "Show this message and exit.", 
115                                         v => showHelp = v != null );
116
117                         List<string> extra = null;
118                         if (args != null) {
119                                 extra = p.Parse (args.Skip (1));
120                         }
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);
129                                 return null;
130                         }
131                         return extra;
132                 }
133
134                 public void Error (string format, params object[] args)
135                 {
136                         throw new Exception (string.Format (format, args));
137                 }
138
139                 public void Message (TraceLevel level, string format, params object[] args)
140                 {
141                         if ((int) level > (int) TraceLevel)
142                                 return;
143                         if (level == TraceLevel.Error)
144                                 Console.Error.WriteLine (format, args);
145                         else
146                                 Console.WriteLine (format, args);
147                 }
148         }
149
150         class MDocHelpCommand : MDocCommand {
151
152                 MDoc instance;
153
154                 public MDocHelpCommand (MDoc instance)
155                 {
156                         this.instance = instance;
157                 }
158
159                 public override void Run (IEnumerable<string> args)
160                 {
161                         if (args != null && args.Count() > 1) {
162                                 foreach (var arg in args.Skip (1)) {
163                                         instance.GetCommand (arg).Run (new string[]{arg, "--help"});
164                                 }
165                                 return;
166                         }
167                         Message (TraceLevel.Warning, 
168                                 "usage: mdoc COMMAND [OPTIONS]\n" +
169                                 "Use `mdoc help COMMAND' for help on a specific command.\n" +
170                                 "\n" + 
171                                 "Available commands:\n\n   " +
172                                 string.Join ("\n   ", instance.subcommands.Keys.OrderBy (v => v).ToArray()) +
173                                 "\n\n" + 
174                                 "mdoc is a tool for documentation management.\n" +
175                                 "For additional information, see http://www.mono-project.com/"
176                         );
177                 }
178         }
179 }
180