File: mdoc.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (182 lines) | stat: -rw-r--r-- 4,742 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Part of the mdoc(7) suite of tools.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Mono.Options;

namespace Mono.Documentation {

	class MDoc {

		private static bool debug;

		private static void Main (string[] args)
		{
			MDoc d = new MDoc ();
			try {
				d.Run (args);
			}
			catch (Exception e) {
				if (debug) {
					Console.Error.WriteLine ("mdoc: {0}", e.ToString ());
				}
				else {
					Console.Error.WriteLine ("mdoc: {0}", e.Message);
				}
				Console.Error.WriteLine ("See `mdoc help' for more information or use --debug to diagnose.");
				Environment.ExitCode = 1;
			}
		}

		int verbosity = 2;

		internal Dictionary<string, MDocCommand> subcommands;

		private void Run (string[] args)
		{
			subcommands = new Dictionary<string, MDocCommand> () {
				{ "assemble",         new MDocAssembler () },
				{ "dump-tree",        new MDocTreeDumper () },
				{ "export-html",      new MDocToHtmlConverter () },
				{ "export-html-webdoc",   new MDocExportWebdocHtml () },
				{ "export-msxdoc",    new MDocToMSXDocConverter () },
				{ "help",             new MDocHelpCommand (this) },
				{ "update",           new MDocUpdater () },
				{ "update-ecma-xml",  new MDocUpdateEcmaXml () },
				{ "validate",         new MDocValidator () },
				{ "index",            new MDocIndex () },
				{ "fx-bootstrap",     new MDocFrameworksBootstrapper () }
			};

			bool showVersion = false;
			bool showHelp    = false;
			var p = new OptionSet () {
				{ "version",  v => showVersion = v != null },
				{ "v:",       (int? v) => verbosity = v.HasValue ? v.Value : verbosity+1 },
				{ "debug",    v => debug = v != null },
				{ "h|?|help", v => showHelp = v != null },
				new ResponseFileSource (),
			};

			var extra = p.Parse (args);

            if (showVersion) {
				Console.WriteLine ("mdoc {0}", Consts.MonoVersion);
				return;
			}
			if (extra.Count == 0) {
				Console.WriteLine ("Use `mdoc help' for usage.");
				return;
			}
			if (showHelp) {
				extra.Add ("--help");
			}
			switch (extra [0]) {
				case "x-msitomsx":
					new MsidocToMsxdocConverter ().Run (extra);
					break;
				default: 
					GetCommand (extra [0]).Run (extra);
					break;
			}
		}

		internal MDocCommand GetCommand (string command)
		{
			MDocCommand h;
			if (!subcommands.TryGetValue (command, out h)) {
				Error ("Unknown command: {0}.", command);
			}
			h.TraceLevel  = (TraceLevel) verbosity;
			h.DebugOutput = debug;
			return h;
		}

		private static void Error (string format, params object[] args)
		{
			throw new Exception (string.Format (format, args));
		}
	}

	public abstract class MDocCommand {

		public TraceLevel TraceLevel { get; set; }
		public bool DebugOutput { get; set; }

		public abstract void Run (IEnumerable<string> args);

		protected List<string> Parse (OptionSet p, IEnumerable<string> args, 
				string command, string prototype, string description)
		{
			bool showHelp = false;
			p.Add ("h|?|help", 
					"Show this message and exit.", 
					v => showHelp = v != null );

			List<string> extra = null;
			if (args != null) {
				extra = p.Parse (args.Skip (1));
			}
			if (args == null || showHelp) {
				Console.WriteLine ("usage: mdoc {0} {1}", 
						args == null ? command : args.First(), prototype);
				Console.WriteLine ();
				Console.WriteLine (description);
				Console.WriteLine ();
				Console.WriteLine ("Available Options:");
				p.WriteOptionDescriptions (Console.Out);
				return null;
			}
			return extra;
		}

		public void Error (string format, params object[] args)
		{
			throw new Exception (string.Format (format, args));
		}

		public void Message (TraceLevel level, string format, params object[] args)
		{
			if ((int) level > (int) TraceLevel)
				return;
			if (level == TraceLevel.Error)
				Console.Error.WriteLine (format, args);
			else
				Console.WriteLine (format, args);
		}
	}

	class MDocHelpCommand : MDocCommand {

		MDoc instance;

		public MDocHelpCommand (MDoc instance)
		{
			this.instance = instance;
		}

		public override void Run (IEnumerable<string> args)
		{
			if (args != null && args.Count() > 1) {
				foreach (var arg in args.Skip (1)) {
					instance.GetCommand (arg).Run (new string[]{arg, "--help"});
				}
				return;
			}
			Message (TraceLevel.Warning, 
				"usage: mdoc COMMAND [OPTIONS]\n" +
				"Use `mdoc help COMMAND' for help on a specific command.\n" +
				"\n" + 
				"Available commands:\n\n   " +
				string.Join ("\n   ", instance.subcommands.Keys.OrderBy (v => v).ToArray()) +
				"\n\n" + 
				"mdoc is a tool for documentation management.\n" +
				"For additional information, see http://www.mono-project.com/"
			);
		}
	}
}