Esempio n. 1
0
        /// <summary>
        /// Shows help.
        /// </summary>
        public void PrintHelp(TextWriter output)
        {
            const int ArgHelpLineLength = 100;
            const int ArgHelpIndent     = 30;

            string prefix = string.Format("usage: {0} ", this.AppName);

            output.Write(prefix);
            int indent = prefix.Length;

            var wrapper = new WordWrapper(output, indent, ArgHelpLineLength);

            foreach (var name in this.PositionalNames)
            {
                var    arg  = this.Arguments[name];
                string text = arg.LongSyntax;
                if (arg.DependsOn != null)
                {
                    text = "[" + text + "]";
                }

                wrapper.WriteWord(text);
            }

            foreach (var name in this.LongNames)
            {
                var arg = this.Arguments[name];
                if (arg.IsHidden)
                {
                    continue;
                }

                string text = arg.LongSyntaxAndDataType;

                if (!arg.IsRequired)
                {
                    text = "[" + text + "]";
                }

                wrapper.WriteWord(text);
            }

            output.WriteLine();
            output.WriteLine();
            wrapper = new WordWrapper(output, 0, ArgHelpLineLength);
            wrapper.Write(this.AppDescription);
            output.WriteLine();
            output.WriteLine();
            var visitedOptions = new HashSet <string>();

            foreach (var name in this.GroupNames)
            {
                CommandLineGroup g = this.Groups[name];
                if (g.IsHidden)
                {
                    continue;
                }

                output.WriteLine(g.Description + ":");
                output.WriteLine(new string('-', g.Description.Length + 1));

                foreach (var option in this.PositionalNames.Concat(this.LongNames))
                {
                    var arg = this.Arguments[option];
                    if (arg.IsHidden)
                    {
                        continue;
                    }

                    if (arg.Group == name)
                    {
                        visitedOptions.Add(option);

                        string syntax = "  ";
                        if (!string.IsNullOrEmpty(arg.ShortName))
                        {
                            syntax += string.Format("{0}, ", arg.ShortSyntax);
                        }

                        syntax += string.Format("{0} ", arg.LongSyntaxAndDataType);

                        output.Write(syntax);
                        if (syntax.Length < ArgHelpIndent)
                        {
                            output.Write(new string(' ', ArgHelpIndent - syntax.Length));
                        }
                        else
                        {
                            output.WriteLine();
                            output.Write(new string(' ', ArgHelpIndent));
                        }

                        if (!string.IsNullOrEmpty(arg.Description))
                        {
                            output.Write(": ");
                            wrapper = new WordWrapper(output, ArgHelpIndent + 2, ArgHelpLineLength);
                            wrapper.Write(arg.Description);
                        }

                        output.WriteLine();
                    }
                }

                output.WriteLine();
            }

            bool optionalHeader = false;

            foreach (var option in this.PositionalNames.Concat(this.LongNames))
            {
                var arg = this.Arguments[option];
                if (arg.IsHidden)
                {
                    continue;
                }

                if (!visitedOptions.Contains(arg.LongName))
                {
                    if (!optionalHeader)
                    {
                        optionalHeader = true;
                        const string optionalBanner = "Optional Arguments:";
                        output.WriteLine(optionalBanner);
                        output.WriteLine(new string('-', optionalBanner.Length));
                    }

                    string syntax = "  ";
                    if (!string.IsNullOrEmpty(arg.ShortName))
                    {
                        syntax += string.Format("{0}, ", arg.ShortSyntax);
                    }

                    syntax += string.Format("{0} ", arg.LongSyntax);
                    output.Write(syntax);
                    if (syntax.Length < ArgHelpIndent)
                    {
                        output.Write(new string(' ', ArgHelpIndent - syntax.Length));
                    }
                    else
                    {
                        output.WriteLine();
                        output.Write(new string(' ', ArgHelpIndent));
                    }

                    if (!string.IsNullOrEmpty(arg.Description))
                    {
                        wrapper = new WordWrapper(output, ArgHelpIndent, ArgHelpLineLength);
                        wrapper.Write(arg.Description);
                    }

                    output.WriteLine();
                }
            }
        }