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); }
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 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)); }
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)); }
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 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 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 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); }