public void nullNameThrowsException()
 {
     Assert.Throws<CommandLine.ConfigurationException>(
         delegate
         {
             CommandLineBoolOption version = new CommandLineBoolOption(null);
         });
 }
 public void multicharOptionShortNameThrowsException()
 {
     Assert.Throws<CommandLine.ConfigurationException>(
         delegate
         {
             CommandLineBoolOption version = new CommandLineBoolOption("version", "version");
         });
 }
        public void notPresentValueAlwaysNull()
        {
            CommandLineBoolOption verbose = new CommandLineBoolOption("verbose");
            parser.AddOption(verbose);

            parser.Parse(new string[] { });

            Assert.Null(verbose.Value);
        }
        public void presentParameterThrowsException()
        {
            CommandLineBoolOption verbose = new CommandLineBoolOption("verbose");
            parser.AddOption(verbose);

            Assert.Throws<ParsingException>(
                delegate
                {
                    parser.Parse(new string[] { "--verbose=true" });
                });
        }
        public void parameterTypeRequiredThrowsException()
        {
            CommandLineBoolOption verbose = new CommandLineBoolOption("verbose", "v");
            verbose.ParameterType = ParameterType.Required;
            parser.AddOption(verbose);

            Assert.Throws<ParsingException>(
                delegate
                {
                    parser.Parse(new string[] { "-v", "true"});
                });
        }
예제 #6
0
파일: Program.cs 프로젝트: miryn/DPP
        static CommandLineParser GetGnuTimeParser()
        {
            CommandLineParser parser = new CommandLineParser();

            var format = new CommandLineStringOption("format", "f");
            format.Help = "Specify output format, possibly overriding the format specified in the environment variable TIME.";
            format.ExpectedValue = "format";
            parser.AddOption(format);

            var portability = new CommandLineBoolOption("portability", "p");
            portability.Help = "Use the portable output format.";
            parser.AddOption(portability);

            var output = new CommandLineStringOption("output", "o");
            output.Help = "Do not send the results to stderr, but overwrite the specified file.";
            output.ExpectedValue = "file";
            parser.AddOption(output);

            var append = new CommandLineBoolOption("append", "a");
            output.Help = "(Used together with -o.) Do not overwrite but append.";
            parser.AddOption(append);

            var verbose = new CommandLineBoolOption("verbose", "v");
            verbose.Help = "Give very verbose output about all the program knows about.";
            parser.AddOption(verbose);

            var help = new CommandLineBoolOption("help");
            help.Help = "Print a usage message on standard output and exit successfully.";
            parser.AddOption(help);

            var version = new CommandLineBoolOption("version", "V");
            version.Help = "Print version information on standard output, then exit successfully.";
            parser.AddOption(version);

            return parser;
        }
예제 #7
0
파일: Program.cs 프로젝트: miryn/DPP
        static void Main(string[] args)
        {
            TestGnuTime();

            TestValueType();

            TestRequiredAndOptionalParameters();

            TestExceptions();

            TestOptionalParameters();

            TestTerminator();

            TestParsingException();

            TestConfigExceptions();

            TestRequiredOption();

            //args = new string[] { "--listen",  "--port", "80", "--hello=world" };
            args = new string[] { "-p", "80", "-l", "naky", "navic" };
            //args = new string[] { "-lp80" };
            //args = new string[] { "--port=100000" };
            //args = new string[] { "--hello=world" };

            CommandLineParser parser = new CommandLineParser();

            CommandLineIntOption p = new CommandLineIntOption("port", "p");
            p.MaxValue = 65535;
            p.Delegate = x => Console.WriteLine("Já jsem delegát: " + x.Value);
            parser.AddOption(p);

            CommandLineBoolOption l = new CommandLineBoolOption(name: "listen", shortName: "l");
            parser.AddOption(l);

            CommandLineStringOption s = new CommandLineStringOption("hello");
            s.AllowedValues.AddRange(new string[] { "abc", "def", "ghi" });
            parser.AddOption(s);

            List<string> extraParameters;

            //try
            {
                // run the parser
                extraParameters = parser.Parse(args);
            }

            /*catch (ParsingException e)
            {
                Console.WriteLine(e.Message);
                return;
            }*/
            /*catch (OptionNotFoundException e)
            {
                Console.WriteLine(e.Message);
                return;
            }*/

            Console.WriteLine("p.Value: " + p.Value);
            Console.WriteLine("l.Present: " + l.Present);
            Console.WriteLine("s.Value: " + s.Value);
            Console.WriteLine("extraParameters: " + string.Join(",", extraParameters));
        }
예제 #8
0
파일: Program.cs 프로젝트: miryn/DPP
        static void TestRequiredAndOptionalParameters()
        {
            CommandLineParser p = new CommandLineParser();

            var s = new CommandLineStringOption("string", "s");
            s.ParameterType = ParameterType.Optional;
            p.AddOption(s);

            var b = new CommandLineBoolOption("bool", "b");
            p.AddOption(b);

            var args = new string[] { "--string", "dalsi", "hodnota" };
            var extraParameters = p.Parse(args);

            TestAssert(s.Value == "dalsi");
            TestAssert(b.Present == false);
            TestAssert(extraParameters.Count == 1);
            TestAssert(extraParameters[0] == "hodnota");

            args = new string[] { "--string", "-b", "dalsi", "hodnota" };
            extraParameters = p.Parse(args);

            TestAssert(s.Value == "-b");
            TestAssert(b.Present == false);
            TestAssert(extraParameters.Count == 2);
            TestAssert(extraParameters[0] == "dalsi");
            TestAssert(extraParameters[1] == "hodnota");

            args = new string[] { "-b", "--string", "dalsi", "hodnota" };
            extraParameters = p.Parse(args);

            TestAssert(s.Value == "dalsi");
            TestAssert(b.Present == true);
            TestAssert(extraParameters.Count == 1);
            TestAssert(extraParameters[0] == "hodnota");
        }
예제 #9
0
파일: Program.cs 프로젝트: miryn/DPP
        static void TestExceptions()
        {
            CommandLineParser p = new CommandLineParser();

            /*
            var s = new CommandLineBoolOption("bool");
            try
            {
                s.ParameterType = ParameterType.Optional;
                TestAssert(false);
            }
            catch (Exception e)
            {
                TestAssert(e.GetType() == typeof(ParsingException));
            }

            try
            {
                s.ParameterType = ParameterType.Required;
                TestAssert(false);
            }
            catch (Exception e)
            {
                TestAssert(e.GetType() == typeof(ParsingException));
            }

            try
            {
                s.ParameterType = (ParameterType)10;
                TestAssert(false);
            }
            catch (Exception e)
            {
                TestAssert(e.GetType() == typeof(ParsingException));
            }

            try
            {
                s.ParameterType = (ParameterType)10;
                TestAssert(false);
            }
            catch (Exception e)
            {
                TestAssert(e.GetType() == typeof(ParsingException));
            }

            p.AddOption(s);

            try
            {
                p.AddOption(s);
                TestAssert(false);
            }
            catch (Exception e)
            {
                TestAssert(e.GetType() == typeof(ParsingException));
            }
            */

            {
                p = new CommandLineParser();
                CommandLineBoolOption b1 = new CommandLineBoolOption("bool1", "q");
                CommandLineBoolOption b2 = new CommandLineBoolOption("bool2", "w");
                p.AddOption(b1);
                p.AddOption(b2);

                try
                {
                    b2.Name = "bool1"; // exception
                    //TestAssert(false);
                }
                catch (Exception e)
                {
                    TestAssert(e.GetType() == typeof(ConfigurationException));
                }

                try
                {
                    b2.ShortName = "q"; // exception
                    //TestAssert(false);
                }
                catch (Exception e)
                {
                    TestAssert(e.GetType() == typeof(ConfigurationException));
                }

                b2.Name = "bool3";

                p.Parse(new string[] { "--bool3" });
                TestAssert(b1.Present == false);
                TestAssert(b2.Present == true);
            }

            {
                p = new CommandLineParser();
                CommandLineBoolOption b1 = new CommandLineBoolOption("bool1", "q");
                CommandLineBoolOption b2 = new CommandLineBoolOption("bool2", "q");
                p.AddOption(b1);

                try
                {
                    p.AddOption(b2); // exception
                    //TestAssert(false);
                }
                catch (Exception e)
                {
                    TestAssert(e.GetType() == typeof(ConfigurationException));
                }
            }
        }
        public void sameOptionNameThrowsException()
        {
            string sameOptionName = "verbose";
            CommandLineBoolOption verbose = new CommandLineBoolOption(sameOptionName);
            parser.AddOption(verbose);

            CommandLineBoolOption version = new CommandLineBoolOption(sameOptionName);

            Assert.Throws<CommandLine.ConfigurationException>(
                delegate
                {
                    parser.AddOption(version);
                });
        }
예제 #11
0
        public Time(string[] args)
        {
            CommandLineParser parser = new CommandLineParser();

            var format = new CommandLineStringOption("format", "f");
            format.Help = "Specify output format, possibly overriding the format specified in the environment variable TIME.";
            format.ExpectedValue = "format";
            parser.AddOption(format);
            format.ShortName = "X";

            var portability = new CommandLineBoolOption("portability", "p");
            portability.Help = "Use the portable output format.";
            //parser.AddOption(portability);

            var output = new CommandLineStringOption("output", "o");
            output.Help = "Do not send the results to stderr, but overwrite the specified file.";
            output.ExpectedValue = "file";
            output.Required = true;
            //parser.AddOption(output);

            var append = new CommandLineBoolOption("append", "a");
            append.Help = "(Used together with -o.) Do not overwrite but append.";
            //parser.AddOption(append);

            var verbose = new CommandLineBoolOption("verbose", "v");
            verbose.Help = "Give very verbose output about all the program knows about.";
            verbose.Required = true;
            //verbose.ParameterType = ParameterType.Required;
            //parser.AddOption(verbose);

            var help = new CommandLineBoolOption("help");
            help.Help = "Print a usage message on standard output and exit successfully.";
            //parser.AddOption(help);

            var version = new CommandLineBoolOption("version", "V");
            version.Help = "Print version information on standard output, then exit successfully.";
            //parser.AddOption(version);

            List<string> extraParameters;

            try
            {
                extraParameters = parser.Parse(args);
            }
            catch (ParsingException ex)
            {
                if (ex.Option != null)
                {
                    Console.WriteLine("An error occurred in parameter " + ex.Option.Name);
                }
                Console.WriteLine("Message: " + ex.Message);
                return;
            }

            if ((args.Length == 0) || (help.Present))
            {
                parser.PrintHelp();
                return;
            }

            Console.WriteLine("Format: " + format.Value);
            Console.WriteLine("Verbose: " + verbose.Present);
            Console.WriteLine("Output: " + output.Value);
        }