Esempio n. 1
0
        /// <summary>
        /// Generate the module and its implementation.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="prefix">The module prefix.</param>
        /// <param name="regenerate">The command string used to generate.</param>
        /// <param name="component">The component.</param>
        /// <param name="moduleId"></param>
        /// <param name="moduleName"></param>
        /// <param name="moduleOwner"></param>
        /// <param name="moduleVersion"></param>
        /// <param name="moduleState"></param>
        /// <param name="moduleType"></param>
        /// <param name="moduleDeterminism"></param>
        /// <param name="moduleCategory"></param>
        /// <param name="exclude">The set of parameters to exclude</param>
        /// <param name="namespaces">The set of extra namespaces</param>
        public void Generate(IndentingTextWriter writer, string prefix, string regenerate, ComponentCatalog.LoadableClassInfo component,
                             string moduleId, string moduleName, string moduleOwner, string moduleVersion, string moduleState, string moduleType, string moduleDeterminism, string moduleCategory,
                             HashSet <string> exclude, HashSet <string> namespaces)
        {
            Contracts.AssertValue(exclude);
            Name        = moduleName;
            Owner       = moduleOwner;
            Version     = moduleVersion;
            State       = moduleState;
            ModuleType  = moduleType;
            Determinism = moduleDeterminism;
            Category    = moduleCategory;
            Exclude     = exclude;
            Namespaces  = namespaces;

            GenerateHeader(writer, regenerate);
            using (writer.Nest())
            {
                GenerateClassName(writer, prefix, component);
                using (writer.Nest())
                    GenerateContent(writer, prefix, component, moduleId);
                GenerateFooter(writer);
            }
            GenerateFooter(writer);
        }
        private static void ShowMetadata(IndentingTextWriter itw, ISchema schema, int col, bool showVals)
        {
            Contracts.AssertValue(itw);
            Contracts.AssertValue(schema);
            Contracts.Assert(0 <= col && col < schema.ColumnCount);

            using (itw.Nest())
            {
                foreach (var kvp in schema.GetMetadataTypes(col).OrderBy(p => p.Key))
                {
                    Contracts.AssertNonEmpty(kvp.Key);
                    Contracts.AssertValue(kvp.Value);
                    var type = kvp.Value;
                    itw.Write("Metadata '{0}': {1}", kvp.Key, type);
                    if (showVals)
                    {
                        if (!type.IsVector)
                        {
                            ShowMetadataValue(itw, schema, col, kvp.Key, type);
                        }
                        else
                        {
                            ShowMetadataValueVec(itw, schema, col, kvp.Key, type);
                        }
                    }
                    itw.WriteLine();
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Process a script to be parsed (from the input resource).
        /// </summary>
        private static void Process(IndentingTextWriter wrt, string text, ArgsBase defaults)
        {
            var env = new TlcEnvironment(seed: 42);

            using (wrt.Nest())
            {
                var args1 = defaults.Clone();
                using (wrt.Nest())
                {
                    if (!CmdParser.ParseArguments(env, text, args1, s => wrt.WriteLine("*** {0}", s)))
                    {
                        wrt.WriteLine("*** Failed!");
                    }
                }
                string str1 = args1.ToString();
                wrt.WriteLine("ToString: {0}", str1);
                string settings1 = CmdParser.GetSettings(env, args1, defaults, SettingsFlags.None);
                wrt.WriteLine("Settings: {0}", settings1);

                var args2 = defaults.Clone();
                using (wrt.Nest())
                {
                    if (!CmdParser.ParseArguments(env, settings1, args2, s => wrt.WriteLine("*** BUG: {0}", s)))
                    {
                        wrt.WriteLine("*** BUG: parsing result of GetSettings failed!");
                    }
                }
                string str2 = args2.ToString();
                if (str1 != str2)
                {
                    wrt.WriteLine("*** BUG: ToString Mismatch: {0}", str2);
                }
                string settings2 = CmdParser.GetSettings(env, args2, defaults, SettingsFlags.None);
                if (settings1 != settings2)
                {
                    wrt.WriteLine("*** BUG: Settings Mismatch: {0}", settings2);
                }
            }
        }
        private void ListKinds(IndentingTextWriter writer)
        {
            var sigs = _env.ComponentCatalog.GetAllSignatureTypes()
                       .Select(ComponentCatalog.SignatureToString)
                       .OrderBy(x => x);

            writer.WriteLine("Available component kinds:");
            using (writer.Nest())
            {
                foreach (var sig in sigs)
                {
                    writer.WriteLine(sig);
                }
            }
        }
 protected override void GenerateMethodSignature(IndentingTextWriter w, string prefix, ComponentCatalog.LoadableClassInfo component)
 {
     w.Write("public static Tuple<ITrainer> Create{0}{1}(", prefix, component.LoadNames[0]);
     using (w.Nest())
     {
         var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
         var arguments    = argumentInfo.Args.Where(a => !a.IsHidden).ToArray();
         var pre          = "";
         foreach (var arg in arguments)
         {
             GenerateMethodSignature(w, arg, null, null, null, ref pre, "");
         }
         w.WriteLine(")");
     }
 }
 protected override void GenerateMethodSignature(IndentingTextWriter w, string prefix, ComponentCatalog.LoadableClassInfo component)
 {
     w.WriteLine("/// <summary>");
     w.WriteLine("/// Creates a {0}", component.LoadNames[0]);
     w.WriteLine("/// </summary>");
     w.WriteLine("/// <param name=\"env\">The environment</param>");
     w.WriteLine("/// <param name=\"data\">The data set</param>");
     w.WriteLine("/// <returns>The transformed data.</returns>");
     w.WriteLine("public IDataView Create{0}{1}Impl(", prefix, component.LoadNames[0]);
     using (w.Nest())
     {
         w.WriteLine("IHostEnvironment env,");
         w.WriteLine("IDataView data)");
     }
 }
 protected override void GenerateImplBody(IndentingTextWriter w, ComponentCatalog.LoadableClassInfo component)
 {
     w.WriteLine("{");
     using (w.Nest())
     {
         w.WriteLine("var args = new {0}();", GetCSharpTypeName(component.ArgType));
         w.WriteLine("var defs = new {0}();", GetCSharpTypeName(component.ArgType));
         var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
         var arguments    = argumentInfo.Args.Where(a => !a.IsHidden).ToArray();
         foreach (var arg in arguments)
         {
             GenerateImplBody(w, arg, "");
         }
         w.WriteLine("return new Tuple<string, string>(\"{0}\", CmdParser.GetSettings(args, defs));", component.LoadNames[0]);
     }
     w.WriteLine("}");
 }
 protected override void GenerateMethodSignature(IndentingTextWriter w, string prefix,
                                                 ComponentCatalog.LoadableClassInfo component)
 {
     w.WriteLine("public static Tuple<IDataView, DataTransform> Create{0}{1}(", prefix, component.LoadNames[0]);
     using (w.Nest())
     {
         var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
         w.WriteLine("[DataLabInputPort(FriendlyName = \"IDataView\", DisplayName = \"IDataView\", IsOptional = false, DataTypes = WellKnownDataTypeIds.IDataViewDotNet, Description = \"Input data (IDataView)\")]");
         w.Write("IDataView data");
         var pre = ",";
         foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
         {
             GenerateMethodSignature(w, arg, null, null, null, ref pre, "");
         }
         w.WriteLine(")");
     }
 }
        protected static void GenerateProperty(IndentingTextWriter w, string typeName, string argName, string defVal,
                                               bool isBool, string helpText)
        {
            var help = helpText ?? argName;

            help = help.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;");
            w.WriteLine("/// <summary> Gets or sets {0}{1} </summary>", isBool ? "a value indicating whether " : "", help);
            w.WriteLine("public {0} {1}", typeName, Capitalize(argName));
            w.WriteLine("{");
            using (w.Nest())
            {
                w.WriteLine("get {{ return {0}; }}", argName);
                w.WriteLine("set {{ {0} = value; }}", argName);
            }
            w.WriteLine("}");
            w.WriteLine();
        }
Esempio n. 10
0
 protected override void GenerateImplCall(IndentingTextWriter w, string prefix, ComponentCatalog.LoadableClassInfo component)
 {
     w.WriteLine("{");
     using (w.Nest())
     {
         var className = prefix + component.LoadNames[0];
         w.WriteLine("var builder = new {0}();", className);
         var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
         var arguments    = argumentInfo.Args.Where(a => !a.IsHidden).ToArray();
         foreach (var arg in arguments)
         {
             GenerateImplCall(w, arg, "");
         }
         w.WriteLine("var learner = builder.GetTlcSettings();");
         w.WriteLine("return new TlcTrainer(learner.Item1, learner.Item2);");
     }
     w.WriteLine("}");
 }
 protected override void GenerateImplCall(IndentingTextWriter w, string prefix, ComponentCatalog.LoadableClassInfo component)
 {
     w.WriteLine("{");
     using (w.Nest())
     {
         var className = prefix + component.LoadNames[0];
         w.WriteLine("var builder = new {0}();", className);
         var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
         foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
         {
             GenerateImplCall(w, arg, "");
         }
         w.WriteLine("var env = new TlcEnvironment(1, verbose: true);");
         w.WriteLine("var view = builder.Create{0}{1}Impl(env, data);", prefix, component.LoadNames[0]);
         w.WriteLine("return new Tuple<IDataView, DataTransform>(view, new DataTransform(view));");
     }
     w.WriteLine("}");
 }
 protected override void GenerateModuleAttribute(IndentingTextWriter w, string prefix,
                                                 ComponentCatalog.LoadableClassInfo component, string moduleId)
 {
     if (!string.IsNullOrEmpty(prefix))
     {
         prefix += " ";
     }
     w.WriteLine("[DataLabModule(FriendlyName = \"{0}{1}\",", prefix, component.UserName);
     using (w.Nest())
     {
         var desc = component.Summary ?? component.LoadNames[0];
         w.WriteLine("Description = \"{0}\",", desc.Replace("\n", "\\n").Replace("\r", "\\r"));
         w.WriteLine("IsBlocking = true,");
         w.WriteLine("IsDeterministic = true,");
         w.WriteLine("Version = \"2.0\",");
         w.WriteLine("Owner = \"Microsoft Corporation\",");
         w.WriteLine("FamilyId = \"{{{0}}}\",", moduleId.ToUpperInvariant());
         w.WriteLine("ReleaseState = States.Alpha)]");
     }
 }
Esempio n. 13
0
        private static void ShowMetadataValueVec <T>(IndentingTextWriter itw, ISchema schema, int col, string kind, ColumnType type)
        {
            Contracts.AssertValue(itw);
            Contracts.AssertValue(schema);
            Contracts.Assert(0 <= col && col < schema.ColumnCount);
            Contracts.AssertNonEmpty(kind);
            Contracts.AssertValue(type);
            Contracts.Assert(type.IsVector);
            Contracts.Assert(type.ItemType.RawType == typeof(T));

            var conv = Conversions.Instance.GetStringConversion <T>(type.ItemType);

            var value = default(VBuffer <T>);

            schema.GetMetadata(kind, col, ref value);

            itw.Write(": Length={0}, Count={0}", value.Length, value.GetValues().Length);

            using (itw.Nest())
            {
                var sb    = default(StringBuilder);
                int count = 0;
                foreach (var item in value.Items())
                {
                    if ((count % 10) == 0)
                    {
                        itw.WriteLine();
                    }
                    else
                    {
                        itw.Write(", ");
                    }
                    var val = item.Value;
                    conv(in val, ref sb);
                    itw.Write("[{0}] '{1}'", item.Key, sb);
                    count++;
                }
            }
        }
Esempio n. 14
0
 private void GenerateHeader(IndentingTextWriter w, string regenerate)
 {
     w.WriteLine("//------------------------------------------------------------------------------");
     w.WriteLine("// <auto-generated>");
     w.WriteLine("//     This code was generated by a tool.");
     w.WriteLine("//");
     w.WriteLine("//     Changes to this file may cause incorrect behavior and will be lost if");
     w.WriteLine("//     the code is regenerated.");
     w.WriteLine("// </auto-generated>");
     w.WriteLine("//------------------------------------------------------------------------------");
     w.WriteLine("// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
     w.WriteLine("// DO NOT MODIFY THIS FILE");
     w.WriteLine("// This file is generated from the TLC components, please don't modify.");
     w.WriteLine("// The following command was used to generate this file: " + regenerate);
     w.WriteLine("// Version used to generate this file: " + Assembly.GetExecutingAssembly().GetName().Version);
     w.WriteLine("// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
     w.WriteLine();
     w.WriteLine("namespace Microsoft.Analytics.Platform.ML.IDVUtils");
     w.WriteLine("{");
     using (w.Nest())
         GenerateUsings(w);
 }
        private void ShowUsage(IndentingTextWriter writer, string kind, string summary, string loadName,
                               IReadOnlyList <string> loadNames, object args, int?columns)
        {
            _env.Assert(loadName == loadNames[0]);

            writer.WriteLine("Help for {0}: '{1}'", kind, loadName);
            using (writer.Nest())
                ShowAliases(writer, loadNames);

            writer.WriteLine();
            ShowFormattedSummary(writer, summary, columns);

            if (args == null)
            {
                writer.WriteLine("Component '{0}' is not configurable", loadName);
                writer.WriteLine();
            }
            else
            {
                writer.WriteLine(CmdParser.ArgumentsUsage(_env, args.GetType(), args, false, columns));
            }
        }
Esempio n. 16
0
        protected override void GenerateModuleAttribute(IndentingTextWriter w, string prefix, ComponentCatalog.LoadableClassInfo component, string moduleId)
        {
            if (!string.IsNullOrEmpty(prefix))
            {
                prefix += " ";
            }
            w.WriteLine("[DataLabModule(FriendlyName = \"{0}{1}\",", prefix, component.UserName);
            using (w.Nest())
            {
                var desc = component.Summary ?? component.LoadNames[0];
                w.WriteLine("Description = \"{0}\",", desc.Replace("\n", "\\n").Replace("\r", "\\r"));
                string cat;
                if (component.IsOfType(typeof(SignatureBinaryClassifierTrainer)) ||
                    component.IsOfType(typeof(SignatureMultiClassClassifierTrainer)))
                {
                    cat = @"Machine Learning\Initialize Model\Classification";
                }
                else if (component.IsOfType(typeof(SignatureRegressorTrainer)))
                {
                    cat = @"Machine Learning\Initialize Model\Regression";
                }
                else if (component.IsOfType(typeof(SignatureAnomalyDetectorTrainer)))
                {
                    cat = @"Machine Learning\Initialize Model\Anomaly Detection";
                }
                else
                {
                    cat = @"Machine Learning\Initialize Model";
                }

                w.WriteLine("Category = @\"{0}\",", cat);
                w.WriteLine("IsBlocking = true,");
                w.WriteLine("IsDeterministic = true,");
                w.WriteLine("Version = \"2.0\",");
                w.WriteLine("Owner = \"Microsoft Corporation\",");
                w.WriteLine("FamilyId = \"{{{0}}}\",", Guid.NewGuid().ToString().ToUpperInvariant());
                w.WriteLine("ReleaseState = States.Alpha)]");
            }
        }
 protected override void GenerateImplBody(IndentingTextWriter w, ComponentCatalog.LoadableClassInfo component)
 {
     w.WriteLine("{");
     using (w.Nest())
     {
         if (component.ArgType == null)
         {
             var call = GenerateCall(component);
             w.WriteLine("return {0}(env, data);", call);
         }
         else
         {
             w.WriteLine("var args = new {0}();", GetCSharpTypeName(component.ArgType));
             var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
             foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
             {
                 GenerateImplBody(w, arg, "");
             }
             var call = GenerateCall(component);
             w.WriteLine("return {0}(args, env, data);", call);
         }
     }
     w.WriteLine("}");
 }
Esempio n. 18
0
        /// <summary>
        /// Generate enums for subcomponents. Uses ReflectionUtils to filter only the subcomponents that match the base type and the signature.
        /// </summary>
        /// <param name="w"></param>
        /// <param name="arg"></param>
        /// <param name="seen"></param>
        protected void GenerateEnums(IndentingTextWriter w, CmdParser.ArgInfo.Arg arg, HashSet <Tuple <Type, Type> > seen)
        {
            if (Exclude.Contains(arg.LongName))
            {
                return;
            }

            var moreEnums = new List <CmdParser.ArgInfo.Arg>();

            if (arg.IsHidden || !arg.IsSubComponentItemType)
            {
                return;
            }
            Contracts.Assert(arg.ItemType.GetGenericTypeDefinition() == typeof(SubComponent <,>));
            var types    = arg.ItemType.GetGenericArguments();
            var baseType = types[0];
            var sigType  = types[1];
            var key      = new Tuple <Type, Type>(baseType, sigType);

            if (seen.Contains(key) || IsTrainer(sigType) || sigType == typeof(SignatureDataLoader))
            {
                return;
            }
            seen.Add(key);
            var typeName = EnumName(arg, sigType);

            w.WriteLine("/// <summary> Available choices for {0} </summary>", sigType);
            w.WriteLine("public enum {0}", typeName);
            w.Write("{");
            using (w.Nest())
            {
                var pre = "";
                if (arg.NullName != null)
                {
                    w.WriteLine();
                    GenerateEnumValue(w, null);
                    pre = ",";
                }
                var infos = ComponentCatalog.GetAllDerivedClasses(baseType, sigType);
                foreach (var info in infos)
                {
                    w.WriteLine(pre);
                    if (pre != "")
                    {
                        w.WriteLine();
                    }
                    pre = ",";
                    GenerateEnumValue(w, info);
                    var args = info.CreateArguments();
                    if (args == null)
                    {
                        continue;
                    }
                    var argInfo = CmdParser.GetArgInfo(args.GetType(), args);
                    moreEnums.AddRange(argInfo.Args);
                }
                w.WriteLine();
            }
            w.WriteLine("}");
            w.WriteLine();
            foreach (var argument in moreEnums)
            {
                GenerateEnums(w, argument, seen);
            }
        }
        protected override void GenerateContent(IndentingTextWriter writer, string prefix,
                                                ComponentCatalog.LoadableClassInfo component, bool generateEnums,
                                                string moduleId)
        {
            writer.WriteLine("[Module(");
            _compName = prefix + component.LoadNames[0];
            var name = Name ?? PrettyPrintDisplayName(component.LoadNames[0]);

            using (writer.Nest())
            {
                writer.WriteLine("Name = \"{0}\",", name);
                writer.WriteLine("FamilyId = \"{0}\",", moduleId);
                writer.WriteLine("Owner = \"{0}\",", Owner);
                writer.WriteLine("ReleaseVersion = \"{0}\",", Version);
                writer.WriteLine("State = ModuleState.{0},", State);
                writer.WriteLine("Type = ModuleType.{0},", ModuleType);
                writer.WriteLine("Determinism = Determinism.{0},", Determinism);
                writer.WriteLine("Category = @\"{0}\")]", Category);
            }
            writer.WriteLine("public static IModule Create{0}(", _compName);
            using (writer.Nest())
            {
                writer.WriteLine("[Help(Display = @\"Dataset\", ToolTip = @\"Input dataset\")]");
                writer.WriteLine("[ModuleInputPort]");
                writer.WriteLine("IDataView idataset,");
                var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
                foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
                {
                    GenerateMethodSignature(writer, arg, null, null, null, "");
                }
                writer.WriteLine("[Help(Display = @\"Results dataset\", ToolTip = @\"Transformed dataset\")]");
                writer.WriteLine("[ModuleOutputPort]");
                writer.WriteLine("IDataView odataset,");
                writer.WriteLine("[Help(Display = @\"{0}\", ToolTip = @\"{0}\")]", name);
                writer.WriteLine("[ModuleOutputPort]");
                writer.WriteLine("DataTransform otransform,");
                writer.WriteLine("[Context]");
                writer.WriteLine("IContext context)");
            }
            writer.WriteLine("{");
            using (writer.Nest())
            {
                writer.WriteLine("var instance = new {0}Module();", _compName);
                writer.WriteLine();
                writer.WriteLine("var ports = new Dictionary<string, object> { { \"idataset\", idataset } };");
                writer.WriteLine("var parameters = new Dictionary<string, object>");
                writer.WriteLine("{");
                using (writer.Nest())
                {
                    var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
                    foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
                    {
                        GenerateDictionaryEntry(writer, arg, "");
                    }
                }
                writer.WriteLine("};");
                writer.WriteLine();
                writer.WriteLine("instance.Context = context;");
                writer.WriteLine("instance.SetInputPorts(ports);");
                writer.WriteLine("instance.SetParameters(parameters);");
                writer.WriteLine();
                writer.WriteLine("return instance;");
            }
            writer.WriteLine("}");
            writer.WriteLine();
            writer.WriteLine("public class {0}Module : ModuleBase", _compName);
            writer.WriteLine("{");
            using (writer.Nest())
            {
                writer.WriteLine("private Dictionary<string, object> parameters;");
                writer.WriteLine("private Dictionary<string, object> ports;");
                writer.WriteLine();
                writer.WriteLine("public override Dictionary<string, object> Run()");
                writer.WriteLine("{");
                using (writer.Nest())
                {
                    writer.WriteLine("var view = ConstructTransform((IDataView)ports[\"idataset\"]);");
                    writer.WriteLine("return new Dictionary<string, object> { { \"odataset\", view }, { \"otransform\", new DataTransform(view) } };");
                }
                writer.WriteLine("}");
                writer.WriteLine();
                writer.WriteLine("public override void SetParameters(Dictionary<string, object> parameters)");
                writer.WriteLine("{");
                using (writer.Nest())
                    writer.WriteLine("this.parameters = parameters;");
                writer.WriteLine("}");
                writer.WriteLine();
                writer.WriteLine("public override void SetInputPorts(Dictionary<string, object> ports)");
                writer.WriteLine("{");
                using (writer.Nest())
                    writer.WriteLine("this.ports = ports;");
                writer.WriteLine("}");
                writer.WriteLine();
                writer.WriteLine("public override Dictionary<string, object> ComputeSchema(Dictionary<string, object> inputports)");
                writer.WriteLine("{");
                using (writer.Nest())
                {
                    writer.WriteLine("var view = ConstructTransform((IDataView)inputports[\"idataset\"]);");
                    writer.WriteLine("return new Dictionary<string, object> { { \"odataset\", view.Schema } };");
                }
                writer.WriteLine("}");
                writer.WriteLine();
                writer.WriteLine("private IDataView ConstructTransform(IDataView input)");
                writer.WriteLine("{");
                using (writer.Nest())
                {
                    writer.WriteLine("var builder = new {0}();", _compName);
                    var argumentInfo = CmdParser.GetArgInfo(component.ArgType, component.CreateArguments());
                    foreach (var arg in argumentInfo.Args.Where(a => !a.IsHidden))
                    {
                        GenerateImplCall(writer, arg, null, null, null, "");
                    }
                    writer.WriteLine("return builder.Create{0}Impl(Host, input);", _compName);
                }
                writer.WriteLine("}");
            }
            writer.WriteLine("}");
        }
Esempio n. 20
0
        protected void GenerateImplBody(IndentingTextWriter w, CmdParser.ArgInfo.Arg arg, string argSuffix)
        {
            if (Exclude.Contains(arg.LongName))
            {
                return;
            }

            if (arg.IsSubComponentItemType)
            {
                // We need to create a tree with all the subcomponents, unless the subcomponent is a Trainer.
                Contracts.Assert(arg.ItemType.GetGenericTypeDefinition() == typeof(SubComponent <,>));
                var types    = arg.ItemType.GetGenericArguments();
                var baseType = types[0];
                var sigType  = types[1];
                if (IsTrainer(sigType))
                {
                    if (arg.IsCollection)
                    {
                        w.WriteLine("args{0}.{1} = new[] {{ new SubComponent<{2}, {3}>({4}.Item1, {4}.Item2) }};",
                                    argSuffix, arg.LongName, GetCSharpTypeName(baseType), GetCSharpTypeName(sigType),
                                    arg.LongName + argSuffix);
                    }
                    else
                    {
                        w.WriteLine("args{0}.{1} = new SubComponent<{2}, {3}>({4}.Item1, {4}.Item2);", argSuffix,
                                    arg.LongName, GetCSharpTypeName(baseType), GetCSharpTypeName(sigType),
                                    arg.LongName + argSuffix);
                    }
                    return;
                }
                if (sigType == typeof(SignatureDataLoader))
                {
                    return;
                }
                var typeName = EnumName(arg, sigType);
                w.WriteLine("switch ({0})", arg.LongName + argSuffix);
                w.WriteLine("{");
                using (w.Nest())
                {
                    if (arg.NullName != null)
                    {
                        w.WriteLine("case {0}.None:", typeName);
                        using (w.Nest())
                        {
                            w.WriteLine("args{0}.{1} = null;", argSuffix, arg.LongName);
                            w.WriteLine("break;");
                        }
                    }
                    var infos = ComponentCatalog.GetAllDerivedClasses(baseType, sigType);
                    foreach (var info in infos)
                    {
                        w.WriteLine("case {0}.{1}:", typeName, info.LoadNames[0]);
                        using (w.Nest())
                        {
                            if (info.ArgType != null)
                            {
                                var newArgSuffix = argSuffix + info.LoadNames[0];
                                w.WriteLine("var args{0} = new {1}();", newArgSuffix, GetCSharpTypeName(info.ArgType));
                                w.WriteLine("var defs{0} = new {1}();", newArgSuffix, GetCSharpTypeName(info.ArgType));
                                var args = info.CreateArguments();
                                if (args != null)
                                {
                                    var argInfo = CmdParser.GetArgInfo(args.GetType(), args);
                                    foreach (var a in argInfo.Args)
                                    {
                                        GenerateImplBody(w, a, newArgSuffix);
                                    }
                                }
                                w.WriteLine(
                                    "args{0}.{1} = new {2}(\"{3}\", CmdParser.GetSettings(args{4}, defs{4}));",
                                    argSuffix, arg.LongName, GetCSharpTypeName(arg.ItemType), info.LoadNames[0],
                                    newArgSuffix);
                            }
                            else
                            {
                                w.WriteLine("args{0}.{1} = new {2}(\"{3}\");", argSuffix, arg.LongName, GetCSharpTypeName(arg.ItemType), info.LoadNames[0]);
                            }
                            w.WriteLine("break;");
                        }
                    }
                }
                w.WriteLine("}");
            }
            else if (arg.IsCollection)
            {
                if (IsColumnType(arg))
                {
                    w.WriteLine("args{0}.{1} = {1}.Select({2}.Parse).ToArray();", argSuffix, arg.LongName, GetCSharpTypeName(arg.ItemType));
                }
                else if (IsStringColumnType(arg))
                {
                    w.WriteLine("args{0}.{1} = {2};", argSuffix, arg.LongName, arg.LongName + argSuffix);
                }
                else
                {
                    w.WriteLine("args{0}.{1} = new[] {{ {2} }};", argSuffix, arg.LongName, arg.LongName + argSuffix);
                }
            }
            else
            {
                w.WriteLine("args{0}.{1} = {2};", argSuffix, arg.LongName, arg.LongName + argSuffix);
            }
        }
        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);
                }
            }
        }