Example #1
0
        public CommandResult <List <IDataObject> > Vars(
            CommandEvaluationContext context,
            [Parameter(0, "variable namespace or value path below the root namespace. if specified and exists, output is built from this point, otherwise outputs all variables from env root", true)] string varPath,
            [Option("u", "unfold-namespace", "unfold namespaces")] bool unfoldNamespaces     = false,
            [Option("o", "unfold-value", "unfold values of type object")] bool unfoldObjects = false,
            [Option("p", "parse", "echo string values in parsed mode (ansi and directives). By default strings objects are represented by raw text")] bool parsed = false
            )
        {
            object obj;

            if (varPath == null)
            {
                obj = context.Variables.RootObject;
            }
            else
            {
                context.Variables.GetObject(varPath, out obj);
            }

            var options = new TableFormattingOptions(context.ShellEnv.TableFormattingOptions)
            {
                UnfoldCategories = unfoldNamespaces,
                UnfoldItems      = unfoldObjects,
                IsRawModeEnabled = !parsed
            };

            if (obj is DataValue value)
            {
                var lst = new List <IDataObject>()
                {
                    value
                };
                var resultValue = new CommandResult <List <IDataObject> >(lst);
                var wrapper     = new DataObject("");
                wrapper.Set(new string[] { "x" }, value);
                wrapper.Echo(new EchoEvaluationContext(context.Out, context, options));
                return(resultValue);
            }
            else
            {
                if (obj is IDataObject envVars)
                {
                    var values = envVars.GetAttributes();
                    envVars.Echo(new EchoEvaluationContext(context.Out, context, options));
                    return(new CommandResult <List <IDataObject> >(values));
                }
                else
                {
                    // directly dump object members
                    EchoPrimitives.DumpObject(obj, new EchoEvaluationContext(context.Out, context, options));
                    return(new CommandResult <List <IDataObject> >(null));
                }
            }
        }
        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
        }