Created
June 1, 2012 13:43
-
-
Save bobbychopra/2852255 to your computer and use it in GitHub Desktop.
Use Plossum CommandLine to parse and verify DateTime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Plossum.CommandLine; | |
namespace ConsoleApp | |
{ | |
[CommandLineManager(ApplicationName = "ProgramName", Copyright = "Copyright (c) Bobby Chopra")] | |
internal class CommandLineOptions | |
{ | |
private string _date; | |
[CommandLineOption(Description = "Displays this help text")] | |
public bool Help = false; | |
[CommandLineOption(Description = "Specifies the Date for the Child Pages", MinOccurs = 1)] | |
public string Date | |
{ | |
get { return _date; } | |
set | |
{ | |
DateTime dateAsArg; | |
if (!DateTime.TryParse(value, out dateAsArg)) | |
throw new InvalidOptionValueException("The argument is not a date", false); | |
if (dateAsArg < DateTime.Today.AddMonths(-6) || dateAsArg > DateTime.Today.AddMonths(1)) | |
throw new InvalidOptionValueException( "The date is too far away", false); | |
_date = dateAsArg.ToString("MM/dd/yyyy"); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Plossum.CommandLine; | |
namespace ConsoleApp | |
{ | |
internal class Program | |
{ | |
private static int Main(string[] args) | |
{ | |
var options = new CommandLineOptions(); | |
var parser = new CommandLineParser(options); | |
parser.Parse(); | |
Console.WriteLine(parser.UsageInfo.GetHeaderAsString(78)); // 78 is the screen width | |
if (options.Help) | |
{ | |
Console.WriteLine(parser.UsageInfo.GetOptionsAsString(78)); | |
return 0; | |
} | |
else if (parser.HasErrors) | |
{ | |
Console.WriteLine(parser.UsageInfo.GetErrorsAsString(78)); | |
return -1; | |
} | |
//get date | |
var date = options.Date; | |
// ... continue | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment