private void AssertTextSplit(string text, int splitAt, string expectedBefore, string expectedAfter)
        {
            string before;
            string after;

            MonospaceTextFormat.SplitTextOnSeparator(text, out before, out after, splitAt, new char[] { ' ' });
            Assert.That(before, Is.EqualTo(expectedBefore));
            Assert.That(after, Is.EqualTo(expectedAfter));
        }
        public void TestAppendIndentedText()
        {
            string        label       = "this is the label  ";
            string        description = "the quick brown fox jumps over the lazy dog. THE (VERY QUICK) FOX JUMPS OVER THE LAZY DOG.";
            StringBuilder sb          = new StringBuilder(label);

            MonospaceTextFormat.AppendIndentedText(sb, label.Length, 30, description);
            string expectedText =
                "this is the label  the quick"
                + "\n                   brown fox"
                + "\n                   jumps over"
                + "\n                   the lazy"
                + "\n                   dog. THE"
                + "\n                   (VERY"
                + "\n                   QUICK) FOX"
                + "\n                   JUMPS OVER"
                + "\n                   THE LAZY"
                + "\n                   DOG.";

            Assert.That(sb.ToString(), Is.EqualTo(expectedText));
        }
Exemple #3
0
        /// <summary>
        /// Returns a string containing the syntax description of the arguments contained in <see cref="Arguments"/>.
        /// </summary>
        /// <param name="commandName">The file name of the program.</param>
        /// <param name="maxWidth">The maximum line length (for screen output, use 79 for 80 character output to avoid blank lines).</param>
        /// <returns>A syntax overview containing a short command line overview and a table of parameters and desciptions.</returns>
        public string GetAsciiSynopsis(string commandName, int maxWidth)
        {
            StringBuilder sb = new StringBuilder(2048);

            sb.Append(commandName);
            int maxLength          = 0;
            int openSquareBrackets = 0;

            for (int i = 0; i < Arguments.Count; ++i)
            {
                CommandLineArgument argument     = Arguments[i];
                CommandLineArgument nextArgument = ((i + 1) < Arguments.Count) ? Arguments[i + 1] : null;

                if (!(argument is ICommandLinePartArgument))
                {
                    // append opening square bracket
                    sb.Append(" ");
                    if (argument.IsOptional)
                    {
                        sb.Append("[");
                        ++openSquareBrackets;
                    }

                    argument.AppendSynopsis(sb);

                    // append closing square brackets after last optional argument
                    if (nextArgument == null ||
                        !nextArgument.IsOptional ||
                        !nextArgument.IsPositional)
                    {
                        for (int k = 0; k < openSquareBrackets; ++k)
                        {
                            sb.Append("]");
                        }
                        openSquareBrackets = 0;
                    }
                }

                if (!(argument is CommandLineGroupArgument))
                {
                    if (argument.Name != null)
                    {
                        maxLength = Math.Max(maxLength, argument.Name.Length + 1);
                    }
                    else if (argument.Placeholder != null)
                    {
                        maxLength = Math.Max(maxLength, argument.Placeholder.Length);
                    }
                }
            }

            // insert word breaks
            string synopsis = sb.ToString();

            sb.Length = 0;
            MonospaceTextFormat.AppendWrappedText(sb, maxWidth, synopsis);

            // create parameter name/description table
            sb.Append("\n");
            foreach (CommandLineArgument argument in Arguments)
            {
                if (!(argument is CommandLineGroupArgument))
                {
                    string name = argument.Placeholder;
                    if (argument.Name != null)
                    {
                        name = _argumentDeclarationPrefix + argument.Name;
                    }

                    sb.AppendFormat("\n  {0,-" + maxLength.ToString() + "}  ", name);
                    MonospaceTextFormat.AppendIndentedText(sb, maxLength + 4, maxWidth, argument.Description);
                }
            }

            return(sb.ToString());
        }