Example #1
0
        void PrintCommandHelp(
            CommandEvaluationContext context,
            CommandSpecification com,
            bool shortView       = false,
            bool list            = false,
            int maxcnamelength   = -1,
            int maxcmdtypelength = -1,
            int maxmodlength     = -1,
            bool singleout       = false)
        {
#pragma warning disable IDE0071                  // Simplifier l’interpolation
#pragma warning disable IDE0071WithoutSuggestion // Simplifier l’interpolation
            if (maxcnamelength == -1)
            {
                maxcnamelength = com.Name.Length + 1;
            }
            if (maxcmdtypelength == -1)
            {
                maxcmdtypelength = com.DeclaringTypeShortName.Length + 1;
            }
            var col = singleout? "": "".PadRight(maxcnamelength, ' ');
            var f   = GetCmd(EchoDirectives.f + "", DefaultForeground.ToString().ToLower());
            if (list)
            {
                if (!shortView)
                {
                    context.Out.Echoln($"{Darkcyan}{com.ModuleName.PadRight(maxmodlength, ' ')}   {com.DeclaringTypeShortName.PadRight(maxcmdtypelength, ' ')}{Tab}{ColorSettings.Highlight}{com.Name.PadRight(maxcnamelength, ' ')}{Tab}{f}{com.Description}{ColorSettings.Default}");
                }
                else
                {
                    context.Out.Echoln($"{ColorSettings.Highlight}{com.Name.PadRight(maxcnamelength, ' ')}{f}{Tab}{com.Description}{ColorSettings.Default}");
                }
            }
            else
            {
                if (singleout)
                {
                    context.Out.Echoln(com.Description);
                    if (com.ParametersCount > 0)
                    {
                        context.Out.Echo($"{Br}{col}{ColorSettings.Label}syntax: {f}{com.ToColorizedString()}{(!shortView ? Br : "")}");
                    }
                    context.Out.Echoln(GetPrintableDocText(com.LongDescription, list, shortView, 0));
                }
                else
                {
                    context.Out.Echoln($"{com.Name.PadRight(maxcnamelength, ' ')}{com.Description}");
                    if (com.ParametersCount > 0)
                    {
                        context.Out.Echo($"{Br}{col}{ColorSettings.Label}syntax: {f}{com.ToColorizedString()}{(!shortView ? Br : "")}");
                    }
                    context.Out.Echo(GetPrintableDocText(com.LongDescription, list, shortView, maxcnamelength));
                }
            }

            if (!list)
            {
                if (com.ParametersCount > 0)
                {
                    if (!shortView)
                    {
                        var mpl = com.ParametersSpecifications.Values.Select(x => x.Dump(false).Length).Max() + TabLength;
                        foreach (var p in com.ParametersSpecifications.Values)
                        {
                            var ptype  = (!p.IsOption && p.HasValue) ? $"of type: {Darkyellow}{p.ParameterInfo.ParameterType.Name}{f}" : "";
                            var pdef   = (p.HasValue && p.IsOptional && p.HasDefaultValue && p.DefaultValue != null && (!p.IsOption || p.ParameterValueTypeName != typeof(bool).Name)) ? ((ptype != ""?". ":"") + $"default value: {Darkyellow}{DumpAsText(p.DefaultValue)}{f}") : "";
                            var supdef = $"{ptype}{pdef}";
                            context.Out.Echoln($"{col}{Tab}{p.ToColorizedString(false)}{"".PadRight(mpl - p.Dump(false).Length, ' ')}{p.Description}");
                            if (!string.IsNullOrWhiteSpace(supdef))
                            {
                                context.Out.Echoln($"{col}{Tab}{" ".PadRight(mpl)}{supdef}");
                            }
                        }

                        if (string.IsNullOrWhiteSpace(com.Documentation))
                        {
                            context.Out.Echoln();
                        }
                        context.Out.Echo(GetPrintableDocText(com.Documentation, list, shortView, singleout ? 0 : maxcnamelength));
                    }
                    else
                    {
                        context.Out.Echoln(GetPrintableDocText(com.Documentation, list, shortView, singleout ? 0 : maxcnamelength));
                    }
                }
                if (!shortView)
                {
                    context.Out.Echoln($"{col}{ColorSettings.Label}type  : {ColorSettings.DarkLabel}{com.DeclaringTypeShortName}");
                    context.Out.Echoln($"{col}{ColorSettings.Label}module: {ColorSettings.DarkLabel}{com.ModuleName}{ColorSettings.Default}");
                }
            }
#pragma warning restore IDE0071WithoutSuggestion // Simplifier l’interpolation
#pragma warning restore IDE0071                  // Simplifier l’interpolation
        }
        void _PrintCommandHelp(
            CommandEvaluationContext context,
            CommandSpecification com,
            bool shortView       = false,
            bool verboseView     = false,
            bool list            = false,
            int maxnslength      = -1,
            int maxcnamelength   = -1,
            int maxcmdtypelength = -1,
            int maxmodlength     = -1,
            bool singleout       = false
            )
        {
#pragma warning disable IDE0071                  // Simplifier l’interpolation
#pragma warning disable IDE0071WithoutSuggestion // Simplifier l’interpolation
            if (maxcnamelength == -1)
            {
                maxcnamelength = com.Name.Length + 1;
            }
            if (maxnslength == -1)
            {
                maxnslength = com.Namespace.Length + 1;
            }
            if (maxcmdtypelength == -1)
            {
                maxcmdtypelength = com.DeclaringTypeShortName.Length + 1;
            }
            var col = singleout ? "" : "".PadRight(maxcnamelength, ' ');
            var f   = GetCmd(EchoDirectives.f + "", context.CommandLineProcessor.Console.DefaultForeground.ToString().ToLower());
            if (list)
            {
                if (!shortView)
                {
                    context.Out.Echoln($"{Darkcyan}{com.ModuleName.PadRight(maxmodlength, ' ')}   {com.DeclaringTypeShortName.PadRight(maxcmdtypelength, ' ')}   {com.Namespace.PadRight(maxnslength, ' ')}{Tab}{context.ShellEnv.Colors.Highlight}{com.Name.PadRight(maxcnamelength, ' ')}{Tab}{f}{com.Description}{context.ShellEnv.Colors.Default}");
                }
                else
                {
                    context.Out.Echoln($"{context.ShellEnv.Colors.Highlight}{com.Name.PadRight(maxcnamelength, ' ')}{f}{Tab}{com.Description}{context.ShellEnv.Colors.Default}");
                }
            }
            else
            {
                bool hasrtt   = com.ReturnType != null;
                bool hasalias = com.Aliases != null;
                if (singleout)
                {
                    context.Out.Echoln(com.Description);
                    context.Out.Echo($"{Br}{col}{context.ShellEnv.Colors.Label}syntax: {f}{com.ToColorizedString(context.ShellEnv.Colors)}", hasrtt | hasalias);
                    if (hasrtt)
                    {
                        context.Out.Echoln($"{col}{context.ShellEnv.Colors.Label}returns: {context.ShellEnv.Colors.TypeName}{com.ReturnType.UnmangledName()}");
                    }
                    if (hasalias)
                    {
                        context.Out.Echoln($"{col}{context.ShellEnv.Colors.Label}aliases: {context.ShellEnv.Colors.TypeName}{string.Join(",", com.Aliases.Select(x => x.name))}");
                    }
                    context.Out.Echo(!shortView ? Br : "");
                    if (!string.IsNullOrWhiteSpace(com.LongDescription))
                    {
                        context.Out.Echoln(_GetPrintableDocText(com.LongDescription, list, shortView, 0));
                    }
                }
                else
                {
                    context.Out.Echoln($"{com.Name.PadRight(maxcnamelength, ' ')}{com.Description}");
                    context.Out.Echoln($"{Br}{col}{context.ShellEnv.Colors.Label}syntax: {f}{com.ToColorizedString(context.ShellEnv.Colors)}");
                    if (hasrtt)
                    {
                        context.Out.Echoln($"{col}{context.ShellEnv.Colors.Label}returns: {context.ShellEnv.Colors.TypeName}{com.ReturnType.UnmangledName()}");
                    }
                    if (hasalias)
                    {
                        context.Out.Echoln($"{col}{context.ShellEnv.Colors.Label}aliases: {context.ShellEnv.Colors.TypeName}{string.Join(",", com.Aliases.Select(x => x.name))}");
                    }
                    context.Out.Echo(!shortView ? Br : "");
                    if (!string.IsNullOrWhiteSpace(com.LongDescription))
                    {
                        context.Out.Echo(_GetPrintableDocText(com.LongDescription + "(br)", list, shortView, maxcnamelength));
                    }
                }
            }

            if (!list)
            {
                if (com.ParametersCount > 0)
                {
                    if (!shortView)
                    {
                        var mpl = (com.ParametersCount > 0 ? com.ParametersSpecifications.Values.Select(x => x.Dump(false).Length).Max() : 0) + context.CommandLineProcessor.Console.TabLength;
                        foreach (var p in com.ParametersSpecifications.Values)
                        {
                            var ptype = (!p.IsOption && p.HasValue) ? $"of type: {context.ShellEnv.Colors.TypeName}{p.ParameterInfo.ParameterType.UnmangledName()}{f}" : "";
                            var pdef  = (p.HasValue && p.IsOptional && p.HasDefaultValue && p.DefaultValue != null &&
                                         (!p.IsOption || p.ParameterValueTypeName != typeof(bool).Name)) ?
                                        ((ptype != "" ? ". " : "") + $"default value: {context.ShellEnv.Colors.OptionValue}{EchoPrimitives.DumpAsText(context, p.DefaultValue)}{f}")
                                        : "";

                            var pleftCol = "".PadRight(mpl - p.Dump(false).Length, ' ');
                            var leftCol  = "".PadRight(mpl, ' ');

                            if (p.ParameterInfo.ParameterType.IsEnum)
                            {
                                pdef += $"(br){col}{Tab}{leftCol}possibles values: {context.ShellEnv.Colors.OptionValue}{string.Join(CommandLineSyntax.ParameterTypeListValuesSeparator, Enum.GetNames(p.ParameterInfo.ParameterType))}(rdc)";
                            }

                            var supdef = $"{ptype}{pdef}";
                            // method 'Echo if has' else to string
                            context.Out.Echoln($"{col}{Tab}{p.ToColorizedString(context.ShellEnv.Colors, false)}{pleftCol}{p.Description}");
                            if (!string.IsNullOrWhiteSpace(supdef))
                            {
                                context.Out.Echoln($"{col}{Tab}{" ".PadRight(mpl)}{supdef}");
                            }
                        }

                        //if (string.IsNullOrWhiteSpace(com.Documentation)) context.Out.Echoln();
                        if (!string.IsNullOrWhiteSpace(com.Documentation))
                        {
                            context.Out.Echo(_GetPrintableDocText("(br)" + com.Documentation, list, shortView, singleout ? 0 : maxcnamelength));
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrWhiteSpace(com.Documentation))
                        {
                            context.Out.Echoln(_GetPrintableDocText("(br)" + com.Documentation, list, shortView, singleout ? 0 : maxcnamelength));
                        }
                    }
                }
                if (verboseView)
                {
                    if (com.ParametersCount > 0)
                    {
                        context.Out.Echoln("");
                    }
                    context.Out.Echoln($"{col}{context.ShellEnv.Colors.Label}namespace       : {context.ShellEnv.Colors.HalfDarkLabel}{com.Namespace}");
                    context.Out.Echoln($"{col}{context.ShellEnv.Colors.Label}declaring type  : {context.ShellEnv.Colors.HalfDarkLabel}{com.DeclaringTypeShortName}");
                    context.Out.Echoln($"{col}{context.ShellEnv.Colors.Label}module          : {context.ShellEnv.Colors.HalfDarkLabel}{com.ModuleName}{context.ShellEnv.Colors.Default}");
                }
            }
#pragma warning restore IDE0071WithoutSuggestion // Simplifier l’interpolation
#pragma warning restore IDE0071                  // Simplifier l’interpolation
        }