Example #1
0
        public void HeaderStartingWithLineSeparator()
        {
            // related to Bugzilla #21215
            Options       options   = new Options();
            HelpFormatter formatter = new HelpFormatter();
            String        header    = Eol + "Header";
            String        footer    = "Footer";
            StringWriter  output    = new StringWriter();

            var settings = new HelpSettings {
                Width              = 80,
                Header             = header,
                Footer             = footer,
                LeftPadding        = 2,
                DescriptionPadding = 2,
                CommandLineSyntax  = "foobar"
            };

            formatter.PrintHelp(options, settings, output, true);
            Assert.AreEqual(
                "usage: foobar" + Eol +
                "" + Eol +
                "Header" + Eol +
                "" + Eol +
                "Footer" + Eol
                , output.ToString());
        }
Example #2
0
        public void OptionWithoutShortFormat()
        {
            // related to Bugzilla #19383 (CLI-67)
            Options options = new Options();

            options.AddOption(new Option("a", "aaa", false, "aaaaaaa"));
            options.AddOption(new Option(null, "bbb", false, "bbbbbbb"));
            options.AddOption(new Option("c", null, false, "ccccccc"));

            HelpFormatter formatter = new HelpFormatter();
            StringWriter  output    = new StringWriter();

            var settings = new HelpSettings {
                Width              = 80,
                LeftPadding        = 2,
                DescriptionPadding = 2,
                CommandLineSyntax  = "foobar"
            };

            formatter.PrintHelp(options, settings, output, true);
            Assert.AreEqual(
                "usage: foobar [-a] [--bbb] [-c]" + Eol +
                "  -a,--aaa  aaaaaaa" + Eol +
                "     --bbb  bbbbbbb" + Eol +
                "  -c        ccccccc" + Eol
                , output.ToString());
        }
Example #3
0
        private void AppendOptionGroup(HelpSettings settings, StringBuilder buff, IOptionGroup group)
        {
            if (!group.IsRequired)
            {
                buff.Append("[");
            }

            var optList = new List <IOption>(group.Options);

            optList.Sort(settings.OptionComparer);
            // for each option in the OptionGroup
            for (int i = 0; i < optList.Count; i++)
            {
                Option option = (Option)optList[i];
                // whether the option is required or not is handled at group level
                AppendOption(settings, buff, option, true);

                if (i < optList.Count - 1)
                {
                    buff.Append(" | ");
                }
            }

            if (!group.IsRequired)
            {
                buff.Append("]");
            }
        }
Example #4
0
        public void PrintWrapped(HelpSettings settings, TextWriter pw, int nextLineTabStop, String text)
        {
            StringBuilder sb = new StringBuilder(text.Length);

            RenderWrappedText(settings, sb, settings.Width, nextLineTabStop, text);
            pw.WriteLine(sb.ToString());
        }
Example #5
0
        public void PrintHelp(Options options, HelpSettings settings, TextWriter writer, bool autoUsage)
        {
            if (string.IsNullOrEmpty(settings.CommandLineSyntax))
            {
                throw new InvalidOperationException("Command-line syntax not specified.");
            }

            if (autoUsage)
            {
                PrintUsage(options, settings, writer);
            }
            else
            {
                PrintSimpleUsage(settings, writer);
            }

            if (settings.HasHeader)
            {
                PrintWrapped(settings, writer, settings.Header);
            }

            PrintOptions(settings, writer, settings.Width, options, settings.LeftPadding, settings.DescriptionPadding);

            if (settings.HasFooter)
            {
                PrintWrapped(settings, writer, settings.Footer);
            }
        }
Example #6
0
        private void AppendOption(HelpSettings settings, StringBuilder buff, Option option, bool required)
        {
            if (!required)
            {
                buff.Append("[");
            }

            if (option.Name != null)
            {
                buff.Append("-").Append(option.Name);
            }
            else
            {
                buff.Append("--").Append(option.LongName);
            }

            // if the Option has a value
            if (option.HasArgument())
            {
                if (option.HasArgumentName)
                {
                    buff.Append(" <").Append(option.ArgumentName).Append(">");
                }
                else if (!String.IsNullOrEmpty(settings.ArgumentName))
                {
                    buff.Append(" <").Append(settings.ArgumentName).Append(">");
                }
            }

            // if the Option is not a required option
            if (!required)
            {
                buff.Append("]");
            }
        }
Example #7
0
        public void PrintUsage(IOptions options, HelpSettings settings, TextWriter writer)
        {
            // initialise the string buffer
            StringBuilder buff = new StringBuilder(settings.SyntaxPrefix)
                                 .Append(settings.CommandLineSyntax)
                                 .Append(" ");

            // create a list for processed option groups
            var processedGroups = new List <IOptionGroup>();

            // temp variable

            var optList = new List <IOption>(options.AllOptions);

            optList.Sort(settings.OptionComparer);

            // iterate over the options
            for (int i = 0; i < optList.Count; i++)
            {
                // get the next Option
                var option = (Option)optList[i];

                // check if the option is part of an OptionGroup
                IOptionGroup group = options.GetOptionGroup(option);

                // if the option is part of a group
                if (group != null)
                {
                    // and if the group has not already been processed
                    if (!processedGroups.Contains(group))
                    {
                        // add the group to the processed list
                        processedGroups.Add(group);


                        // add the usage clause
                        AppendOptionGroup(settings, buff, group);
                    }

                    // otherwise the option was displayed in the group
                    // previously so ignore it.
                }

                // if the Option is not part of an OptionGroup
                else
                {
                    AppendOption(settings, buff, option, option.IsRequired);
                }

                if (i < optList.Count - 1)
                {
                    buff.Append(" ");
                }
            }


            // call printWrapped
            PrintWrapped(settings, writer, buff.ToString().IndexOf(' ') + 1, buff.ToString());
        }
Example #8
0
        public void PrintOptions(HelpSettings settings, TextWriter pw,
                                 int width,
                                 Options options,
                                 int leftPad,
                                 int descPad)
        {
            StringBuilder sb = new StringBuilder();

            RenderOptions(settings, sb, width, options, leftPad, descPad);
            pw.WriteLine(sb.ToString());
        }
Example #9
0
        public void PrintOptions()
        {
            var       sb      = new StringBuilder();
            var       hf      = new HelpFormatter();
            const int leftPad = 1;
            const int descPad = 3;
            String    lpad    = hf.createPadding(leftPad);
            String    dpad    = hf.createPadding(descPad);

            var settings = new HelpSettings();

            Options options  = new Options().AddOption("a", false, "aaaa aaaa aaaa aaaa aaaa");
            string  expected = lpad + "-a" + dpad + "aaaa aaaa aaaa aaaa aaaa";

            hf.RenderOptions(settings, sb, 60, options, leftPad, descPad);
            Assert.AreEqual(expected, sb.ToString(), "simple non-wrapped option");

            int nextLineTabStop = leftPad + descPad + "-a".Length;

            expected = lpad + "-a" + dpad + "aaaa aaaa aaaa" + settings.NewLine +
                       hf.createPadding(nextLineTabStop) + "aaaa aaaa";
            sb.Length = 0;
            hf.RenderOptions(settings, sb, nextLineTabStop + 17, options, leftPad, descPad);
            Assert.AreEqual(expected, sb.ToString(), "simple wrapped option");


            options   = new Options().AddOption("a", "aaa", false, "dddd dddd dddd dddd");
            expected  = lpad + "-a,--aaa" + dpad + "dddd dddd dddd dddd";
            sb.Length = 0;
            hf.RenderOptions(settings, sb, 60, options, leftPad, descPad);
            Assert.AreEqual(expected, sb.ToString(), "long non-wrapped option");

            nextLineTabStop = leftPad + descPad + "-a,--aaa".Length;
            expected        = lpad + "-a,--aaa" + dpad + "dddd dddd" + settings.NewLine +
                              hf.createPadding(nextLineTabStop) + "dddd dddd";
            sb.Length = 0;
            hf.RenderOptions(settings, sb, 25, options, leftPad, descPad);
            Assert.AreEqual(expected, sb.ToString(), "long wrapped option");

            options = new Options().
                      AddOption("a", "aaa", false, "dddd dddd dddd dddd").
                      AddOption("b", false, "feeee eeee eeee eeee");
            expected = lpad + "-a,--aaa" + dpad + "dddd dddd" + settings.NewLine +
                       hf.createPadding(nextLineTabStop) + "dddd dddd" + settings.NewLine +
                       lpad + "-b      " + dpad + "feeee eeee" + settings.NewLine +
                       hf.createPadding(nextLineTabStop) + "eeee eeee";
            sb.Length = 0;
            hf.RenderOptions(settings, sb, 25, options, leftPad, descPad);
            Assert.AreEqual(expected, sb.ToString(), "multiple wrapped options");
        }
Example #10
0
        public void PrintWrapped()
        {
            var sb = new StringBuilder();
            var hf = new HelpFormatter();

            String text = "This is a test.";

            var settings = new HelpSettings();

            String expected = "This is a" + settings.NewLine + "test.";

            hf.RenderWrappedText(settings, sb, 12, 0, text);
            Assert.AreEqual(expected, sb.ToString(), "single line text");

            sb.Length = 0;
            expected  = "This is a" + settings.NewLine + "    test.";
            hf.RenderWrappedText(settings, sb, 12, 4, text);
            Assert.AreEqual(expected, sb.ToString(), "single line padded text");

            text = "  -p,--period <PERIOD>  PERIOD is time duration of form " +
                   "DATE[-DATE] where DATE has form YYYY[MM[DD]]";

            sb.Length = 0;
            expected  = "  -p,--period <PERIOD>  PERIOD is time duration of" +
                        settings.NewLine +
                        "                        form DATE[-DATE] where DATE" +
                        settings.NewLine +
                        "                        has form YYYY[MM[DD]]";
            hf.RenderWrappedText(settings, sb, 53, 24, text);
            Assert.AreEqual(expected, sb.ToString(), "single line padded text 2");

            text = "aaaa aaaa aaaa" + settings.NewLine +
                   "aaaaaa" + settings.NewLine +
                   "aaaaa";

            expected  = text;
            sb.Length = 0;
            hf.RenderWrappedText(settings, sb, 16, 0, text);
            Assert.AreEqual(expected, sb.ToString(), "multi line text");

            expected = "aaaa aaaa aaaa" + settings.NewLine +
                       "    aaaaaa" + settings.NewLine +
                       "    aaaaa";
            sb.Length = 0;
            hf.RenderWrappedText(settings, sb, 16, 4, text);
            Assert.AreEqual(expected, sb.ToString(), "multi-line padded text");
        }
Example #11
0
        internal StringBuilder RenderWrappedText(HelpSettings settings, StringBuilder sb,
                                                 int width,
                                                 int nextLineTabStop,
                                                 String text)
        {
            int pos = findWrapPos(text, width, 0);

            if (pos == -1)
            {
                sb.Append(rtrim(text));

                return(sb);
            }
            sb.Append(rtrim(text.Substring(0, pos))).Append(settings.NewLine);

            if (nextLineTabStop >= width)
            {
                // stops infinite loop happening
                nextLineTabStop = 1;
            }

            // all following lines must be padded with nextLineTabStop space
            // characters
            String padding = createPadding(nextLineTabStop);

            while (true)
            {
                text = padding + text.Substring(pos).Trim();
                pos  = findWrapPos(text, width, 0);

                if (pos == -1)
                {
                    sb.Append(text);

                    return(sb);
                }

                if ((text.Length > width) && (pos == nextLineTabStop - 1))
                {
                    pos = width;
                }

                sb.Append(rtrim(text.Substring(0, pos))).Append(settings.NewLine);
            }
        }
Example #12
0
        public void OptionWithoutShortFormat2()
        {
            // related to Bugzilla #27635 (CLI-26)
            Option help       = new Option("h", "help", false, "print this message");
            Option version    = new Option("v", "version", false, "print version information");
            Option newRun     = new Option("n", "new", false, "Create NLT cache entries only for new items");
            Option trackerRun = new Option("t", "tracker", false, "Create NLT cache entries only for tracker items");

            Option timeLimit = OptionBuilder.New().WithLongName("limit")
                               .HasArgument()
                               .WithValueSeparator()
                               .WithDescription("Set time limit for execution, in mintues")
                               .Create("l");

            Option age = OptionBuilder.New().WithLongName("age")
                         .HasArgument()
                         .WithValueSeparator()
                         .WithDescription("Age (in days) of cache item before being recomputed")
                         .Create("a");

            Option server = OptionBuilder.New().WithLongName("server")
                            .HasArgument()
                            .WithValueSeparator()
                            .WithDescription("The NLT server address")
                            .Create("s");

            Option numResults = OptionBuilder.New().WithLongName("results")
                                .HasArgument()
                                .WithValueSeparator()
                                .WithDescription("Number of results per item")
                                .Create("r");

            Option configFile = OptionBuilder.New().WithLongName("config")
                                .HasArgument()
                                .WithValueSeparator()
                                .WithDescription("Use the specified configuration file")
                                .Create();

            Options mOptions = new Options();

            mOptions.AddOption(help);
            mOptions.AddOption(version);
            mOptions.AddOption(newRun);
            mOptions.AddOption(trackerRun);
            mOptions.AddOption(timeLimit);
            mOptions.AddOption(age);
            mOptions.AddOption(server);
            mOptions.AddOption(numResults);
            mOptions.AddOption(configFile);

            HelpFormatter formatter = new HelpFormatter();

            String       EOL    = Environment.NewLine;
            StringWriter output = new StringWriter();

            var settings = new HelpSettings {
                ArgumentName       = "arg",
                Width              = 80,
                Header             = "header",
                Footer             = "footer",
                DescriptionPadding = 2,
                LeftPadding        = 2,
                CommandLineSyntax  = "commandline"
            };

            formatter.PrintHelp(mOptions, settings, output, true);
            Assert.AreEqual(
                "usage: commandline [-a <arg>] [--config <arg>] [-h] [-l <arg>] [-n] [-r <arg>]" + EOL +
                "       [-s <arg>] [-t] [-v]" + EOL +
                "header" + EOL +
                "  -a,--age <arg>      Age (in days) of cache item before being recomputed" + EOL +
                "     --config <arg>   Use the specified configuration file" + EOL +
                "  -h,--help           print this message" + EOL +
                "  -l,--limit <arg>    Set time limit for execution, in mintues" + EOL +
                "  -n,--new            Create NLT cache entries only for new items" + EOL +
                "  -r,--results <arg>  Number of results per item" + EOL +
                "  -s,--server <arg>   The NLT server address" + EOL +
                "  -t,--tracker        Create NLT cache entries only for tracker items" + EOL +
                "  -v,--version        print version information" + EOL +
                "footer" + EOL
                , output.ToString());
        }
Example #13
0
        // --------------------------------------------------------------- Protected

        internal StringBuilder RenderOptions(HelpSettings settings, StringBuilder sb, int width, IOptions options, int leftPad, int descPad)
        {
            String lpad = createPadding(leftPad);
            String dpad = createPadding(descPad);

            // first create list containing only <lpad>-a,--aaa where
            // -a is opt and --aaa is long opt; in parallel look for
            // the longest opt string this list will be then used to
            // sort options ascending
            int           max = 0;
            StringBuilder optBuf;
            ArrayList     prefixList = new ArrayList();

            var optList = new List <IOption>(options.AllOptions);

            optList.Sort(settings.OptionComparer);

            foreach (var option in optList)
            {
                optBuf = new StringBuilder(8);

                if (option.Name == null)
                {
                    optBuf.Append(lpad).Append("   " + settings.LongOptionPrefix).Append(option.LongName);
                }
                else
                {
                    optBuf.Append(lpad).Append(settings.OptionPrefix).Append(option.Name);

                    if (option.HasLongName())
                    {
                        optBuf.Append(',').Append(settings.LongOptionPrefix).Append(option.LongName);
                    }
                }

                if (option.HasArgument())
                {
                    if (option.HasArgumentName())
                    {
                        optBuf.Append(" <").Append(option.ArgumentName).Append(">");
                    }
                    else if (!String.IsNullOrEmpty(settings.ArgumentName))
                    {
                        optBuf.Append(" <").Append(settings.ArgumentName).Append(">");
                    }
                    else
                    {
                        optBuf.Append(' ');
                    }
                }

                prefixList.Add(optBuf);
                max = (optBuf.Length > max) ? optBuf.Length : max;
            }

            int x = 0;

            for (int i = 0; i < optList.Count; i++)
            {
                Option option = (Option)optList[i];
                optBuf = new StringBuilder(prefixList[x++].ToString());

                if (optBuf.Length < max)
                {
                    optBuf.Append(createPadding(max - optBuf.Length));
                }

                optBuf.Append(dpad);

                int nextLineTabStop = max + descPad;

                if (option.Description != null)
                {
                    optBuf.Append(option.Description);
                }

                RenderWrappedText(settings, sb, width, nextLineTabStop, optBuf.ToString());

                if (i < optList.Count - 1)
                {
                    sb.Append(settings.NewLine);
                }
            }

            return(sb);
        }
Example #14
0
 public void PrintWrapped(HelpSettings settings, TextWriter pw, String text)
 {
     PrintWrapped(settings, pw, 0, text);
 }
Example #15
0
        public void PrintSimpleUsage(HelpSettings settings, TextWriter pw)
        {
            int argPos = settings.CommandLineSyntax.IndexOf(' ') + 1;

            PrintWrapped(settings, pw, settings.SyntaxPrefix.Length + argPos, settings.SyntaxPrefix + settings.CommandLineSyntax);
        }