static void Main(string[] args) { /* Add a custom logger. * BaCon.Loggers.Add(new MyFancyLogger()); */ /* Recommended approach for building console applications. */ // command-line: /Name "Billy Bob" /Age 23 /Occupation "Chicken Catcher" /HasHair /Children Billy Bob Sue /Status ItsComplicated /W BaCon.Start <SampleConsoleApp>(); /* A simpler example for building console applications. * // command-line: /Name "Billy Bob" /BirthDate 7/19/1999 /W * BaCon.Start<BasicConsoleApp>(); */ /* Just want a good command-line parser to populate your POCO? Here you go. * // command-line: /Name "Billy Bob" /Age 23 /Occupation "Chicken Catcher" /HasHair /Children Billy Bob Sue /Status ItsComplicated /W * // ParseArgs actually returns validation information and other information as well, if you want it. * var results = BaCon.ParseArgs<BasicArgs>(); * var myargs = results.Args; * // BaCon.WriteHelp(results); // Displays help if you want to. * Console.WriteLine(myargs.Name); * Console.ReadKey(true); */ }
public override int Start() { BaCon.WriteLine($"Name={Name}", ConsoleColor.White); BaCon.WriteLine($"Age={Age}"); BaCon.WriteLine($"Job={Occupation}"); BaCon.WriteLine($"HasHair={HasHair}"); BaCon.WriteLine($"Children={string.Join(", ", Children)}"); BaCon.WriteLine($"Marital Status={Status}"); return(0); // 0 for success. }
/// <summary> /// Write the log message to the console. /// </summary> /// <param name="level"></param> /// <param name="msg"></param> /// <param name="ex"></param> public override void WriteLogMessage(BaConLogLevel level, string msg, Exception ex) { var log = FormatMessage(level, msg, ex); if (!log.HasValue()) { return; } using (GetLevelColor(level)) { BaCon.WriteLine(log, indentN: "\t"); } }
public override int Start() { var today = DateTime.Today; if (BirthDate.Month == today.Month && BirthDate.Day == today.Day) { BaCon.WriteLine("Happy birthday {0}!!!".Fmt(Name), ConsoleColor.Red); } else { var next = BirthDate.AddYears(today.Year - BirthDate.Year); if (next < today) { next = next.AddYears(1); } int numDays = (next - today).Days; BaCon.WriteLine("Only {0} days until your birthday!".Fmt(numDays)); } return(0); // 0 for success. }
public void ParseQueryStringTest() { var options = new CmdLineOptions(); Assert.That.Throws <ArgumentNullException>(() => { BaCon.QueryStringToArgs(null, null); }, (ex) => ex.ParamName == "options"); // No arguments. var args = BaCon.QueryStringToArgs(null, options); Assert.AreEqual(0, args.Length); // No arguments. args = BaCon.QueryStringToArgs("", options); Assert.AreEqual(0, args.Length); // No arguments. args = BaCon.QueryStringToArgs("&&&", options); Assert.AreEqual(0, args.Length); // Single argument. var qs = "hello=world"; args = BaCon.QueryStringToArgs(qs, options); Assert.AreEqual(2, args.Length); Assert.AreEqual("/hello", args[0]); Assert.AreEqual("world", args[1]); // Single argument. qs = "&&&hello=world&&&"; args = BaCon.QueryStringToArgs(qs, options); Assert.AreEqual(2, args.Length); Assert.AreEqual("/hello", args[0]); Assert.AreEqual("world", args[1]); // Multiple arguments. qs = "hello=world&adios=mundo"; args = BaCon.QueryStringToArgs(qs, options); Assert.AreEqual(4, args.Length); Assert.AreEqual("/hello", args[0]); Assert.AreEqual("world", args[1]); Assert.AreEqual("/adios", args[2]); Assert.AreEqual("mundo", args[3]); // Arguments and flag. qs = "hello=world&goodbye"; args = BaCon.QueryStringToArgs(qs, options); Assert.AreEqual(3, args.Length); Assert.AreEqual("/hello", args[0]); Assert.AreEqual("world", args[1]); Assert.AreEqual("/goodbye", args[2]); // Arguments and negative flag. qs = "hello=world&goodbye-"; args = BaCon.QueryStringToArgs(qs, options); Assert.AreEqual(3, args.Length); Assert.AreEqual("/hello", args[0]); Assert.AreEqual("world", args[1]); Assert.AreEqual("/goodbye-", args[2]); // Full URL. qs = "http://www.theworld.com/?hello=world&adios=mundo"; args = BaCon.QueryStringToArgs(qs, options); Assert.AreEqual(4, args.Length); Assert.AreEqual("/hello", args[0]); Assert.AreEqual("world", args[1]); Assert.AreEqual("/adios", args[2]); Assert.AreEqual("mundo", args[3]); // Shouldn't include fragment. qs = "http://www.theworld.com/?hello=world&adios=mundo#something"; args = BaCon.QueryStringToArgs(qs, options); Assert.AreEqual(4, args.Length); Assert.AreEqual("/hello", args[0]); Assert.AreEqual("world", args[1]); Assert.AreEqual("/adios", args[2]); Assert.AreEqual("mundo", args[3]); // Encoded value. qs = "hello=the+world"; args = BaCon.QueryStringToArgs(qs, options); Assert.AreEqual(2, args.Length); Assert.AreEqual("/hello", args[0]); Assert.AreEqual("the world", args[1]); // Encoded value. qs = "hello=the%20world"; args = BaCon.QueryStringToArgs(qs, options); Assert.AreEqual(2, args.Length); Assert.AreEqual("/hello", args[0]); Assert.AreEqual("the world", args[1]); // Encoded value. qs = "hello=%22Aardvarks+lurk%2C+OK%3F%22"; args = BaCon.QueryStringToArgs(qs, options); Assert.AreEqual(2, args.Length); Assert.AreEqual("/hello", args[0]); Assert.AreEqual("\"Aardvarks lurk, OK?\"", args[1]); qs = "Name=Bob&Age=35"; var results = BaCon.ParseArgs <TestCmdLineObj>(qs); Assert.AreEqual("Bob", results.Args.Name); Assert.AreEqual(35, results.Args.Age); }
/// <summary> /// Writes the console application help to the console. /// </summary> /// <param name="results"></param> public virtual void WriteHelp(CmdLineParseResults results) { if (results.Errors.Length > 0) { if (BaCon.ErrorMessageTitle.HasValue()) { using (var clr = new BaConColor(BaCon.Theme.ErrorTitleText, BaCon.Theme.ErrorTitleBackground)) BaCon.WriteLine(BaCon.ErrorMessageTitle); } foreach (var err in results.Errors) { BaCon.WriteLine(err, BaCon.Theme.ErrorColor, " > ", "\t"); } BaCon.WriteLine(); } var usage = GetUsage(); if (usage.HasValue()) { BaCon.WriteLine(); BaCon.WriteLine("[USAGE]"); BaCon.WriteLine(); BaCon.WriteLine(usage, BaCon.Theme.UsageColor, "", "\t"); BaCon.WriteLine(); } var showSectionTitle = true; foreach (var prop in results.Properties.Where(p => p.ShowInHelp)) { var propHelp = GetPropertyHelp(prop); if (propHelp.IsEmpty()) { continue; } if (showSectionTitle) { // We only want to display the section title if we // are displaying help text for properties. BaCon.WriteLine(); BaCon.WriteLine("[LIST OF VALID ARGUMENTS]"); showSectionTitle = false; } BaCon.WriteLine(); // Place a line break between properties. var clr = prop.Required ? BaCon.Theme.RequiredArgColor : BaCon.Theme.StandardArgColor; BaCon.WriteLine(propHelp, clr, "", "\t"); BaCon.WriteLine(); } var exitCodes = GetExitCodesDisplay(); if (exitCodes.HasValue()) { BaCon.WriteLine(); BaCon.WriteLine("[EXIT CODES]"); BaCon.WriteLine(); BaCon.WriteLine(exitCodes); BaCon.WriteLine(); } }