internal static void Main(string[] args) { Console.WriteLine($"demo - a program that has many command line options - version {Assembly.GetExecutingAssembly().GetName().Version}"); Console.WriteLine(); Console.WriteLine($"Command line: {Environment.CommandLine}"); Console.WriteLine(); var settings = new Settings(); var getOpt = new GetOpt(settings); ParsedArguments parsedArguments; try { parsedArguments = getOpt.Parse(args); } catch (GetOptException ex) { ShowExceptionAndExit(ex); return; } if (settings.ShowHelp) { Usage(getOpt); return; } Console.WriteLine("Job files to run : {0}", string.Join(", ", parsedArguments.NonOptions)); Console.WriteLine("Run job : {0}", settings.WorkDay.HasValue ? (object)settings.WorkDay.Value : "ASAP"); Console.WriteLine("Log goes to to : {0}", settings.LogFile); Console.WriteLine("Be verbose : {0}", settings.Verbose ? "yes" : "no"); Console.WriteLine("Send notification to : {0}", string.Join(", ", settings.EmailNotifications)); Console.WriteLine("Job tags : {0}", string.Join(", ", settings.JobTags)); }
public void TestStrOption() { GetOpt parser = new GetOpt(); parser.AddStrOption(flags: new string[] { "-f", "-file" }); Assert.False(parser.IsSet("-f"), "-f option should not be set"); Assert.False(parser.IsSet("-file"), "-file option should not be set"); string[] rest = parser.Parse("-not option -file name bla bla -fbla".Split(" ")); Assert.True(parser.IsSet("-f"), "-f option should be set"); Assert.True(parser.IsSet("-file"), "-file option should be set"); Assert.True("name".Equals(parser.GetOption("-f")), "-f should be 'name'"); Assert.True("name".Equals(parser.GetOption("-file")), "-file should be 'name'"); List <string> options = parser.GetOptions("-f"); Assert.True(options[0].Equals("name"), "-file[0] should be 'name'"); Assert.True(options[1].Equals("bla"), "-file[1] should be 'bla'"); Assert.True("-not".Equals(rest[0]), "rest[0] should be '-not'"); Assert.True("option".Equals(rest[1]), "rest[1] should be 'option'"); Assert.True("bla".Equals(rest[2]), "rest[2] should be 'bla'"); Assert.True("bla".Equals(rest[3]), "rest[3] should be 'bla'"); parser.Reset(); Assert.False(parser.IsSet("-f"), "-f option should not be set"); Assert.False(parser.IsSet("-file"), "-file option should not be set"); }
public void HostNameRegexPassTests() { var result = GetOpt.Parse ( TestOptions.Standard, Parameters.Default, "-Hwww.google.com", "--host-name=1api.net", "--host-name", "ftp.microsoft.com" ); Assert.AreEqual(0, result.NonOptions.Count); var options = result.Options; Assert.AreEqual(3, options.Count); var hostNameOption = TestOptions.Standard.Single(o => o.ShortName == 'H'); Assert.AreEqual(hostNameOption, options[0].Definition); Assert.AreEqual("www.google.com", options[0].Argument); Assert.AreEqual(hostNameOption, options[1].Definition); Assert.AreEqual("1api.net", options[1].Argument); Assert.AreEqual(hostNameOption, options[2].Definition); Assert.AreEqual("ftp.microsoft.com", options[2].Argument); }
public bool ParseArgs(string[] args) { GetOpt programArgs = new GetOpt(args); try { programArgs.SetOpts(new string[] { "d", "x", "e", "r", "o=" }); programArgs.Parse(); } catch (ArgumentException e) { Console.WriteLine(e.Message); return(false); } _Debug = programArgs.IsDefined("d"); _ForceENaming = programArgs.IsDefined("e"); _ForceXNaming = programArgs.IsDefined("x"); _UseRelativeNaming = programArgs.IsDefined("r"); if (programArgs.IsDefined("o")) { _OutputPath = programArgs.GetOptionArg("o"); } _PathList.Clear(); foreach (string s in programArgs.Args) { _PathList.Add(s); } return(true); }
public bool ParseArgs(string[] args) { GetOpt programArgs = new GetOpt(args); try { programArgs.SetOpts(new string[] { "d" }); programArgs.Parse(); } catch (ArgumentException e) { Console.WriteLine(e.Message); return(false); } _Debug = programArgs.IsDefined("d"); _PathList.Clear(); foreach (string s in programArgs.Args) { _PathList.Add(s); } return(true); }
public void BoolTestsStandard() { var commandLine = new[] { "-e1", "-efalse", "-e", "yes", "--show-minor-errors=no", "dummy", "--show-minor-errors=true", "-e", "0", "--", "-etrue", "--show-minor-errors", }; var result = GetOpt.Parse(commandLine, TestOptions.Standard); var(nonOptions, options) = (result.NonOptions, result.Options); Assert.AreEqual(3, nonOptions.Count); Assert.AreEqual("dummy", nonOptions[0]); Assert.AreEqual("-etrue", nonOptions[1]); Assert.AreEqual("--show-minor-errors", nonOptions[2]); var definition = TestOptions.Standard.Single(o => o.ShortName == 'e'); Assert.AreEqual(6, options.Count); Assert.IsTrue(options.All(o => ReferenceEquals(definition, o.Definition))); Assert.IsTrue(options[0].Argument); Assert.IsFalse(options[1].Argument); Assert.IsTrue(options[2].Argument); Assert.IsFalse(options[3].Argument); Assert.IsTrue(options[4].Argument); Assert.IsFalse(options[5].Argument); }
public void HostNameRegexFailTests() { void CheckHostName(params string[] arguments) { Assert.ThrowsException <GetOptException>(() => GetOpt.Parse(arguments, TestOptions.Standard)); } CheckHostName("-H_sip._udp.phone-company.com"); CheckHostName("--host-name=thisHostnameIsExtraordinaryLongAndThusFailsTheRegexCheckAccordingToRfc1123"); CheckHostName("--host-name", "*.google.com"); }
public void WorkingDayInvalid() { void CheckForInvalidEnum(IEnumerable <string> arguments) { Assert.ThrowsException <GetOptException>(() => GetOpt.Parse(arguments, TestOptions.Standard)); } CheckForInvalidEnum(new[] { "-wMOnday" }); CheckForInvalidEnum(new[] { "-w", "tuesday" }); CheckForInvalidEnum(new[] { "--work-day=bullshit" }); }
public void WorkingDayOutOfRange() { void CheckForOutOfRange(IEnumerable <string> arguments) { Assert.ThrowsException <GetOptException>(() => GetOpt.Parse(arguments, TestOptions.Standard)); } CheckForOutOfRange(new[] { "-wSaturday" }); CheckForOutOfRange(new[] { "-w", "Sunday" }); CheckForOutOfRange(new[] { "--work-day=Sunday" }); }
public void BoolWrongArguments() { void CheckWrongBoolArguments(IList <string> arguments) { Assert.ThrowsException <GetOptException>(() => GetOpt.Parse(arguments, TestOptions.Standard)); } CheckWrongBoolArguments(new[] { "-eTrue" }); CheckWrongBoolArguments(new[] { "-e", "False" }); CheckWrongBoolArguments(new[] { "--show-minor-errors=dummy" }); }
public void TestCustomValidationSuccess() { GetOpt.Parse ( options, null, "-r127.0.0.1", "-r127.0.0.256", // this is a valid hostname according to RFC 1123 (pray that your gethostbyname knows that) "--remote-host=www.xn--hochsttter-v5a.de", "-r", "2001:db8::dead:beef:FACE:B00C", "--remote-host", "::FFFF:192.168.0.1" ); }
public void WorkDayArgumentMissing() { void CheckForMissingArgument(IEnumerable <string> arguments) { Assert.ThrowsException <GetOptException>(() => GetOpt.Parse(arguments, TestOptions.Standard)); } CheckForMissingArgument(new[] { "-w" }); CheckForMissingArgument(new[] { "--work-day" }); CheckForMissingArgument(new[] { "--work-day", "--", "Monday" }); CheckForMissingArgument(new[] { "-w", "--", "Tuesday" }); }
public void TestCustomValidationFail() { var getOpt = new GetOpt(options, Parameters.Default); void CheckRemoteHost(params string[] arguments) { Assert.ThrowsException <GetOptException>(() => getOpt.Parse(arguments)); } CheckRemoteHost("-r127.0.0.1/32"); CheckRemoteHost("-r", "2001:db8::dead:beef:FACE:B00K"); CheckRemoteHost("--remote-host", "_sip._udp.dus.net"); CheckRemoteHost("--remote-host=www.hochstätter.de"); }
public void TestBoolOption() { GetOpt parser = new GetOpt(); parser.AddBoolOption(flags: new string[] { "-h", "-help" }); Assert.False(parser.IsSet("-h"), "-h option should not be set"); Assert.False(parser.IsSet("-help"), "-help option should not be set"); string[] rest = parser.Parse( "-not option -help -file name bla bla -fbla".Split(" ")); Assert.True(parser.IsSet("-h"), "-h option should be set"); Assert.True(parser.IsSet("-help"), "-help option should be set"); parser.Reset(); Assert.False(parser.IsSet("-h"), "-h option should not be set"); Assert.False(parser.IsSet("-help"), "-help option should not be set"); }
public void WorkingDayStandardTests() { var commandLine = new[] { "-wFriday", "--work-day=Wednesday", "-vwMonday", "-w", "Tuesday", "Friday", "-vw", "Tuesday", "--work-day", "Thursday", "--", "-wSaturday", "--work-day=Sunday" }; var result = GetOpt.Parse(commandLine, TestOptions.Standard); Assert.AreEqual(3, result.NonOptions.Count); Assert.AreEqual("Friday", result.NonOptions[0]); Assert.AreEqual("-wSaturday", result.NonOptions[1]); Assert.AreEqual("--work-day=Sunday", result.NonOptions[2]); var options = result.Options; Assert.AreEqual(8, result.Options.Count); Assert.AreSame(TestOptions.Standard.Single(o => o.ShortName == 'w'), options[0].Definition); Assert.AreEqual(WeekDay.Friday, options[0].Argument); Assert.AreSame(TestOptions.Standard.Single(o => o.ShortName == 'w'), options[1].Definition); Assert.AreEqual(WeekDay.Wednesday, options[1].Argument); Assert.AreSame(TestOptions.Standard.Single(o => o.ShortName == 'v'), options[2].Definition); Assert.IsNull(options[2].Argument); Assert.AreSame(TestOptions.Standard.Single(o => o.ShortName == 'w'), options[3].Definition); Assert.AreEqual(WeekDay.Monday, options[3].Argument); Assert.AreSame(TestOptions.Standard.Single(o => o.ShortName == 'w'), options[4].Definition); Assert.AreEqual(WeekDay.Tuesday, options[4].Argument); Assert.AreSame(TestOptions.Standard.Single(o => o.ShortName == 'v'), options[5].Definition); Assert.IsNull(options[5].Argument); Assert.AreSame(TestOptions.Standard.Single(o => o.ShortName == 'w'), options[6].Definition); Assert.AreEqual(WeekDay.Tuesday, options[6].Argument); Assert.AreSame(TestOptions.Standard.Single(o => o.ShortName == 'w'), options[6].Definition); Assert.AreEqual(WeekDay.Thursday, options[7].Argument); }
public void TestAttributesSuccess() { var getOpt = new GetOpt(this); Assert.AreEqual(3, getOpt.OptionDefinitions.Count); getOpt.Parse ( "-vlC:\\Temp\\Logfile.txt", "-nroot@localhost", "[email protected]", "-n", "*****@*****.**" ); var help = getOpt.GetHelp(); Assert.IsTrue(verbose); Assert.AreEqual("C:\\Temp\\Logfile.txt", LogFile); Assert.AreEqual(3, EmailNotifications.Count); Assert.IsTrue(EmailNotifications.Contains("root@localhost")); Assert.IsTrue(EmailNotifications.Contains("*****@*****.**")); Assert.IsTrue(EmailNotifications.Contains("*****@*****.**")); }
public static void Main(string[] args) { GetOpt oGetOpt = new GetOpt(args); string MdbFile = ""; try { oGetOpt.SetOpts(new string[] { "c", "d=", "s=" }); oGetOpt.Parse(); //DEBUG: Console.WriteLine("Successfully parsed arguments."); } catch (ArgumentException) { Console.Error.WriteLine("ERROR: arguments not supplied"); //TODO: Write usage info function oGetOpt.Args.ToString(); Console.WriteLine(); System.Environment.Exit(666); } if (!oGetOpt.IsDefined("d")) { Console.Error.WriteLine("Must specify the database."); System.Environment.Exit(666); } else { MdbFile = oGetOpt.GetOptionArg("d"); } Console.WriteLine("Database: {0}", MdbFile); try { if (oGetOpt.HasArgument("s")) { Console.WriteLine("SQL Script: {0}", oGetOpt.GetOptionArg("s")); } } catch (ArgumentNullException) { } if (oGetOpt.IsDefined("c")) { if (File.Exists(MdbFile)) { Console.Error.WriteLine("JetSQL file \"{0}\" already exists!", MdbFile); } else { JetSqlUtil.CreateMDB(MdbFile); } } //If the Access file doesn't exist at this point we can't go on if (!File.Exists(MdbFile)) { Console.Error.WriteLine("JetSQL file \"{0}\" does not exist!", MdbFile); } if (oGetOpt.IsDefined("s")) { OdbcDba dbconn = new OdbcDba(); dbconn.ConnectMDB(MdbFile); dbconn.ExecuteSqlFile(oGetOpt.GetOptionArg("s")); dbconn.Disconnect(); } }