public static InputOptions Create(IEnumerable<string> args)
{
string app = null, config = null, thresh = null, pid = null;
bool verbose = false, help = false;
OptionSet options = new OptionSet()
{
{ "app=", "(required) the path to the directory of the application to run jobs for.", v => app = v },
{ "config=", "the path to the configuration file to use, if not the default for the application.", v => config = v },
{ "v|verbose", "write logging information to standard out.", v => verbose = v != null },
{ "thresh=", "the threshold, in milliseconds, to compress file system events into.", v => thresh = v },
{ "h|?|help", "display usage help.", v => help = v != null }
};
InputOptions result = new InputOptions();
result.OptionSet = options;
try
{
options.Parse(args);
result.ApplicationPath = PathQuotesExp.Replace(app ?? string.Empty, "$1");
result.ConfigPath = PathQuotesExp.Replace(config ?? string.Empty, "$1");
result.IsValid = true;
}
catch (OptionException ex)
{
result.IsValid = false;
result.ParseException = ex;
}
if (result.IsValid)
{
result.Help = help;
result.Verbose = verbose;
if (!result.Help)
{
if (string.IsNullOrEmpty(result.ApplicationPath))
{
result.ParseErrorMessage = "Application path is required.";
result.IsValid = false;
}
pid = Environment.GetEnvironmentVariable("COLLARSERVICEPID");
if (!string.IsNullOrEmpty(pid))
{
try
{
result.ParentProcessId = Convert.ToInt32(pid, CultureInfo.InvariantCulture);
}
catch (FormatException)
{
}
catch (OverflowException)
{
}
}
if (!string.IsNullOrEmpty(thresh))
{
const string ThresholdErrorMessage = "Threshold must be an integer value greater than or equal to 500.";
try
{
result.Threshold = Convert.ToInt32(thresh, CultureInfo.InvariantCulture);
if (result.Threshold < 500)
{
result.IsValid = false;
result.ParseErrorMessage = ThresholdErrorMessage;
}
}
catch (FormatException)
{
result.IsValid = false;
result.ParseErrorMessage = ThresholdErrorMessage;
}
catch (OverflowException)
{
result.IsValid = false;
result.ParseErrorMessage = ThresholdErrorMessage;
}
}
}
}
return result;
}