I have a console application, which uses Ndesk.Options to parse console arguments, request weather data and print them. I am looking primarily for code style suggestions.
This is the code, which parses the console arguments:
var argSet = new OptionSet() {
{ "l|location=", "Can be a city or a zip-code. Add a countrycode or a countryname to specify location. Use ISO 3166 codes. Example: Berlin,Germany", (loc) =>
{
var locAndCode = loc.Split(',');
if(locAndCode[0].All(ch => char.IsDigit(ch)))
{
zipCode = int.Parse(locAndCode[0]);
requestMethod = RequestMethod.ZipCode;
}
else
{
city = locAndCode[0];
requestMethod = RequestMethod.City;
}
if(locAndCode.Length > 1)
{
if(CountryCodeHelper.IsValidCode(countryCode)) countryCode = locAndCode[1];
else countryCode = CountryCodeHelper.ConvertToCode(locAndCode[1]);
}
}
},
{"d|days=", "Days of weather to receive. 0 for current weather, 4 days forecast is avaible.", (int v) =>
{
days = v <= 4 ? v : 4;
}
},
{ "h|help", (help) => showHelp = help != null },
{
"u|units=", "Determites the units. Avaible values are Default, Metric and Imperial. For more information see the openweathermap documentation" ,(string v) =>
{
Enum.TryParse(v, out units);
}
}
};
try
{
argSet.Parse(args);
if(requestMethod == RequestMethod.None && !showHelp) throw new OptionException("No location was given!", "location");
}
catch(OptionException opEx)
{
PrintError(opEx.Message);
Console.WriteLine("Try --help for more information.");
return;
}
if(showHelp)
{
ShowHelp(argSet);
return;
}
This methods are used to print help and errors:
static void ShowHelp(OptionSet argSet)
{
Console.WriteLine("Usage: openWeather [OPTIONS]");
Console.WriteLine("Prints the weather for the given location.");
argSet.WriteOptionDescriptions(Console.Out);
}
static void PrintError(string message)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[ERR]");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(message);
}
After parsing and request of the weather data it's printed:
//This is placed in main//
PrintLocation(currentWeather);
ChangeColor(ConsoleColor.Magenta);
Console.WriteLine("== Current weather ==");
PrintWeather(currentWeather);
if(days > 0)
{
DateTime printDate = DateTime.Now.Date;
for(int i = 0; i < days;i++)
{
printDate = printDate.AddDays(1);
ChangeColor(ConsoleColor.Magenta);
Console.WriteLine("== Day {0}/12:00am ==",printDate.ToShortDateString());
PrintWeather(forecast.TimePoints.Single((weatherTimePoint) => weatherTimePoint.TimePoint.Date == printDate && weatherTimePoint.TimePoint.Hour == 12));
}
}
static void PrintWeather(IWeatherSet value)
{
ChangeColor(ConsoleColor.DarkGreen);
Console.Write(value.Weather[0].Condition);
ChangeColor(ConsoleColor.White);
Console.WriteLine("({0})", value.Weather[0].Description);
Console.Write("Temperature:");
ChangeColor(ConsoleColor.Red);
Console.WriteLine("{0}{1}", value.ClimaticData.Temperature, UnitsHelper.GetUnit(value.Units, WeatherField.Temperature));
ChangeColor(ConsoleColor.White);
Console.WriteLine("Humidity:{0}%",value.ClimaticData.Humidity );
Console.WriteLine("Pressure:{0}hPa",value.ClimaticData.Pressure);
Console.WriteLine("Wind speed:{0} {1}",value.Wind.Speed,UnitsHelper.GetUnit(value.Units,WeatherField.WindSpeed));
Console.WriteLine("Wind degree:{0}°",value.Wind.Direction);
}
static void PrintLocation(ILocation value)
{
ChangeColor(ConsoleColor.White);
Console.Write("Weather for: ");
ChangeColor(ConsoleColor.DarkCyan);
Console.Write(value.City);
ChangeColor(ConsoleColor.White);
Console.WriteLine(", {0}:", value.Country);
}
The complete code can be looked up at the GitHub page.