public void AddOptionDependency_OptionNotDefined()
        {
            var ps = new ProgramSettings("command");

            ps.AddOption('o', "option", false);
            Assert.Throws <ConstraintException>(delegate
            {
                ps.AddOptionDependency("o", "x");
            });
        }
Пример #2
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"));
        }
Пример #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);
            });
        }
        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");
            });
        }
Пример #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 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);
        }