/// <summary>
 /// Returns all kinds of models.
 /// </summary>
 public static string[] GetAllKinds()
 {
     using (var env = new ConsoleEnvironment())
     {
         ComponentHelper.AddStandardComponents(env);
         var sigs    = env.ComponentCatalog.GetAllSignatureTypes();
         var typeSig = sigs.Select(t => ComponentCatalog.SignatureToString(t).ToLowerInvariant());
         return(new HashSet <string>(typeSig.OrderBy(c => c)).ToArray());
     }
 }
        private void ShowAllHelp(IndentingTextWriter writer, int?columns = null)
        {
            string sig = _kind?.ToLowerInvariant();

            var infos = _env.ComponentCatalog.GetAllClasses()
                        .OrderBy(info => info.LoadNames[0].ToLowerInvariant())
                        .ThenBy(info => ComponentCatalog.SignatureToString(info.SignatureTypes[0]).ToLowerInvariant());
            var components = new List <Component>();

            foreach (var info in infos)
            {
                // REVIEW: We should only be printing the usage once, not for every signature.
                _env.AssertValue(info.SignatureTypes);
                foreach (var signature in info.SignatureTypes)
                {
                    _env.Assert(signature.BaseType == typeof(MulticastDelegate));

                    string kind = ComponentCatalog.SignatureToString(signature);
                    if (sig != null && !kind.StartsWithInvariantCultureIgnoreCase(sig))
                    {
                        continue;
                    }

                    // Don't show classes that have no arguments.
                    var args = info.CreateArguments();
                    if (args == null)
                    {
                        continue;
                    }

                    ShowUsage(writer, kind, info.Summary, info.LoadNames[0], info.LoadNames, args, columns);
                    components.Add(new Component(kind, info, args));
                }
            }

            if (components.Count > 0)
            {
                Serialize(components);
            }
        }
Exemplo n.º 3
0
 protected virtual string EnumName(CmdParser.ArgInfo.Arg arg, Type sigType)
 {
     return(Capitalize(arg.LongName) + ComponentCatalog.SignatureToString(sigType));
 }
        /// <summary>
        /// Returns all kinds of models.
        /// </summary>
        public static IEnumerable <ComponentDescription> EnumerateComponents(string kind)
        {
            if (kind == "argument")
            {
                foreach (var comp in EnumerateComponentsParameter(false))
                {
                    yield return(comp);
                }
            }
            else if (kind == "command")
            {
                foreach (var comp in EnumerateComponentsParameter(true))
                {
                    yield return(comp);
                }
            }
            else
            {
                var kinds = GetAllKinds();
                if (!string.IsNullOrEmpty(kind) && !kinds.Where(c => c == kind).Any())
                {
                    throw new ArgumentException($"Unable to find kind '{kind}' in\n{string.Join("\n", kinds)}.");
                }

                using (var env = new ConsoleEnvironment())
                {
                    ComponentHelper.AddStandardComponents(env);
                    var    sigs    = env.ComponentCatalog.GetAllSignatureTypes();
                    var    typeRes = typeof(object);
                    Type[] typeSigs;
                    if (string.IsNullOrEmpty(kind))
                    {
                        typeSigs = sigs.ToArray();
                    }
                    else
                    {
                        typeSigs = new[] { sigs.FirstOrDefault(t => ComponentCatalog.SignatureToString(t).ToLowerInvariant() == kind) }
                    };
                    foreach (var typeSig in typeSigs)
                    {
                        var infos = env.ComponentCatalog.GetAllDerivedClasses(typeRes, typeSig)
                                    .Where(x => !x.IsHidden)
                                    .OrderBy(x => x.LoadNames[0].ToLowerInvariant());
                        foreach (var info in infos)
                        {
                            var args = info.CreateArguments();
                            if (args == null)
                            {
                                yield return(new ComponentDescription(info, args, null, null));
                            }
                            else
                            {
                                var asse = args.GetType().Assembly;

                                var parsedArgs = CmdParser.GetArgInfo(args.GetType(), args).Args;
                                var arguments  = new List <ComponentDescription.Argument>();
                                foreach (var arg in parsedArgs)
                                {
                                    var a = new ComponentDescription.Argument()
                                    {
                                        Name         = arg.LongName,
                                        ShortName    = arg.ShortNames == null || !arg.ShortNames.Any() ? null : arg.ShortNames.First(),
                                        DefaultValue = arg.DefaultValue == null ? null : arg.DefaultValue.ToString(),
                                        Help         = arg.HelpText,
                                        Arg          = arg,
                                    };
                                    arguments.Add(a);
                                }

                                var cmp = new ComponentDescription(info, args, asse, arguments);
                                yield return(cmp);
                            }
                        }
                    }
                }
            }
        }

        #endregion
    }
        private void ShowComponents(IndentingTextWriter writer)
        {
            Type   typeSig;
            Type   typeRes;
            string kind;

            if (_kind == null)
            {
                // Show commands.
                typeSig = typeof(SignatureCommand);
                typeRes = typeof(ICommand);
                kind    = "Command";
                writer.WriteLine("Available commands:");
            }
            else
            {
                kind = _kind.ToLowerInvariant();
                var sigs = _env.ComponentCatalog.GetAllSignatureTypes();
                typeSig = sigs.FirstOrDefault(t => ComponentCatalog.SignatureToString(t).ToLowerInvariant() == kind);
                if (typeSig == null)
                {
                    typeSig = sigs.FirstOrDefault(t => ComponentCatalog.SignatureToString(t).StartsWithInvariantCultureIgnoreCase(kind));
                    if (typeSig == null)
                    {
                        writer.WriteLine("Couldn't find kind '{0}'", kind);
                        ListKinds(writer);
                        return;
                    }
                }
                typeRes = typeof(object);
                writer.WriteLine("Available components for kind '{0}':", ComponentCatalog.SignatureToString(typeSig));
            }

            var infos = _env.ComponentCatalog.GetAllDerivedClasses(typeRes, typeSig)
                        .Where(x => !x.IsHidden)
                        .OrderBy(x => x.LoadNames[0].ToLowerInvariant());

            using (writer.Nest())
            {
                var components = new List <Component>();
                foreach (var info in infos)
                {
                    _env.Assert(info.LoadNames.Count > 0);

                    writer.Write("{0}", info.LoadNames[0]);
                    if (!string.IsNullOrWhiteSpace(info.UserName))
                    {
                        writer.Write(": {0}", info.UserName);
                    }
                    writer.WriteLine();

                    using (writer.Nest())
                        ShowAliases(writer, info.LoadNames);
                    components.Add(new Component(kind, info, info.CreateArguments()));
                }

                if (components.Count > 0)
                {
                    Serialize(components);
                }
            }
        }
        private void ShowHelp(IndentingTextWriter writer, int?columns = null)
        {
            _env.AssertValue(_component);

            string name = _component.Trim();

            string sig = _kind?.ToLowerInvariant();

            // Note that we don't check IsHidden here. The current policy is when IsHidden is true, we don't
            // show the item in "list all" functionality, but will still show help when explicitly requested.

            var infos = _env.ComponentCatalog.FindLoadableClasses(name)
                        .OrderBy(x => ComponentCatalog.SignatureToString(x.SignatureTypes[0]).ToLowerInvariant());
            var kinds      = new StringBuilder();
            var components = new List <Component>();

            foreach (var info in infos)
            {
                _env.AssertValue(info.SignatureTypes);
                kinds.Clear();
                bool foundSig = false;
                foreach (var signature in info.SignatureTypes)
                {
                    _env.Assert(signature.BaseType == typeof(MulticastDelegate));

                    string kind;
                    if (signature == typeof(SignatureDefault))
                    {
                        kind = "Component";
                        if (sig == null || "default".StartsWithInvariantCulture(sig))
                        {
                            foundSig = true;
                        }
                    }
                    else
                    {
                        kind = ComponentCatalog.SignatureToString(signature);
                        if (sig == null || kind.StartsWithInvariantCultureIgnoreCase(sig))
                        {
                            foundSig = true;
                        }
                    }

                    if (kinds.Length > 0)
                    {
                        kinds.Append(", ");
                    }
                    kinds.Append(kind);
                }
                if (foundSig)
                {
                    string kindsStr = kinds.ToString();
                    var    args     = info.CreateArguments();

                    ShowUsage(writer, kindsStr, info.Summary, info.LoadNames[0], info.LoadNames, args, columns);
                    components.Add(new Component(kindsStr, info, args));
                }
            }

            if (components.Count == 0)
            {
                writer.WriteLine("Unknown component: '{0}'", name);
            }
            else
            {
                Serialize(components);
            }
        }