static void Main(string [] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); // For the sake of this example, we're just printing the arguments to the console. for (int i = 0; i < args.Length; i++) { Console.WriteLine("args[{0}] == {1}", i, args[i]); } string paramFileName = string.Empty; bool paramAll = false; var opt = new GetOpt("Chuong trinh ky ten", new[] { new CommandLineOption('s', "save", "Luu file voi ten da duoc quy uoc", ParameterType.String, o => paramFileName = (string)o), new CommandLineOption('a', "all", "Tai ve tat ca cac file", ParameterType.None, o => paramAll = true), }); try { opt.ParseOptions(args); } catch (Exception e) { Console.WriteLine(e.Message); return; } Form1 myForm = new Form1(); myForm.DestinationFileName = paramFileName; // Tim hieu va phan tich bang GetOpt Application.Run(myForm); }
public void CanParseShortParameter() { bool a = false; var opts = new GetOpt("desc", new[] { new CommandLineOption('a', null, "a param", ParameterType.None, o => a = true), }); // Act opts.ParseOptions(new[] { "foo", "-a", "bar" }); // Assert Assert.AreEqual(true, a); }
//[ExpectedException(typeof(CommandLineException), ExpectedMessage = "Missing parameters")] public void StringParamPlusMissingFilenameShouldGenerateError() { var opts = new GetOpt("desc", new[] { new CommandLineOption('s', null, "a param", ParameterType.String, null), new CommandLineOption("filename", ParameterType.String, null), }); var exception = Assert.Throws <CommandLineException>(() => opts.ParseOptions(new[] { "-s", "sparam" })); Assert.AreEqual("Missing parameters", exception.Message); }
static void Main(string[] args) { GetOpt opts = configOpts(); opts.ParseOptions(args); if (projectOpt.Equals(String.Empty)) { opts.ShowUsage(); } else { generate(); } }
private static void Main(string[] args) { var opts = new GetOpt("This application clones a website into a local folder, rewriting URLs as needed.", new[] { new CommandLineOption('o', "outputPath", "The folder where the downloaded pages will be written", ParameterType.String, value => SETTINGS.OutputPath = (string)value), } ); opts.ParseOptions(args); var downloader = new SiteDownloader(); if (opts.AdditionalParameters.Any()) { downloader.Download(opts.AdditionalParameters[0], SETTINGS); } }
public void CanParseMultipleShortParametersSeparated() { bool a = false, b = true, c = false; var opts = new GetOpt("desc", new[] { new CommandLineOption('a', null, "a param", ParameterType.None, o => a = true), new CommandLineOption('b', null, "a param", ParameterType.None, o => b = false), new CommandLineOption('c', null, "a param", ParameterType.None, o => c = true), }); // Act opts.ParseOptions(new[] { "foo", "-a", "bar", "-b", "-c" }); // Assert Assert.AreEqual(true, a); Assert.AreEqual(false, b); Assert.AreEqual(true, c); }
public void CanUseEqualToAssignValues() { string s = null; int num = 0; var opts = new GetOpt("desc", new[] { new CommandLineOption('s', null, "a param", ParameterType.String, o => s = (string)o), new CommandLineOption('\0', "num", "a number", ParameterType.Integer, o => num = (int)o), }); // Act opts.ParseOptions(new[] { "--num=32", "-s=sparam" }); // Assert Assert.AreEqual("sparam", s); Assert.AreEqual(32, num); }
public void CanParseSeveralUnnamed() { string s = null, f = null; var opts = new GetOpt("desc", new[] { new CommandLineOption("filename", ParameterType.String, o => s = (string)o), new CommandLineOption("filename2", ParameterType.String, o => f = (string)o), }); // Act opts.ParseOptions(new[] { "string1", "string2", "ignored" }); // Assert Assert.AreEqual("string1", s); Assert.AreEqual("string2", f); Assert.AreEqual(1, opts.AdditionalParameters.Count); Assert.AreEqual("ignored", opts.AdditionalParameters[0]); }
private static void Main(string[] args) { string Server = "localhost"; string DatabaseName = null; string ConnectionString = null; string Table = null; string Query = null; string Format = null; bool Follow = false; int SleepInterval = 200; bool Retry = false; var opts = new GetOpt("Tail a database query just like you tail a file", new[] { new CommandLineOption('S', "server", "Database Server [localhost]", ParameterType.String, o => Server = (string)o), new CommandLineOption('d', "database", "Database Name", ParameterType.String, o => DatabaseName = (string)o), new CommandLineOption('c', "connectionstring", "Connection String (Overrides --server and --database)", ParameterType.String, o => ConnectionString = (string)o), new CommandLineOption('t', "table", "Table Name to Query", ParameterType.String, o => Table = (string)o), new CommandLineOption('q', "query", "Custom SQL Query (Overrides --table)", ParameterType.String, o => Query = (string)o), new CommandLineOption('F', "format", "Custom String.Format output", ParameterType.String, o => Format = (string)o), new CommandLineOption('f', "follow", "Follow output as it gets added", ParameterType.None, o => Follow = true), new CommandLineOption('s', "sleep-interval", "Sleep interval in ms [200]", ParameterType.Integer, o => SleepInterval = (int)o), new CommandLineOption('r', "retry", "keep trying to query if it is unsuccessful", ParameterType.None, o => Retry = true) }); opts.ParseOptions(args); var valid = true; if (String.IsNullOrEmpty(ConnectionString) && String.IsNullOrEmpty(DatabaseName)) { Console.Error.WriteLine("Database connection information required."); valid = false; } if (String.IsNullOrEmpty(Table) && String.IsNullOrEmpty(Query)) { Console.Error.WriteLine("Table Name or Custom SQL Query required."); valid = false; } if (!valid) { opts.ShowUsage(); return; } bool ThrewException = false; do { ThrewException = false; try { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConnectionString ?? String.Format("Server={0};Database={1};Trusted_Connection=true", Server, DatabaseName); conn.Open(); var cmd = new SqlCommand(Query ?? String.Format("SELECT * FROM {0}", Table), conn); string lastRow = null; var triggered = true; do { using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { var values = new object[reader.FieldCount]; reader.GetValues(values); var currentRow = String.IsNullOrEmpty(Format) ? String.Join(" ", values) : String.Format(Format, values); if (!triggered) { if (lastRow == currentRow) { triggered = true; } continue; } Console.WriteLine(currentRow); lastRow = currentRow; } triggered = false; } if (Follow) { Thread.Sleep(TimeSpan.FromMilliseconds(SleepInterval)); } } while (Follow); } } catch (Exception e) { Console.Error.WriteLine(e.Message); ThrewException = true; } if (Retry && ThrewException) { Thread.Sleep(TimeSpan.FromMilliseconds(SleepInterval)); } } while (Retry && ThrewException); }
/// <summary> /// Main entry /// </summary> /// <param name="args"> Command line parameters </param> public static void GetOptMain(string[] args) { var separator = "|"; var field = 0; bool verbose = false, numeric = false; string file = null, file2 = null; var opts = new GetOpt( "Sample application that sorts input rows based on a delimeted field", new[] { new CommandLineOption('s', "separator", "Field separator", ParameterType.String, o => separator = (string)o), new CommandLineOption('v', "verbose", "Show more info about found files", ParameterType.None, o => verbose = true), new CommandLineOption('V', null, "Show version", ParameterType.None, o => Console.WriteLine("Version: 1.0")), new CommandLineOption('B', null, "set B", ParameterType.String, o => Console.WriteLine("Version: 1.0")), new CommandLineOption('\0', "numeric", "sort numerically", ParameterType.None, o => numeric = true), new CommandLineOption('f', "field", "Which field to sort by. Default = 0", ParameterType.Integer, o => field = (int)o), new CommandLineOption("file", ParameterType.String, o => file = (string)o), new CommandLineOption("file2", ParameterType.String, o => file2 = (string)o, true), }); try { opts.ParseOptions(args); } catch (CommandLineException e) { Console.WriteLine("Error: {0}", e.Message); return; } GetOpt.Description("Sample application that sorts input rows based on a delimeted field") .Parameter('v', "verbose", () => verbose = true) .Parameter('s', "separator", o => separator = o) .Parameter('f', "field", o => field = o) .Parameter(o => file = o, "file"); if (verbose) { Console.WriteLine("Starting..."); } // Read given file or standard input, split it up according to delimiter and sort by given field. No error handling, this is an example ;) StreamReader input = file != null ? new StreamReader(file) : new StreamReader(Console.OpenStandardInput()); string line; var s = new List <Tuple <string, string> >(); while ((line = input.ReadLine()) != null) { var key = line.Split(separator[0])[field]; s.Add(new Tuple <string, string>(key, line)); } foreach (var linepair in numeric ? s.OrderBy(x => int.Parse(x.Item1)) : s.OrderBy(x => x.Item1)) { Console.WriteLine(linepair.Item2); } if (opts.AdditionalParameters.Count > 1) { // Handle additional files here foreach (var additionalParameter in opts.AdditionalParameters) { Console.WriteLine("Another parameter '{0}' was included", additionalParameter); } } }
static void Main(string[] args) { XmlDiffOptions xDiffOptions = new XmlDiffOptions(); string fromFile = string.Empty; string toFile = string.Empty; string outFile = string.Empty; bool toCsv = false; var options = new GetOpt("XmlDiff: Tool for finding the difference between two Xml files.", new[] { new CommandLineOption('o', "outfile", "Output file to write to. Files with csv extension open in Excel.", ParameterType.String, o => outFile = (string)o), new CommandLineOption('\0', "csv", "Creates a diff csv file and opens in Excel. If no outfile is specified writes output to xmldiff.csv. Default=False", ParameterType.None, none => toCsv = true), new CommandLineOption('m', "nomatch", "Don't match text node value types (i.e. 0.00 != 0). Default=False", ParameterType.None, none => xDiffOptions.MatchValueTypes = false), new CommandLineOption('\0', "ignoretypes", "If -m or --nomatch is NOT chosen, then this chooses which match types to ignore. " + "Possible values are (string, integer, double, datetime). Multiple values may be separated by '|'", ParameterType.String, (types) => { string[] values = ((string)types).Split('|'); foreach (string value in values) { switch (value.ToLower().Trim()) { case "string": xDiffOptions.IgnoreTextTypes.Add(XmlDiffOptions.IgnoreTextNodeOptions.XmlString); break; case "integer": xDiffOptions.IgnoreTextTypes.Add(XmlDiffOptions.IgnoreTextNodeOptions.XmlInteger); break; case "double": xDiffOptions.IgnoreTextTypes.Add(XmlDiffOptions.IgnoreTextNodeOptions.XmlDouble); break; case "datetime": xDiffOptions.IgnoreTextTypes.Add(XmlDiffOptions.IgnoreTextNodeOptions.XmlDateTime); break; default: throw new CommandLineException("Error parsing enumerated values.", "ignoretypes"); } } }), new CommandLineOption('d', "nodetail", "Will not display details of matching nodes. Default=False", ParameterType.None, none => xDiffOptions.MatchDescendants = false), new CommandLineOption('c', "case", "Case Sensitive. Default=False", ParameterType.None, none => xDiffOptions.IgnoreCase = false), new CommandLineOption('\0', "2way", "Does a comparison in both directions. Default=False", ParameterType.None, none => xDiffOptions.TwoWayMatch = true), new CommandLineOption("Required. FromFile", ParameterType.String, file => fromFile = (string)file), new CommandLineOption("Required. ToFile", ParameterType.String, file => toFile = (string)file) }); try { options.ParseOptions(args); } catch (CommandLineException ex) { Console.WriteLine("Error: {0}", ex.Message); return; } StreamWriter sw; XmlDiff xdiff; try { xdiff = new XmlDiff(File.ReadAllText(fromFile), File.ReadAllText(toFile)); xdiff.CompareDocuments(xDiffOptions); } catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message); return; } if (toCsv) { try { string file; if (!string.IsNullOrEmpty(outFile)) { file = outFile; } else { file = "xmldiff.csv"; } sw = new StreamWriter(file); sw.Write((toCsv) ? xdiff.ToCSVString() : xdiff.ToJsonString()); sw.Close(); Process.Start(file); } catch (IOException ex) { Console.WriteLine("Error: {0}", ex.Message); return; } } else { if (string.IsNullOrEmpty(outFile)) { Console.WriteLine(xdiff.ToJsonString()); } else { try { sw = new StreamWriter(outFile); sw.WriteLine(xdiff.ToJsonString()); sw.Close(); } catch (IOException ex) { Console.WriteLine("Error: {0}", ex.Message); return; } } } }