2007-02-23 Nagappan A <anagappan@novell.com>
[mono.git] / mcs / tools / monop / monop.cs
index 1a13924a2e3747a5f619bd9e4d0f1b2dc9b31956..df046cd903e714f42bdd7ddaa9c785433b9a8ae0 100644 (file)
@@ -41,10 +41,6 @@ using System.Text;
 
 class MonoP {
        static string assembly;
-       static BindingFlags default_flags = 
-               BindingFlags.Instance |
-               BindingFlags.Static |
-               BindingFlags.Public;
        
        // very common namespaces, all in corlib
        static readonly string [] v_common_ns = {
@@ -81,27 +77,39 @@ class MonoP {
                return t;
        }
        
-       static string SearchTypes (string name)
+       static string SearchTypes (string name, ref Type retval, out int count)
        {
                StringBuilder sb = new StringBuilder ();
+               Type current = null;
+               count = 0;
 
-               foreach (string asm in GetKnownAssemblyNames ()) {
-                       Assembly a = GetAssembly (asm, false);
-
+               string [] assemblies = GetKnownAssemblyNames ();
+               for (int i = 0; i < assemblies.Length; i++) {
+                       Assembly a = GetAssembly (assemblies [i], false);
                        if (a == null)
                                continue;
 
-                       foreach (Type t in a.GetTypes ()) {
-                               if (t.Name == name)
-                                       sb.Append (t.FullName + " from " + a.Location + "\n");
-
-                               if (t.Name.ToLower ().IndexOf (name.ToLower ()) > 0)
+                       Type [] types = a.GetTypes ();
+                       for (int j = 0; j < types.Length; j++) {
+                               Type t = types [j];
+                               if (t.IsPublic == false)
+                                       continue;
+                               
+                               if (t.Name == name || t.Name.ToLower ().IndexOf (name.ToLower ()) > 0) {
+                                       current = t;
+                                       count ++;
                                        sb.Append (t.FullName + " from " + a.Location + "\n");
+                               }
                        }
                }
 
-               if (sb.Length == 0) 
+               if (count == 0) 
                        return null;
+
+               if (count == 1) {
+                       retval = current;
+                       return String.Empty;
+               }
                
                return sb.ToString ();
        }
@@ -113,7 +121,13 @@ class MonoP {
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.FileName = "gacutil";
                p.StartInfo.Arguments = "-l";
-               p.Start ();
+               try {
+                       p.Start ();
+               }
+               catch {
+                       Console.WriteLine ("WARNING: gacutil could not be found.");
+                       return new string[0];
+               }
 
                string s;
                ArrayList names = new ArrayList ();
@@ -183,8 +197,15 @@ class MonoP {
        {
                return GetType (tname, false);
        }
-       
-       static void PrintTypes (string assembly)
+
+       static void PrintRefs (string assembly)
+       {
+               Assembly a = GetAssembly (assembly, true);
+               foreach (AssemblyName an in a.GetReferencedAssemblies ())
+                       Console.WriteLine (an);
+       }
+
+       static void PrintTypes (string assembly, bool show_private, bool filter_obsolete)
        {
                Assembly a = GetAssembly (assembly, true);
 
@@ -202,16 +223,21 @@ class MonoP {
                        Console.WriteLine (ai.Trim ());
                        
                Console.WriteLine ();
-               Type [] types = a.GetExportedTypes ();
+               Type [] types = show_private ? a.GetTypes () : a.GetExportedTypes ();
                Array.Sort (types, new TypeSorter ());
 
-               foreach (Type t in types)
-                       Console.WriteLine (t.FullName);
+               int obsolete_count = 0;
+               foreach (Type t in types) {
+                       if (filter_obsolete && t.IsDefined (typeof (ObsoleteAttribute), false))
+                               obsolete_count ++;
+                       else
+                               Console.WriteLine (t.FullName);
+               }
 
-               Console.WriteLine ("\nTotal: {0} types.", types.Length);
+               Console.WriteLine ("\nTotal: {0} types.", types.Length - obsolete_count);
        }
        
-       static void Completion (string prefix)
+       internal static void Completion (string prefix)
        {
                foreach (Type t in typeof (object).Assembly.GetExportedTypes ()) {
                        if (t.Name.StartsWith (prefix)) {
@@ -250,88 +276,46 @@ class MonoP {
                
        }
 
-       static void PrintUsage ()
-       {
-               Console.WriteLine ("Usage is: monop [option] [-c] [-r:Assembly] [class-name]");
-       }
-
-       static void PrintHelp ()
-       {
-               PrintUsage ();
-               Console.WriteLine ("");
-               Console.WriteLine ("options:");
-               Console.WriteLine ("\t--declared-only,-d\tOnly show members declared in the Type");
-               Console.WriteLine ("\t--help,-h\t\tShow this information");
-               Console.WriteLine ("\t--private,-p\t\tShow private members");
-               Console.WriteLine ("\t--search,-s,-k\t\tSearch through all known namespaces");
-       }
-       
        static void Main (string [] args)
        {
-               if (args.Length < 1) {
-                       PrintUsage ();
-                       return;
-               }
-
-               if (args.Length == 1 && (args[0] == "--help" || args[0] == "-h"))
-               {
-                       PrintHelp ();
+               Options options = new Options ();
+               if (!options.ProcessArgs (args))
                        return;
-
-               }
                
-               int i = 0;
-               if (args [0].StartsWith ("-r:") || args [0].StartsWith ("/r:")){
-                       i++;
-                       assembly = args [0].Substring (3);
+               if (options.AssemblyReference != null) {
+                       assembly = options.AssemblyReference;
                        
-                       if (args.Length == 1) {
-                               PrintTypes (assembly);
+                       if (options.Type == null) {
+                               if (options.PrintRefs)
+                                       PrintRefs (assembly);
+                               else
+                                       PrintTypes (assembly, options.ShowPrivate, options.FilterObsolete);
                                return;
                        }
                }
-               
-               if (args [0] == "-c") {
-                       Completion (args [1]);
-                       return;
-               }
-
-               if (args [i] == "--private" || args [i] == "-p") {
-                       default_flags |= BindingFlags.NonPublic;
-                       i++;
-               }
-
-               if (args [i] == "--declared-only" || args [i] == "-d") {
-                       default_flags |= BindingFlags.DeclaredOnly;
-                       i++;
-               }
-
-               bool search = false;
-               if (args [i] == "--search" || args [i] == "-s" || args [i] == "-k") {
-                       search = true;
-                       i++;
-               }
-
-               if (args.Length < i + 1) {
-                       PrintUsage ();
-                       return;
-               }
 
                string message = null;
-               string tname = args [i];
+               string tname = options.Type;
+               Type t = null;
+               int count;
 
-               if (search){
-                       string matches = SearchTypes (tname);
+               if (options.Search) {
+                       string matches = SearchTypes (tname, ref t, out count);
 
-                       if (matches != null) {
-                               Console.WriteLine ("The follow types match:");
+                       if (count == 0)
+                               goto notfound;
+
+                       if (count == 1)
+                               goto found;
+                       
+                       if (count > 1){
+                               Console.WriteLine ("Found " + count + " types that match:");
                                Console.WriteLine (matches);
                                return;
-                       } else
-                               goto notfound;
+                       }
                }
                        
-               Type t = GetType (tname);
+               t = GetType (tname);
 
                if (t == null) {
                        // Try some very common ones, dont load anything
@@ -376,8 +360,8 @@ class MonoP {
                //
                // This gets us nice buffering
                //
-               StreamWriter sw = new StreamWriter (Console.OpenStandardOutput (), Console.Out.Encoding);
-               new Outline (t, sw).OutlineType (default_flags);
+               StreamWriter sw = new StreamWriter (Console.OpenStandardOutput (), Console.Out.Encoding);                               
+               new Outline (t, sw, options).OutlineType ();
                sw.Flush ();
 
                if (message != null)