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 () },
52 bool showVersion = false;
53 bool showHelp = false;
54 var extra = new List<string> ();
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 },
61 if (v.Length > 0 && v [0] == '@')
62 extra.AddRange (ReadResponseFile (v.Substring (1)));
71 Console.WriteLine ("mdoc {0}", Consts.MonoVersion);
74 if (extra.Count == 0) {
75 Console.WriteLine ("Use `mdoc help' for usage.");
81 GetCommand (extra [0]).Run (extra);
84 // Cribbed from mcs/driver.cs:LoadArgs(string)
85 static IEnumerable<string> ReadResponseFile (string file)
87 StreamReader response;
89 response = File.OpenText (file);
95 StringBuilder arg = new StringBuilder ();
98 while ((line = response.ReadLine ()) != null) {
101 for (int i = 0; i < t; i++) {
104 if (c == '"' || c == '\'') {
107 for (i++; i < t; i++){
114 } else if (c == ' ') {
115 if (arg.Length > 0) {
116 yield return arg.ToString ();
122 if (arg.Length > 0) {
123 yield return arg.ToString ();
130 internal MDocCommand GetCommand (string command)
133 if (!subcommands.TryGetValue (command, out h)) {
134 Error ("Unknown command: {0}.", command);
136 h.TraceLevel = (TraceLevel) verbosity;
137 h.DebugOutput = debug;
141 private static void Error (string format, params object[] args)
143 throw new Exception (string.Format (format, args));
147 public abstract class MDocCommand {
149 public TraceLevel TraceLevel { get; set; }
150 public bool DebugOutput { get; set; }
152 public abstract void Run (IEnumerable<string> args);
154 protected List<string> Parse (OptionSet p, IEnumerable<string> args,
155 string command, string prototype, string description)
157 bool showHelp = false;
159 "Show this message and exit.",
160 v => showHelp = v != null );
162 List<string> extra = null;
164 extra = p.Parse (args.Skip (1));
166 if (args == null || showHelp) {
167 Console.WriteLine ("usage: mdoc {0} {1}",
168 args == null ? command : args.First(), prototype);
169 Console.WriteLine ();
170 Console.WriteLine (description);
171 Console.WriteLine ();
172 Console.WriteLine ("Available Options:");
173 p.WriteOptionDescriptions (Console.Out);
179 public void Error (string format, params object[] args)
181 throw new Exception (string.Format (format, args));
184 public void Message (TraceLevel level, string format, params object[] args)
186 if ((int) level > (int) TraceLevel)
188 if (level == TraceLevel.Error)
189 Console.Error.WriteLine (format, args);
191 Console.WriteLine (format, args);
195 class MDocHelpCommand : MDocCommand {
199 public MDocHelpCommand (MDoc instance)
201 this.instance = instance;
204 public override void Run (IEnumerable<string> args)
206 if (args != null && args.Count() > 1) {
207 foreach (var arg in args.Skip (1)) {
208 instance.GetCommand (arg).Run (new string[]{arg, "--help"});
212 Message (TraceLevel.Warning,
213 "usage: mdoc COMMAND [OPTIONS]\n" +
214 "Use `mdoc help COMMAND' for help on a specific command.\n" +
216 "Available commands:\n\n " +
217 string.Join ("\n ", instance.subcommands.Keys.OrderBy (v => v).ToArray()) +
219 "mdoc is a tool for documentation management.\n" +
220 "For additional information, see http://www.mono-project.com/"