예제 #1
0
파일: Program.cs 프로젝트: miryn/DPP
        static void Example3(string[] args)
        {
            CommandLineParser parser = new CommandLineParser();
            int intValue = 0;

            CommandLineIntOption intOption = new CommandLineIntOption("number");
            intOption.MinValue = 100;
            intOption.MaxValue = 200;
            intOption.ParameterType = ParameterType.Optional;
            intOption.Delegate = x => intValue = x.Value;
            parser.AddOption(intOption);

            parser.Parse(args);

            Console.WriteLine("Value: " + intValue);
        }
예제 #2
0
        public List <string> Parse(string programName = null)
        {
            var           commandLine = Environment.CommandLine;
            var           parser      = new CommandLineParser(commandLine);
            List <string> result      = null;

            programName = programName ?? UnspecifiedProgramName;

            try
            {
                result = parser.Parse(Matchers);
            }
            catch (PrematureMatchTerminationException e)
            {
                if (HasErrors)
                {
                    DisplayErrors(programName);
                }
                else
                {
                    if (!string.IsNullOrEmpty(e.Message))
                    {
                        Console.WriteLine(e.Message);
                    }
                    Console.WriteLine("{0} {1}", programName, GetHelp());
                    Environment.Exit(DefaultErrorCodeValidCommandLine);
                }
            }

            if (HasErrors)
            {
                DisplayErrors(programName);
            }

            return(result);
        }
예제 #3
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));
        }
예제 #4
0
파일: Program.cs 프로젝트: miryn/DPP
        static void TestValueType()
        {
            CommandLineParser parser = new CommandLineParser();

            CommandLineIntOption intOption = new CommandLineIntOption("int", "i");
            parser.AddOption(intOption);

            var otherParams = parser.Parse(new string[] { "-i", "10" });

            TestAssert(intOption.Value == 10);
            TestAssert(intOption.Value.GetType() == typeof(int));
        }
예제 #5
0
파일: Program.cs 프로젝트: miryn/DPP
        static void TestTerminator()
        {
            CommandLineParser parser = new CommandLineParser();

            CommandLineStringOption strOption = new CommandLineStringOption("str", "s");
            strOption.ParameterType = ParameterType.Optional;
            parser.AddOption(strOption);

            var otherParams = parser.Parse(new string[] { "-s", "ahoj", "--", "-s" });

            Console.WriteLine(strOption.Value);

            TestAssert(otherParams.Count == 1);
            TestAssert(otherParams[0] == "-s");
        }
예제 #6
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");
        }
예제 #7
0
파일: Program.cs 프로젝트: miryn/DPP
        static void TestParsingException()
        {
            CommandLineParser parser = new CommandLineParser();

            CommandLineStringOption strOption = new CommandLineStringOption("str", "s");
            strOption.ParameterType = ParameterType.Required;
            parser.AddOption(strOption);

            try
            {
                var otherParams = parser.Parse(new string[] { "-s" });
                TestAssert(false);
            }
            catch (ParsingException p)
            {
                TestAssert(p.Option.Name == "str");
            }
        }
예제 #8
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));
                }
            }
        }
예제 #9
0
 /// <summary>
 /// Convenience overload when we start parsing at the first element in args[] and use default settings
 /// </summary>
 /// <param name="args">The command line arguments</param>
 /// <returns>A populated instance of the application command line settings class</returns>
 public static ValueTuple <T, List <string> > Parse(string[] args)
 {
     return(CommandLineParser <T> .Parse(args, 0, new Settings()));
 }
예제 #10
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);
        }