예제 #1
0
        public static IColorConsole WriteHelp(this IColorConsole con, ClassDefinition cd)
        {
            foreach (var method in cd.IncludedMethods)
            {
                con.w(ExeColor, "{0}", cd.ExeName).w(CommandColor, " {0}", method.Command);
                foreach (var group in method.Parameters.GroupBy(p => !p.HasDefaultValue))
                {
                    bool optionals = group.Key;
                    foreach (var parameter in group)
                    {
                        con.WriteShortHelp(parameter);
                    }
                }
                con.wl();
            }

            if (cd.CommonParameters.Length > 0)
            {
                con.w("Common parameters:");
                foreach (var parameter in cd.CommonParameters)
                {
                    con.WriteShortHelp(parameter);
                }
                con.wl();
            }
            return(con);
        }
예제 #2
0
        static IColorConsole WriteHelp(this IColorConsole con, CmdLineParameter p)
        {
            bool optional = p.HasDefaultValue;
            Type type     = p.Type;
            bool isFlag   = type.IsEquivalentTo(typeof(Boolean));
            var  color    = optional || isFlag ? OptionalParameterColor : ParameterColor;

            con.w(Indent).w(color, p.Name.PadRight(7)).w(" - {0}", type.ToDisplayName());
            if (p.HelpText != null)
            {
                con.w("{0}", p.HelpText);
            }

            if (type.IsEquivalentTo(typeof(Boolean)))
            {
                if (optional)
                {
                    con.w("(default=").w(DefaultValueColor, "{0}", p.DefaultValueToDisplayString()).w(")");
                }
            }
            else
            {
                Type nullable = type.IsGenericType && type.FullName.StartsWith("System.Nullable`1[") ? type.GetGenericArguments()[0] : null;
                if (optional)
                {
                    if (nullable == null && !type.IsEquivalentTo(typeof(string)) || p.DefaultValue != null)
                    {
                        con.w(" (default=").w(DefaultValueColor, "{0}", p.DefaultValueToDisplayString()).w(")");
                    }
                    //con.w("]");
                }
            }
            con.wl();
            return(con);
        }
예제 #3
0
        public static IColorConsole WriteHelp(this IColorConsole con, ClassDefinition cd, string command)
        {
            var methods = cd.FindMethods(command);

            if (methods.Length == 0)
            {
                con.w("Help for ").w(ConsoleColor.Red, "{0}", command).wl(" not found");
            }
            else
            {
                char argStartsWith    = cd.Config.ArgStartsWith;
                char argSeparator     = cd.Config.ArgSeparator;
                char argListSeparator = cd.Config.ArgListSeparator;
                foreach (var method in methods)
                {
                    con.w(ExeColor, "{0}", cd.ExeName);
                    con.w(CommandColor, " {0}", method.Command);
                    foreach (var group in method.Parameters.GroupBy(p => !p.HasDefaultValue))
                    {
                        bool optionals = group.Key;
                        foreach (var parameter in group)
                        {
                            con.WriteShortHelp(parameter);
                        }
                    }
                    con.wl();
                    if (method.HelpText != null)
                    {
                        con.w(Indent).wl("{0}", method.HelpText);
                    }
                    foreach (var group in method.Parameters.GroupBy(p => !p.HasDefaultValue))
                    {
                        bool optionals = group.Key;
                        foreach (var parameter in group)
                        {
                            con.WriteHelp(parameter);
                        }
                    }
                    //con.wl();
                }
            }
            if (cd.CommonParameters.Length > 0)
            {
                con.wl("Common parameters:");
                foreach (var parameter in cd.CommonParameters)
                {
                    con.WriteHelp(parameter);
                }
            }
            return(con);
        }
예제 #4
0
        static IColorConsole WriteShortHelp(this IColorConsole con, CmdLineParameter p)
        {
            bool optional = p.HasDefaultValue;
            Type type     = p.Type;
            var  color    = optional ? OptionalParameterColor : ParameterColor;

            con.w(" ");
            if (type.IsEquivalentTo(typeof(Boolean)))
            {
                con.w("[{0}", p.Config.ArgStartsWith).w(OptionalParameterColor, p.Name).w("]");
                if (optional)
                {
                    con.w("(default=").w(DefaultValueColor, "{0}", p.DefaultValueToDisplayString()).w(")");
                }
            }
            else
            {
                Type nullable = type.IsGenericType && type.FullName.StartsWith("System.Nullable`1[") ? type.GetGenericArguments()[0] : null;
                if (optional)
                {
                    con.w("[");
                }
                con.w("{0}", p.Config.ArgStartsWith).w(color, p.Name).w("=").w("{0}", nullable != null ? nullable.ToDisplayString() : p.TypeToDisplayString());
                if (optional)
                {
                    if (nullable == null && !type.IsEquivalentTo(typeof(string)) || p.DefaultValue != null)
                    {
                        con.w("(default=").w(DefaultValueColor, "{0}", p.DefaultValueToDisplayString()).w(")");
                    }
                    con.w("]");
                }
            }
            return(con);
        }
예제 #5
0
        static void WriteConsoleImplementation(IColorConsole con, string prefix, bool doBackground)
        {
            string text = doBackground ? "ab" : "\u2588\u2588";

            Console.Write("{0}:", prefix);
            for (int i = 0; i < 16; i++)
            {
                if (doBackground)
                {
                    con.w((ConsoleColor)(-1), (ConsoleColor)i, $"{i,2}{text}");
                }
                else
                {
                    con.w((ConsoleColor)i, $"{i,2}{text}");
                }
            }
            con.wl();
        }
예제 #6
0
 public static IColorConsole WriteAppHeader(this IColorConsole con, ClassDefinition cd)
 {
     if (!string.IsNullOrEmpty(cd.AppTitle) && cd.Version != null && !string.IsNullOrEmpty(cd.Copyright))
     {
         con.w("{0}", cd.AppTitle).w(ConsoleColor.Green, $" {cd.Version}").wl($" {cd.Copyright}");
     }
     if (cd.HelpText != null)
     {
         con.wl("{0}", cd.HelpText);
     }
     else if (!string.IsNullOrEmpty(cd.Description))
     {
         con.wl("{0}", cd.Description);
     }
     return(con);
 }
예제 #7
0
        static public void DumpCmdLineTypeInfo(bool dumpAll = false)
        {
            IColorConsole con        = ColorConsole.Default;
            Type          type       = typeof(T);
            var           classAttrs = type.GetCustomAttributes(false).Where(a => a is CmdLineClassAttribute).ToList();

            if (classAttrs.Count == 0)
            {
                con.wl(ConsoleColor.Red, "{0} is not CmdLine class", type.Name);
                return;
            }
            foreach (var attr in classAttrs)
            {
                con.wl("{0}: {1}", type.Name, attr.GetType().Name);
            }
            foreach (var method in type.GetMethods())
            {
                var  attrs           = method.GetCustomAttributes(false);
                var  methodAttrs     = attrs.Where(a => a is CmdLineMethodAttribute).ToList();
                bool isCmdLineMethod = methodAttrs.Count > 0;
                var  methodColor     = isCmdLineMethod ? ConsoleColor.Cyan : ConsoleColor.DarkGray;
                if (isCmdLineMethod || dumpAll)
                {
                    con.w(methodColor, "{0}()", method.Name);
                    if (isCmdLineMethod && methodAttrs.Count != 1)
                    {
                        con.wl(ConsoleColor.Yellow, ": too many attributes {0}", methodAttrs.Count);
                    }
                    else
                    {
                        con.wl();
                    }
                    foreach (var parameter in method.GetParameters())
                    {
                        var attrs1     = parameter.GetCustomAttributes(false);
                        var paramAttrs = attrs1.Where(a => a is CmdLineArgAttribute).ToList();
                        var paramColor = isCmdLineMethod ? ConsoleColor.White : ConsoleColor.DarkGray;
                        if (isCmdLineMethod)
                        {
                            con.w(paramColor, "  {0}", parameter.Name);
                            con.w("[{0}]", parameter.ParameterType.Name);
                            if (paramAttrs.Count == 0)
                            {
                                con.wl(ConsoleColor.Yellow, "  {0}: not a CmdLine parameter", parameter.Name);
                            }
                            else if (paramAttrs.Count != 1)
                            {
                                con.wl(ConsoleColor.Yellow, "  {0}: too many CmdLine parameters {1}", parameter.Name, paramAttrs.Count);
                            }
                            else
                            {
                                var attr = attrs1[0];
                                var a    = attr as CmdLineArgAttribute;
                                if (a != null)
                                {
                                    con.wl(": {0} - {1}", a.ArgName, a.HelpText);
                                }
                                else
                                {
                                    con.wl(": {0}", a.HelpText);
                                }
                            }
                        }
                        else if (dumpAll)
                        {
                            con.wl(paramColor, "  {0}[{1}]", parameter.Name, parameter.ParameterType.Name);
                        }
                    }
                }
            }

            /*
             * foreach (var attr in type.GetCustomAttributes(false).Where(a => a is CmdLineClassAttribute))
             * {
             *  con.wl(ConsoleColor.Yellow, "{0}", attr.GetType().Name);
             * }*/
        }