Пример #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());
        }
Пример #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());
        }
Пример #3
0
        public void AutomaticUsage()
        {
            var    hf       = new HelpFormatter();
            String expected = "usage: app [-a]";
            var    output   = new MemoryStream();
            var    pw       = new StreamWriter(output);

            Options options = new Options().AddOption("a", false, "aaaa aaaa aaaa aaaa aaaa");

            hf.PrintUsage(options, new HelpSettings {
                Width = 60, CommandLineSyntax = "app"
            }, pw);
            pw.Flush();
            Assert.AreEqual(expected, Encoding.UTF8.GetString(output.ToArray()).Trim(), "simple auto usage");

            output   = new MemoryStream();
            pw       = new StreamWriter(output);
            expected = "usage: app [-a] [-b]";
            options  = new Options().AddOption("a", false, "aaaa aaaa aaaa aaaa aaaa").AddOption("b", false, "bbb");
            hf.PrintUsage(options, new HelpSettings {
                CommandLineSyntax = "app", Width = 60
            }, pw);
            pw.Flush();
            Assert.AreEqual(expected, Encoding.UTF8.GetString(output.ToArray()).Trim(), "simple auto usage");
        }
Пример #4
0
        public void Rtrim()
        {
            HelpFormatter formatter = new HelpFormatter();

            Assert.AreEqual(null, formatter.rtrim(null));
            Assert.AreEqual("", formatter.rtrim(""));
            Assert.AreEqual("  foo", formatter.rtrim("  foo  "));
        }
Пример #5
0
        public void PrintHelpWithEmptySyntax()
        {
            var formatter = new HelpFormatter();

            Assert.Throws <InvalidOperationException>(() => formatter.PrintHelp(new Options(), new HelpSettings(), Console.Out, false),
                                                      "null command line syntax should be rejected");
            Assert.Throws <InvalidOperationException>(
                () => formatter.PrintHelp(new Options(), new HelpSettings {
                CommandLineSyntax = String.Empty
            }, Console.Out, false),
                "empty command line syntax should be rejected");
        }
Пример #6
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");
        }
Пример #7
0
        public void FindWrapPos()
        {
            var hf = new HelpFormatter();

            String text = "This is a test.";

            //text width should be max 8; the wrap position is 7
            Assert.AreEqual(7, hf.findWrapPos(text, 8, 0), "wrap position");
            //starting from 8 must give -1 - the wrap pos is after end
            Assert.AreEqual(-1, hf.findWrapPos(text, 8, 8), "wrap position 2");
            //if there is no a good position before width to make a wrapping look for the next one
            text = "aaaa aa";
            Assert.AreEqual(4, hf.findWrapPos(text, 3, 0), "wrap position 3");
        }
Пример #8
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");
        }
Пример #9
0
        public void Man()
        {
            String cmdLine =
                "man [-c|-f|-k|-w|-tZT device] [-adlhu7V] [-Mpath] [-Ppager] [-Slist] " +
                "[-msystem] [-pstring] [-Llocale] [-eextension] [section] page ...";
            Options options = new Options().
                              AddOption("a", "all", false, "find all matching manual pages.").
                              AddOption("d", "debug", false, "emit debugging messages.").
                              AddOption("e", "extension", false, "limit search to extension type 'extension'.").
                              AddOption("f", "whatis", false, "equivalent to whatis.").
                              AddOption("k", "apropos", false, "equivalent to apropos.").
                              AddOption("w", "location", false, "print physical location of man page(s).").
                              AddOption("l", "local-file", false, "interpret 'page' argument(s) as local filename(s)").
                              AddOption("u", "update", false, "force a cache consistency check.").
                              //FIXME - should generate -r,--prompt string
                              AddOption("r", "prompt", true, "provide 'less' pager with prompt.").
                              AddOption("c", "catman", false, "used by catman to reformat out of date cat pages.").
                              AddOption("7", "ascii", false, "display ASCII translation or certain latin1 chars.").
                              AddOption("t", "troff", false, "use troff format pages.").
                              //FIXME - should generate -T,--troff-device device
                              AddOption("T", "troff-device", true, "use groff with selected device.").
                              AddOption("Z", "ditroff", false, "use groff with selected device.").
                              AddOption("D", "default", false, "reset all options to their default values.").
                              //FIXME - should generate -M,--manpath path
                              AddOption("M", "manpath", true, "set search path for manual pages to 'path'.").
                              //FIXME - should generate -P,--pager pager
                              AddOption("P", "pager", true, "use program 'pager' to display output.").
                              //FIXME - should generate -S,--sections list
                              AddOption("S", "sections", true, "use colon separated section list.").
                              //FIXME - should generate -m,--systems system
                              AddOption("m", "systems", true, "search for man pages from other unix system(s).").
                              //FIXME - should generate -L,--locale locale
                              AddOption("L", "locale", true, "define the locale for this particular man search.").
                              //FIXME - should generate -p,--preprocessor string
                              AddOption("p", "preprocessor", true, "string indicates which preprocessor to run.\n" +
                                        " e - [n]eqn  p - pic     t - tbl\n" +
                                        " g - grap    r - refer   v - vgrind").
                              AddOption("V", "version", false, "show version.").
                              AddOption("h", "help", false, "show this usage message.");

            HelpFormatter hf = new HelpFormatter();

            hf.PrintHelp(options, new HelpSettings {
                CommandLineSyntax = cmdLine
            }, Console.Out, false);
        }
Пример #10
0
        public void PrintSortedUsageWithNullComparer()
        {
            Options opts = new Options();

            opts.AddOption(new Option("a", "first"));
            opts.AddOption(new Option("b", "second"));
            opts.AddOption(new Option("c", "third"));

            HelpFormatter helpFormatter = new HelpFormatter();

            StringWriter output = new StringWriter();

            helpFormatter.PrintUsage(opts, new HelpSettings {
                Width = 80, CommandLineSyntax = "app", OptionComparer = null
            }, output);

            Assert.AreEqual("usage: app [-a] [-b] [-c]" + Eol, output.ToString());
        }
Пример #11
0
        public void PrintUsage()
        {
            var optionA = new Option("a", "first");
            var optionB = new Option("b", "second");
            var optionC = new Option("c", "third");
            var opts    = new Options();

            opts.AddOption(optionA);
            opts.AddOption(optionB);
            opts.AddOption(optionC);
            var helpFormatter = new HelpFormatter();
            var bytesOut      = new MemoryStream();
            var printWriter   = new StreamWriter(bytesOut);

            helpFormatter.PrintUsage(opts, new HelpSettings {
                Width = 80, CommandLineSyntax = "app"
            }, printWriter);
            printWriter.Close();
            Assert.AreEqual("usage: app [-a] [-b] [-c]" + Eol, Encoding.UTF8.GetString(bytesOut.ToArray()));
        }
Пример #12
0
        public void PrintOptionWithEmptyArgNameUsage()
        {
            Option option = new Option("f", true, null);

            option.ArgumentName = "";
            option.IsRequired   = true;

            Options options = new Options();

            options.AddOption(option);

            StringWriter output = new StringWriter();

            HelpFormatter formatter = new HelpFormatter();

            formatter.PrintUsage(options, new HelpSettings {
                ArgumentName = null, CommandLineSyntax = "app", Width = 80
            }, output);

            Assert.AreEqual("usage: app -f" + Eol, output.ToString());
        }
Пример #13
0
        public void PrintOptionGroupUsage()
        {
            OptionGroup group = new OptionGroup();

            group.AddOption(OptionBuilder.New().Create("a"));
            group.AddOption(OptionBuilder.New().Create("b"));
            group.AddOption(OptionBuilder.New().Create("c"));

            Options options = new Options();

            options.AddOptionGroup(group);

            StringWriter output = new StringWriter();

            HelpFormatter formatter = new HelpFormatter();

            formatter.PrintUsage(options, new HelpSettings {
                Width = 80, CommandLineSyntax = "app"
            }, output);

            Assert.AreEqual("usage: app [-a | -b | -c]" + Eol, output.ToString());
        }
Пример #14
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());
        }