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 or use --debug to diagnose.");
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 () }
,
50 { "index", new MDocIndex () }
,
51 { "preserve", new MDocPreserve () }
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 (),
64 var extra
= p
.Parse (args
);
67 Console
.WriteLine ("mdoc {0}", Consts
.MonoVersion
);
70 if (extra
.Count
== 0) {
71 Console
.WriteLine ("Use `mdoc help' for usage.");
79 new MsidocToMsxdocConverter ().Run (extra
);
82 GetCommand (extra
[0]).Run (extra
);
87 internal MDocCommand
GetCommand (string command
)
90 if (!subcommands
.TryGetValue (command
, out h
)) {
91 Error ("Unknown command: {0}.", command
);
93 h
.TraceLevel
= (TraceLevel
) verbosity
;
94 h
.DebugOutput
= debug
;
98 private static void Error (string format
, params object[] args
)
100 throw new Exception (string.Format (format
, args
));
104 public abstract class MDocCommand
{
106 public TraceLevel TraceLevel { get; set; }
107 public bool DebugOutput { get; set; }
109 public abstract void Run (IEnumerable
<string> args
);
111 protected List
<string> Parse (OptionSet p
, IEnumerable
<string> args
,
112 string command
, string prototype
, string description
)
114 bool showHelp
= false;
116 "Show this message and exit.",
117 v
=> showHelp
= v
!= null );
119 List
<string> extra
= null;
121 extra
= p
.Parse (args
.Skip (1));
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
);
136 public void Error (string format
, params object[] args
)
138 throw new Exception (string.Format (format
, args
));
141 public void Message (TraceLevel level
, string format
, params object[] args
)
143 if ((int) level
> (int) TraceLevel
)
145 if (level
== TraceLevel
.Error
)
146 Console
.Error
.WriteLine (format
, args
);
148 Console
.WriteLine (format
, args
);
152 class MDocHelpCommand
: MDocCommand
{
156 public MDocHelpCommand (MDoc instance
)
158 this.instance
= instance
;
161 public override void Run (IEnumerable
<string> args
)
163 if (args
!= null && args
.Count() > 1) {
164 foreach (var arg
in args
.Skip (1)) {
165 instance
.GetCommand (arg
).Run (new string[]{arg, "--help"}
);
169 Message (TraceLevel
.Warning
,
170 "usage: mdoc COMMAND [OPTIONS]\n" +
171 "Use `mdoc help COMMAND' for help on a specific command.\n" +
173 "Available commands:\n\n " +
174 string.Join ("\n ", instance
.subcommands
.Keys
.OrderBy (v
=> v
).ToArray()) +
176 "mdoc is a tool for documentation management.\n" +
177 "For additional information, see http://www.mono-project.com/"