public void allowedAnyFact() { CommandLineStringOption format = new CommandLineStringOption("format"); parser.AddOption(format); string expectedValue = "Value of N is %7d\n"; parser.Parse(new string[] { "--format=" + expectedValue }); Assert.Equal(expectedValue, format.Value); }
public void emptyParameterAllowedFact() { CommandLineStringOption view = new CommandLineStringOption("view"); view.AllowedValues.Add(""); parser.AddOption(view); parser.Parse(new string[] { "--view=" }); Assert.Equal("", view.Value); }
public void caseSensitiveThrowsException() { CommandLineStringOption view = new CommandLineStringOption("view"); view.AllowedValues.Add("standard"); parser.AddOption(view); Assert.Throws<ParsingException>( delegate { parser.Parse(new string[] { "--view=STANDARD" }); }); }
public void allowedSomeFact() { CommandLineStringOption view = new CommandLineStringOption("view"); view.AllowedValues.Add("scientific"); view.AllowedValues.Add("programmer"); view.AllowedValues.Add("standard"); parser.AddOption(view); parser.Parse(new string[] { "--view=standard" }); Assert.Equal("standard", view.Value); }
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; }
static void TestRequiredOption() { { CommandLineParser parser = new CommandLineParser(); try { CommandLineStringOption strOption = new CommandLineStringOption("str", "s1"); TestAssert(false); } catch (ConfigurationException) { TestAssert(true); } } { CommandLineParser parser = new CommandLineParser(); CommandLineStringOption strOption = new CommandLineStringOption("str", "s"); strOption.Required = true; parser.AddOption(strOption); try { strOption.ShortName = "s1"; TestAssert(false); } catch (ConfigurationException) { TestAssert(true); } } { CommandLineParser parser = new CommandLineParser(); CommandLineStringOption strOption = new CommandLineStringOption("str", "s"); strOption.Required = true; parser.AddOption(strOption); try { strOption.Name = ""; TestAssert(false); } catch (ConfigurationException) { TestAssert(true); } } { CommandLineParser parser = new CommandLineParser(); CommandLineStringOption strOption = new CommandLineStringOption("str", "s"); strOption.Required = true; parser.AddOption(strOption); try { strOption.ShortName = ""; TestAssert(false); } catch (ConfigurationException) { TestAssert(true); } } }
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"); }
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"); } }
static void TestConfigExceptions() { CommandLineParser parser = new CommandLineParser(); try { CommandLineStringOption strOption = new CommandLineStringOption("str", "s1"); TestAssert(false); } catch (Exception e) { TestAssert(e.GetType() == typeof(ConfigurationException)); } try { CommandLineStringOption strOption2 = new CommandLineStringOption(null); TestAssert(false); } catch (Exception e) { TestAssert(e.GetType() == typeof(ConfigurationException)); } }
public void missingParameterTypeOptionalFact() { CommandLineStringOption view = new CommandLineStringOption("view"); view.ParameterType = ParameterType.Optional; parser.AddOption(view); parser.Parse(new string[] { "--view" }); Assert.Null(view.Value); }
public void notAllowedThrowsException() { CommandLineStringOption view = new CommandLineStringOption("view"); view.AllowedValues.Add("scientific"); view.AllowedValues.Add("programmer"); view.AllowedValues.Add("standard"); parser.AddOption(view); Assert.Throws<ParsingException>( delegate { parser.Parse(new string[] { "--view=statistics" }); }); }
public void remembersAllowedValues() { CommandLineStringOption view = new CommandLineStringOption("view"); view.AllowedValues.Add("scientific"); view.AllowedValues.Add("programmer"); view.AllowedValues.Add("standard"); /* in different order */ string[] expectedAllowedValues = new string[] { "programmer", "scientific", "standard" }; Assert.Equal(expectedAllowedValues, view.AllowedValues, new CollectionEquivalenceComparer<string>()); }
public void parameterTypeOptionalFact() { CommandLineStringOption view = new CommandLineStringOption("view"); view.ParameterType = ParameterType.Optional; parser.AddOption(view); parser.Parse(new string[] { "--view", "standard" }); Assert.Equal("standard", view.Value); }
public void nullParameterAllowedFact() { CommandLineStringOption view = new CommandLineStringOption("view"); parser.AddOption(view); parser.Parse(new string[] { "--view", null }); Assert.Null(view.Value); }
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"); }
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)); }
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); }