예제 #1
0
        static int Main(string[] args)
        {
            int returnMain = 0; // C good

            int leaveOut = 0;
            var dict     = new Dictionary <string, string>();

            FieldInfo[] fields = typeof(Program).GetFields(BindingFlags.Public | BindingFlags.Static).OrderBy(field => field.Name).ToArray();
            foreach (string arg in args)
            {
                if (!arg.StartsWith("--", StringComparison.OrdinalIgnoreCase))
                {
                    break;
                }

                leaveOut++;

                int    iOfEquals = arg.IndexOf('=');
                string key, value;
                if (-1 < iOfEquals)
                {
                    key   = arg.Substring(0, iOfEquals);
                    value = arg.Substring(iOfEquals + 1);
                }
                else
                {
                    key   = arg.Substring(0);
                    value = null;
                }
                dict.Add(key, value);

                FieldInfo keyField = fields.Where(field => string.Equals(field.Name, key.Substring(2), StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                if (keyField != null)
                {
                    if (keyField.FieldType == typeof(string))
                    {
                        keyField.SetValue(null, value);
                    }
                    else if (keyField.FieldType == typeof(bool))
                    {
                        bool equalsTrue = (value == null || value == string.Empty);
                        if (!equalsTrue)
                        {
                            equalsTrue = string.Equals(value, "true", StringComparison.OrdinalIgnoreCase);
                        }
                        if (!equalsTrue)
                        {
                            equalsTrue = string.Equals(value, "+", StringComparison.OrdinalIgnoreCase);
                        }

                        keyField.SetValue(null, equalsTrue);
                    }
                }
            }

            var    methods = typeof(Program).GetMethods(BindingFlags.Public | BindingFlags.Static).OrderBy(method => method.Name).ToList();
            string command = (args.Length == 0) ? "help" : args[0 + leaveOut].ToLower();

            if (command != "help")
            {
                var method = methods.FirstOrDefault(m => m.Name.Equals(command, StringComparison.OrdinalIgnoreCase));
                if (method != null)
                {
                    object[]        parameters = null;
                    ParameterInfo[] arguments  = method.GetParameters();

                    if (arguments.Length == 2)
                    {
                        parameters = new object[] {
                            dict, args.Skip(1 + leaveOut).ToArray()                             /* string[] args */
                        };
                    }
                    else if (arguments.Length == 1)
                    {
                        parameters = new object[] {
                            args.Skip(1 + leaveOut).ToArray()                             /* string[] args */
                        };
                    }

                    object result = DebugStackTrace <TargetInvocationException> .Invoke(
                        method, /* static */ null, parameters);

                    if (method.ReturnType == typeof(bool))
                    {
                        return((bool)result ? 0 : 1);
                    }
                    if (method.ReturnType == typeof(int))
                    {
                        return((int)result);
                    }
                    return(0); // method.ReturnType == typeof(Void)
                }

                returnMain = 1;                 // C bad
                Console.WriteLine();
                Console.WriteLine("Command invalid.");
            }

            Console.WriteLine();
            Console.WriteLine("Usage: ./" + AppDomain.CurrentDomain.FriendlyName + " [--option] <command> World");

            if (fields.Length > 0)
            {
                Console.WriteLine();
                Console.WriteLine("Available [option]s:");
                Console.WriteLine();
                foreach (FieldInfo field in fields)
                {
                    Console.WriteLine("  --{0}", field.Name);
                }
            }

            Console.WriteLine();
            Console.WriteLine("Available <command>s:");
            Console.WriteLine();

            foreach (MethodInfo method in methods)
            {
                if (method.Name == "Main")
                {
                    continue;
                }
                if (0 < method.GetCustomAttributes(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), true).Length)
                {
                    continue;
                }

                Console.WriteLine("    {0}", method.Name);
            }

            Console.WriteLine();
            return(returnMain);
        }
예제 #2
0
        static int Main(string[] args)
        {
            // REAL
            var fields = typeof(Program).GetFields(BindingFlags.Public | BindingFlags.Static).OrderBy(field => field.Name).ToList();

            int leaveOut = 0;
            var dict     = new Dictionary <string, string>();

            if (args != null && args.Length > 0)
            {
                foreach (string arg in args)
                {
                    if (arg.StartsWith("--"))
                    {
                        leaveOut++;

                        int    iOfEquals = arg.IndexOf('=');
                        string key, value;
                        if (-1 < iOfEquals)
                        {
                            key   = arg.Substring(0, iOfEquals);
                            value = arg.Substring(iOfEquals + 1);
                        }
                        else
                        {
                            key   = arg.Substring(0);
                            value = null;
                        }
                        dict.Add(key, value);

                        FieldInfo keyField = fields.Where(field => string.Equals(field.Name, key.Substring(2), StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                        if (keyField != null)
                        {
                            if (keyField.FieldType == typeof(string))
                            {
                                keyField.SetValue(null, value);
                            }
                            else if (keyField.FieldType == typeof(bool))
                            {
                                bool equalsTrue = (value == null || value == string.Empty);
                                if (!equalsTrue)
                                {
                                    equalsTrue = string.Equals(value, "true", StringComparison.OrdinalIgnoreCase);
                                }
                                if (!equalsTrue)
                                {
                                    equalsTrue = string.Equals(value, "+", StringComparison.OrdinalIgnoreCase);
                                }

                                keyField.SetValue(null, equalsTrue);
                            }
                        }
                    }
                }
            }

            int    returnMain = 0;
            string command    = (args == null || args.Length == 0) ? "help" : args[0 + leaveOut].ToLower();

            var methods = typeof(Program).GetMethods(BindingFlags.Public | BindingFlags.Static).OrderBy(method => method.Name).ToList();

            if (command != "help")
            {
                var method = methods.FirstOrDefault(m => m.Name.Equals(command, StringComparison.OrdinalIgnoreCase));
                if (method != null)
                {
                    ParameterInfo[] methodParameters = method.GetParameters();

                    object[] parameters;

                    if (methodParameters.Length == 2)
                    {
                        parameters = new object[] {
                            dict,
                            args.Skip(1 + leaveOut).ToArray()                             /* string[] args */
                        };
                    }
                    else if (methodParameters.Length == 1)
                    {
                        parameters = new object[] {
                            args.Skip(1 + leaveOut).ToArray()                             /* string[] args */
                        };
                    }
                    else
                    {
                        throw new InvalidOperationException();
                    }

                    // INFO: Invoking the Sample by "the Delegate.Invoke" makes it hard to debug!
                    // Using DebugInvoke
                    object result
                        = DebugStackTrace <TargetInvocationException> .Invoke(
                              method,
                              null,
                              parameters);

                    if (method.ReturnType == typeof(bool) && true == (bool)result)
                    {
                        return(0);                        // C good
                    }

                    return(-1);                    // C bad
                }

                returnMain = -1;
                Console.WriteLine();
                Console.WriteLine("Command invalid.");
            }

            Console.WriteLine();
            Console.WriteLine("Usage: ./" + AppDomain.CurrentDomain.FriendlyName + " [--option=++] [--option=tcp://192.168.1.1:8080] <command> World Me You");

            if (fields.Count > 0)
            {
                Console.WriteLine();
                Console.WriteLine("Available [option]s:");
                Console.WriteLine();
                foreach (FieldInfo field in fields)
                {
                    Console.WriteLine("  --{0}", field.Name);
                }
            }

            Console.WriteLine();
            Console.WriteLine("Available <command>s:");
            Console.WriteLine();

            foreach (MethodInfo meth in methods)
            {
                if (meth.Name == "Main")
                {
                    continue;
                }
                if (0 < meth.GetCustomAttributes(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), true).Length)
                {
                    continue;
                }

                Console.WriteLine("    {0}", meth.Name);
            }

            Console.WriteLine();
            return(returnMain);
        }