Пример #1
0
        public void DuplicitLongNames()
        {
            var s = new ProgramSettings("program");

            s.AddOption(null, "aaa", false);
            s.AddOption('a', "aaa", false);
        }
        public void AddOptionConflict_ConflictingOptionDependency()
        {
            var ps = new ProgramSettings("command");

            ps.AddOption('a', "aOption", false);
            ps.AddOption('b', "bOption", false);
            ps.AddOptionDependency("a", "b");
            Assert.Throws <ConstraintException>(delegate
            {
                ps.AddOptionConflict("a", "b");
            });
        }
Пример #3
0
        public void Parse_OptionDependencyErrorSecond()
        {
            ProgramSettings ps = new ProgramSettings("command");

            ps.AddOption(new string[] { "a" }, true);
            ps.AddOption(new string[] { "b" }, true);
            ps.AddOptionDependency("a", "b");
            Assert.Throws <ParseException>(delegate
            {
                Parser.Parse("command -b", ps);
            });
        }
Пример #4
0
        public void Parse_ConflictingOptions()
        {
            ProgramSettings ps = new ProgramSettings("command");

            ps.AddOption(new string[] { "a" }, true);
            ps.AddOption(new string[] { "b" }, true);
            ps.AddOptionConflict("a", "b");
            Assert.Throws <ParseException>(delegate
            {
                Parser.Parse("command -a -b", ps);
            });
        }
Пример #5
0
        public ParseResultTests()
        {
            ps = new ProgramSettings("command");
            ps.AddOption('a', "aopt", false, new IntParameter("name", true, 0, 10));
            ps.AddOption('b', "bopt", false, new IntParameter("name", true, -5, 10));
            ps.AddOptionDependency("a", "b");

            var domain = new List <string>
            {
                "some", "string", "params"
            };

            ps.AddOption('s', "sopt", false, new StringParameter("name", true, domain));
        }
Пример #6
0
        public void MandatoryOptionMissing()
        {
            var settings = new ProgramSettings("program");

            settings.AddOption('x', "xx", true);
            Parser.Parse(new [] { "b", "c", "hello" }, settings);
        }
Пример #7
0
        public void MandatoryOptionMissingNoArgs()
        {
            var settings = new ProgramSettings("program");

            settings.AddOption('x', "xx", true);
            Parser.Parse(new string[0], settings);
        }
Пример #8
0
        public void ParameterWithCrazyCharacters()
        {
            var settings = new ProgramSettings("program");

            settings.AddOption('h', "hh", true, new StringParameter("par", true));
            var result = Parser.Parse(new[] { "-h", "a-b$*^v78*/-_ěščř", "--", "abraka", "dabra" }, settings);

            Assert.IsTrue(result.WasParsed("hh"));
        }
Пример #9
0
        public void NegativeIntegerParameter()
        {
            var settings = new ProgramSettings("program");

            settings.AddOption('h', "hh", true, new IntParameter("par", true, -10, 10));
            var result = Parser.Parse(new[] { "-h=-5", "--", "abraka", "dabra" }, settings);

            Assert.IsTrue(result.WasParsed("hh"));
        }
        public void AddOption_DuplicateNamesCollection()
        {
            var ps = new ProgramSettings("command");

            Assert.Throws <ArgumentException>(delegate
            {
                ps.AddOption(new string[] { "a", "a" }, false);
            });
        }
        public void AddOption_DuplicateNames()
        {
            var ps = new ProgramSettings("command");

            Assert.Throws <ArgumentException>(delegate
            {
                ps.AddOption('a', "a", false);
            });
        }
        public void AddOption_NoNameCollection()
        {
            var ps = new ProgramSettings("command");

            Assert.Throws <ArgumentException>(delegate
            {
                ps.AddOption(null, false);
            });
        }
        public void AddOptionDependency_OptionNotDefined()
        {
            var ps = new ProgramSettings("command");

            ps.AddOption('o', "option", false);
            Assert.Throws <ConstraintException>(delegate
            {
                ps.AddOptionDependency("o", "x");
            });
        }
Пример #14
0
        public void ParameterWithCommasJoint()
        {
            var settings = new ProgramSettings("program");

            settings.AddOption('h', "hh", true, new StringParameter("par", true));
            var result = Parser.Parse(new[] { "-h=1,2,3", "--", "abraka", "dabra" }, settings);

            Assert.IsTrue(result.WasParsed("hh"));
            Assert.AreEqual((string)result.GetParameterValue("h"), "1,2,3");
        }
        public void AddOptionConflict_Synonyms()
        {
            var ps = new ProgramSettings("command");

            ps.AddOption('o', "option", false);
            Assert.Throws <ConstraintException>(delegate
            {
                ps.AddOptionConflict("o", "option");
            });
        }
Пример #16
0
        public void Parse_WrongNumberOfOptionParameters()
        {
            ProgramSettings ps = new ProgramSettings("command");

            ps.AddOption(new string[] { "intOption" }, true, new IntParameter("name", true, 0, 10));
            Assert.Throws <ParseException>(delegate
            {
                Parser.Parse("command -intOption", ps);
            });
        }
Пример #17
0
        public void Parse_MandatoryOptionMissing()
        {
            ProgramSettings ps = new ProgramSettings("command");

            ps.AddOption(new string[] { "mandatoryOption" }, true);
            Assert.Throws <ParseException>(delegate
            {
                Parser.Parse("", ps);
            });
        }
Пример #18
0
        private static void runTimeCommandExample()
        {
            ProgramSettings s = new ProgramSettings("time");

            s.AddOption('f', "format", false, new StringParameter("FORMAT", true),
                        "Specify output format, possibly overriding the format specified in the environment variable TIME.");
            s.AddOption('p', "portability", false, null,
                        "Use the portable output format.");
            s.AddOption('o', "output", false, new StringParameter("FILE", true),
                        "Do not send the results to stderr, but overwrite the specified file.");
            s.AddOption('a', "append", false, null,
                        "(Used together with -o.) Do not overwrite but append.");
            s.AddOption('v', "verbose", false, null,
                        "Give very verbose output about all the program knows about.");
            s.AddOption('V', "version", false, null,
                        "Print version information on standard output, then exit successfully.");
            s.AddOption(null, "help", false, null,
                        "Print a usage message on standard output and exit successfully.");

            s.AddOptionDependency("a", "o"); // both of these options must be present if first option is present

            string      cmd    = "time -o output_file -a --verbose -- first_file second_file";
            ParseResult result = Parser.Parse(cmd, s);

            // get parameter value for specific option (cast needed)
            string output = (string)result.GetParameterValue("o");

            output = (string)result.GetParameterValue("output");

            // find out whether this option was present (good for non-parametric /Boolean/ options)
            result.WasParsed("v"); // true
            result.WasParsed("p"); // false

            // enumerate all parsed options
            foreach (var item in result.ParsedOptions)
            {
                if (item.Names.Contains("v"))
                {
                    // ...
                }
                else if (item.Names.Contains("o"))
                {
                    string outFile = (string)item.Value;
                    // printOutput(outFile);
                }
                // ...
            }

            // get all the plain arguments in the order they appeared in the command
            List <string> plainArgs = result.PlainArguments;

            // print help using specified TextWriter
            s.PrintHelp(Console.Out);
            //s.PrintHelp(new System.IO.StreamWriter("myfile.txt"));
        }
        public void PrintHelp()
        {
            string help = "help string";

            var ps = new ProgramSettings("command");

            ps.AddOption('o', "option", false, helpString: help);

            var writer = new System.IO.StringWriter();

            ps.PrintHelp(writer);
            Assert.Contains(help, writer.ToString());
        }
Пример #20
0
        public static ProgramSettings GetTestSetting()
        {
            var settings = new ProgramSettings("program");

            settings.AddOption('a', "aa", false);
            settings.AddOption('b', "bb", false);
            settings.AddOption('c', "cc", false);
            settings.AddOption('d', "dd", false,
                               new IntParameter("PARAM", false, 2, 4));
            settings.AddOption('e', "ee", false,
                               new IntParameter("PARAM", true));
            settings.AddOption('f', "ff", false,
                               new StringParameter("PARAM", false));
            settings.AddOption('g', "gg", false,
                               new StringParameter("PARAM", true, new[] { "hello", "hi" }));

            settings.AddOptionDependency("c", "d");

            return(settings);
        }
Пример #21
0
        public void NoNames()
        {
            var s = new ProgramSettings("program");

            s.AddOption(null, null, false);
        }
 public ParsedOptionTests()
 {
     ps = new ProgramSettings("command");
     ps.AddOption('o', "opt", false, new StringParameter("name", true));
 }
Пример #23
0
        private static void runNumactlExample()
        {
            ProgramSettings settings = new ProgramSettings("numactl", 0, 3);

            // note the user-defined parameter class: IntListParameter
            settings.AddOption('i', "interleave", false, new IntListParameter("Numa nodes", true, 0, 3), "Interleave memory allocation across given nodes.");
            settings.AddOption('p', "preferred", false, new IntParameter("Numa node", true, 0, 3), "Prefer memory allocation from given node.");
            settings.AddOption('m', "membind", false, new IntListParameter("Numa nodes", true, 0, 3), "Allocate memory allocation from given nodes only.");
            settings.AddOption('C', "physcpubind", false, new IntListParameter("CPUs", true, 0, 31), "Run on given CPUs only.");
            settings.AddOption('S', "show", false, null, "Show current NUMA policy.");
            settings.AddOption('H', "hardware", false, null, "Print hardware configuration.");

            // define conflicting options
            settings.AddOptionConflict("m", "p", "i");

            // Invalid usage: conflicting options. Throws ParseException.
            Console.WriteLine("Examples of ParseException...");
            string command = "numactl -i=0,1,3 -p=0-3";

            try
            {
                ParseResult result = Parser.Parse(command, settings);
            }
            catch (ParseException ex)
            {
                Console.WriteLine("COMMAND: " + command);
                Console.WriteLine("ERROR MESSAGE: " + ex.Message);
            }

            command = "numactl -i 0,1,4";
            // Invalid usage: parameter values out of range. Throws ParseException.
            try
            {
                ParseResult result = Parser.Parse(command, settings);
            }
            catch (ParseException ex)
            {
                Console.WriteLine("COMMAND: " + command);
                Console.WriteLine("ERROR MESSAGE: " + ex.Message);
            }
            Console.WriteLine();

            // Valid usage, grouped options are allowed
            try
            {
                ParseResult result = Parser.Parse("numactl -SH", settings);

                result.WasParsed("S");        // true
                result.WasParsed("hardware"); // true
                result.WasParsed("i");        // false
            }
            catch (ParseException ex)
            {
                Console.WriteLine(ex.Message);
            }

            // Valid usage
            try
            {
                ParseResult result = Parser.Parse("numactl -i 0,1,3 --physcpubind 15-17 -S -- my_file", settings);

                // get parameter value for specific option (explicit cast needed)
                List <int> interleave = (List <int>)result.GetParameterValue("i");

                result.WasParsed("S"); // true
                result.WasParsed("H"); // false

                // enumerate all parsed options
                foreach (var item in result.ParsedOptions)
                {
                    if (item.Names.Contains("i"))
                    {
                        // ...
                    }
                    else if (item.Names.Contains("m"))
                    {
                        // ...
                    }
                    // ...
                }

                // get all plain arguments in the order they appeared in the command
                List <string> plainArgs = result.PlainArguments;
            }
            // check documentation to see all the cases when this exception is thrown
            catch (ParseException ex)
            {
                Console.WriteLine(ex.Message);
                settings.PrintHelp(Console.Out);
            }

            // add plain arguments and show their help strings in program documentation
            settings.AddPlainArgument(0, "Output file", "Specify the output file.");
            settings.AddPlainArgument(1, "Output directory", "Specify the output directory.");
            settings.AddPlainArgument(2, "Another argument...");
            settings.PrintHelp(Console.Out);
        }
Пример #24
0
        public void InvalidOptionName()
        {
            var settings = new ProgramSettings("program");

            settings.AddOption('x', "xx666", false);
        }