show the difference between two versions for a specified path
[tfs.git] / tools / opentf / HelpCommand.cs
blobafbe7b06c6a6d00c05d4394916ff0d5ba6e0d2f2
1 //
2 // HelpCommand.cs
3 //
4 // Authors:
5 // Joel Reed (joelwreed@gmail.com)
6 //
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System;
30 using System.Collections.Generic;
31 using System.IO;
32 using System.Text;
33 using System.Reflection;
34 using Microsoft.TeamFoundation.Client;
35 using Microsoft.TeamFoundation.VersionControl.Client;
36 using Mono.GetOptions;
38 [Command("help", "Describe the usage of this program or its subcommands.", "<command>")]
39 class HelpCommand : Command
41 [Option("List all commands without description", "L", "list")]
42 public bool OptionList = false;
44 public HelpCommand(Driver driver, string[] args): base(driver, args)
48 public void ShowHelp()
50 Console.WriteLine("Usage: tf [subcommand] [arguments]");
51 Console.WriteLine();
52 Console.WriteLine("Available subcommands:");
53 Console.WriteLine();
55 foreach (CommandAttribute attribute in CommandRegistry.Attributes.Values)
57 ShowCommandHelp(attribute, " ");
61 public void ShowList()
63 foreach (string attribute in CommandRegistry.Attributes.Keys)
65 Console.WriteLine(attribute);
69 static public void ShowCommandHelp(CommandAttribute attribute,
70 string indent)
72 Console.Write(indent + attribute.Name);
73 if (!String.IsNullOrEmpty(attribute.Alias)) Console.Write(" (alias {0})", attribute.Alias);
74 Console.WriteLine();
76 string description = attribute.Description;
77 int x = 0;
79 while (x < description.Length)
81 int y = Math.Min(x + 60, description.Length);
82 int z = description.IndexOf(' ', y) + 1;
83 if (z == 0) z = description.Length;
85 Console.WriteLine(indent + " " + description.Substring(x, z - x));
86 x = z;
89 Console.WriteLine();
92 public void ShowCommandHelp(string cmd)
94 CommandAttribute attr = CommandRegistry.GetCommandAttribute(cmd);
95 if (attr == null)
97 Console.WriteLine("Unknown command: " + cmd);
98 Console.WriteLine();
99 ShowHelp();
100 return;
103 ShowCommandHelp(attr, "");
104 Console.WriteLine("Usage: " + cmd + " " + attr.Args);
105 Console.WriteLine();
106 Console.WriteLine("Options:");
108 Type commandType = CommandRegistry.GetCommandType(cmd);
109 foreach(MemberInfo mi in commandType.GetMembers())
111 if (mi.MemberType != MemberTypes.Field) continue;
113 OptionAttribute[] attribs = mi.GetCustomAttributes(typeof(OptionAttribute), true) as OptionAttribute[];
114 if (attribs == null || attribs.Length == 0) continue;
116 foreach (OptionAttribute attrib in attribs)
118 Type fieldType = ((FieldInfo)mi).FieldType;
119 if (!String.IsNullOrEmpty(attrib.AlternateForm))
121 string altform = " /" + attrib.AlternateForm + "";
122 if (fieldType != typeof(bool)) altform += ":ARG";
123 Console.Write(altform.PadRight(20));
126 Console.Write("\t" + attrib.ShortDescription);
128 if (!String.IsNullOrEmpty(attrib.LongForm))
130 Console.Write(" (also /" + attrib.LongForm);
131 if (fieldType != typeof(bool)) Console.Write(":ARG");
132 Console.Write(")");
135 Console.WriteLine();
139 Console.WriteLine();
142 public override void Run()
144 if (OptionList)
146 ShowList();
147 return;
150 if (Arguments.Length > 0)
151 ShowCommandHelp(Arguments[0]);
152 else
153 ShowHelp();