コード例 #1
0
ファイル: ParserTestCase.cs プロジェクト: meikeric/deveel-cli
        public void PropertiesOption()
        {
            if (style == ParserStyle.Basic)
            {
                return;
            }

            String[] args = new String[] { "-Jsource=1.5", "-J", "target", "1.5", "foo" };

            Options options = new Options();

            options.AddOption(OptionBuilder.New().WithValueSeparator().HasArguments(2).Create('J'));

            ICommandLine cl = parser.Parse(options, args);

            IList values = cl.GetOptionValues("J");

            Assert.IsNotNull(values, "null values");
            Assert.AreEqual(4, values.Count, "number of values");
            Assert.AreEqual("source", values[0], "value 1");
            Assert.AreEqual("1.5", values[1], "value 2");
            Assert.AreEqual("target", values[2], "value 3");
            Assert.AreEqual("1.5", values[3], "value 4");
            IEnumerable <string> argsleft = cl.Arguments;

            Assert.AreEqual(1, argsleft.Count(), "Should be 1 arg left");
            Assert.AreEqual("foo", argsleft.First(), "Expecting foo");
        }
コード例 #2
0
        public void Ls()
        {
            Options options = new Options();

            options.AddOption("a", "all", false, "do not hide entries starting with .");
            options.AddOption("A", "almost-all", false, "do not list implied . and ..");
            options.AddOption("b", "escape", false, "print octal escapes for nongraphic characters");
            options.AddOption(OptionBuilder.New().WithLongName("block-size")
                              .WithDescription("use SIZE-byte blocks")
                              .HasArgument()
                              .WithArgumentName("SIZE")
                              .Create());
            options.AddOption("B", "ignore-backups", false, "do not list implied entried ending with ~");
            options.AddOption("c", false, "with -lt: sort by, and show, ctime (time of last modification of file status information) with -l:show ctime and sort by name otherwise: sort by ctime");
            options.AddOption("C", false, "list entries by columns");

            String[] args = new String[] { "--block-size=10" };

            // create the command line parser
            ICommandLineParser parser = new PosixParser();

            ICommandLine line = parser.Parse(options, args);

            Assert.IsTrue(line.HasOption("block-size"));
            Assert.AreEqual("10", line.GetOptionValue("block-size").Value);
        }
コード例 #3
0
        public static Options Parse(String pattern)
        {
            char       opt      = ' ';
            bool       required = false;
            OptionType type     = OptionType.None;

            Options options = new Options();

            for (int i = 0; i < pattern.Length; i++)
            {
                char ch = pattern[i];

                // a value code comes after an option and specifies
                // details about it
                if (!IsValueCode(ch))
                {
                    if (opt != ' ')
                    {
                        OptionBuilder builder = new OptionBuilder();
                        builder.HasArgument(type != OptionType.None);
                        builder.IsRequired(required);
                        builder.WithType(type);

                        // we have a previous one to deal with
                        options.AddOption(builder.Create(opt));
                        required = false;
                        type     = OptionType.None;
                        opt      = ' ';
                    }

                    opt = ch;
                }
                else if (ch == '!')
                {
                    required = true;
                }
                else
                {
                    type = getValueClass(ch);
                }
            }

            if (opt != ' ')
            {
                OptionBuilder builder = new OptionBuilder();
                builder.HasArgument(type != OptionType.None);
                builder.IsRequired(required);
                builder.WithType(type);

                // we have a final one to deal with
                options.AddOption(builder.Create(opt));
            }

            return(options);
        }
コード例 #4
0
        public static Options Parse(String pattern)
        {
            char opt = ' ';
            bool required = false;
            OptionType type = OptionType.None;

            Options options = new Options();

            for (int i = 0; i < pattern.Length; i++) {
                char ch = pattern[i];

                // a value code comes after an option and specifies
                // details about it
                if (!IsValueCode(ch)) {
                    if (opt != ' ') {
                        OptionBuilder builder = new OptionBuilder();
                        builder.HasArgument(type != OptionType.None);
                        builder.IsRequired(required);
                        builder.WithType(type);

                        // we have a previous one to deal with
                        options.AddOption(builder.Create(opt));
                        required = false;
                        type = OptionType.None;
                        opt = ' ';
                    }

                    opt = ch;
                } else if (ch == '!') {
                    required = true;
                } else {
                    type = getValueClass(ch);
                }
            }

            if (opt != ' ') {
                OptionBuilder builder = new OptionBuilder();
                builder.HasArgument(type != OptionType.None);
                builder.IsRequired(required);
                builder.WithType(type);

                // we have a final one to deal with
                options.AddOption(builder.Create(opt));
            }

            return options;
        }
コード例 #5
0
ファイル: ParserTestCase.cs プロジェクト: meikeric/deveel-cli
        public void LongWithEqualSingleDash()
        {
            if (style == ParserStyle.Basic)
            {
                return;
            }

            String[] args = new String[] { "-foo=bar" };

            Options options = new Options();

            options.AddOption(OptionBuilder.New().WithLongName("foo").HasArgument().Create('f'));

            ICommandLine cl = parser.Parse(options, args);

            Assert.AreEqual("bar", cl.GetOptionValue("foo").Value);
        }
コード例 #6
0
        public void Ant()
        {
            Options options = new Options();

            options.AddOption("help", false, "print this message");
            options.AddOption("projecthelp", false, "print project help information");
            options.AddOption("version", false, "print the version information and exit");
            options.AddOption("quiet", false, "be extra quiet");
            options.AddOption("verbose", false, "be extra verbose");
            options.AddOption("debug", false, "print debug information");
            options.AddOption("logfile", true, "use given file for log");
            options.AddOption("logger", true, "the class which is to perform the logging");
            options.AddOption("listener", true, "add an instance of a class as a project listener");
            options.AddOption("buildfile", true, "use given buildfile");
            options.AddOption(OptionBuilder.New().WithDescription("use value for given property")
                              .HasArguments()
                              .WithValueSeparator()
                              .Create('D'));
            //, null, true, , false, true );
            options.AddOption("find", true, "search for buildfile towards the root of the filesystem and use it");

            String[] args = new String[] { "-buildfile", "mybuild.xml",
                                           "-Dproperty=value", "-Dproperty1=value1",
                                           "-projecthelp" };

            // use the GNU parser
            ICommandLineParser parser = new GnuParser();

            ICommandLine line = parser.Parse(options, args);

            // check multiple values
            String[] opts = line.GetOptionValues("D");
            Assert.AreEqual("property", opts[0]);
            Assert.AreEqual("value", opts[1]);
            Assert.AreEqual("property1", opts[2]);
            Assert.AreEqual("value1", opts[3]);

            // check single value
            Assert.AreEqual("mybuild.xml", line.GetOptionValue("buildfile").Value);

            // check option
            Assert.IsTrue(line.HasOption("projecthelp"));
        }
コード例 #7
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());
        }
コード例 #8
0
        public void GetOptionProperties()
        {
            string[] args = new String[] { "-Dparam1=value1", "-Dparam2=value2", "-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar" };

            Options options = new Options();

            options.AddOption(OptionBuilder.New().WithValueSeparator().HasOptionalArgs(2).Create('D'));
            options.AddOption(OptionBuilder.New().WithValueSeparator().HasArguments(2).WithLongName("property").Create());

            Parser       parser = new GnuParser();
            ICommandLine cl     = parser.Parse(options, args);

            IDictionary <string, string> props = cl.GetOptionProperties("D");

            Assert.IsNotNull(props, "null properties");
            Assert.AreEqual(4, props.Count, "number of properties in " + props);
            Assert.AreEqual("value1", props["param1"], "property 1");
            Assert.AreEqual("value2", props["param2"], "property 2");
            Assert.AreEqual("true", props["param3"], "property 3");
            Assert.AreEqual("value4", props["param4"], "property 4");

            Assert.AreEqual("bar", cl.GetOptionProperties("property")["foo"], "property with long format");
        }
コード例 #9
0
        public void Groovy()
        {
            Options options = new Options();

            options.AddOption(
                OptionBuilder.New().WithLongName("define").
                WithDescription("define a system property").
                HasArgument(true).
                WithArgumentName("name=value").
                Create('D'));
            options.AddOption(
                OptionBuilder.New().HasArgument(false)
                .WithDescription("usage information")
                .WithLongName("help")
                .Create('h'));
            options.AddOption(
                OptionBuilder.New().HasArgument(false)
                .WithDescription("debug mode will print out full stack traces")
                .WithLongName("debug")
                .Create('d'));
            options.AddOption(
                OptionBuilder.New().HasArgument(false)
                .WithDescription("display the Groovy and JVM versions")
                .WithLongName("version")
                .Create('v'));
            options.AddOption(
                OptionBuilder.New().WithArgumentName("charset")
                .HasArgument()
                .WithDescription("specify the encoding of the files")
                .WithLongName("encoding")
                .Create('c'));
            options.AddOption(
                OptionBuilder.New().WithArgumentName("script")
                .HasArgument()
                .WithDescription("specify a command line script")
                .Create('e'));
            options.AddOption(
                OptionBuilder.New().WithArgumentName("extension")
                .HasOptionalArg()
                .WithDescription("modify files in place; create backup if extension is given (e.g. \'.bak\')")
                .Create('i'));
            options.AddOption(
                OptionBuilder.New().HasArgument(false)
                .WithDescription("process files line by line using implicit 'line' variable")
                .Create('n'));
            options.AddOption(
                OptionBuilder.New().HasArgument(false)
                .WithDescription("process files line by line and print result (see also -n)")
                .Create('p'));
            options.AddOption(
                OptionBuilder.New().WithArgumentName("port")
                .HasOptionalArg()
                .WithDescription("listen on a port and process inbound lines")
                .Create('l'));
            options.AddOption(
                OptionBuilder.New().WithArgumentName("splitPattern")
                .HasOptionalArg()
                .WithDescription("split lines using splitPattern (default '\\s') using implicit 'split' variable")
                .WithLongName("autosplit")
                .Create('a'));

            Parser       parser = new PosixParser();
            ICommandLine line   = parser.Parse(options, new String[] { "-e", "println 'hello'" }, true);

            Assert.IsTrue(line.HasOption('e'));
            Assert.AreEqual("println 'hello'", line.GetOptionValue('e').Value);
        }
コード例 #10
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());
        }