Skip to content

Instantly share code, notes, and snippets.

@bobbychopra
Created June 1, 2012 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobbychopra/2852255 to your computer and use it in GitHub Desktop.
Save bobbychopra/2852255 to your computer and use it in GitHub Desktop.
Use Plossum CommandLine to parse and verify DateTime
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");
}
}
}
}
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