-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.cs
153 lines (129 loc) · 5.16 KB
/
parse.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using CommandLine;
using CommandLine.Text;
namespace Weather
{
internal class CommandParser
{
// CommandLine (CLP) input args parser
public class Options
{
private readonly string location;
private readonly char type;
private readonly char units;
public Options(string location, char type, char units)
{
this.location = location;
this.type = type;
this.units = units;
}
[Option('l', Required = true, HelpText = "Location of the forceast")]
public string Location { get { return location; } }
[Option('t', Required = false, HelpText = "Type of the forecast: c - current, d - daily, w - weekly, m - monthly", Default = (char)'c')]
public char Type { get { return type; } }
[Option('u', Required = false, HelpText = "Units of the forecast: m - metric, i - imperial", Default = (char)'m')]
public char Units { get { return units; } }
}
static void Main(string[] args)
{
// disable CLP default help screen
var parser = new CommandLine.Parser(with => with.HelpWriter = null);
var parserResult = parser.ParseArguments<Options>(args);
parserResult
.WithParsed<Options>(options => Run(options))
.WithNotParsed(errs => DisplayHelp(parserResult, errs));
}
static void DisplayHelp<T>(ParserResult<T> result, IEnumerable<Error> errs)
{
var helpText = HelpText.AutoBuild(result, h =>
{
h.AdditionalNewLineAfterOption = false; //remove the extra newline between options
h.Heading = "cs_CLI_weather"; //change header
h.Copyright = "\"CoPyRiGhT\" (c) Patryk Kościk 2022"; //change copyrigt text
return HelpText.DefaultParsingErrorsHandler(result, h);
}, e => e);
Console.WriteLine(helpText);
}
private static void Run(Options options)
{
var places = Geolocation.Geolocate(options.Location, 10);
var selectedPlace = new Geolocation.Place();
// check if there were any results (non empty list)
// TODO: make this better
if (places.Count == 0)
{
Console.WriteLine("NO PLACES WERE FOUND");
}
// only one location matching --location parameter
if (places.Count == 1)
{
selectedPlace = places[0];
}
// if there is more than one place
// display selection screen
if (places.Count > 1)
{
Console.WriteLine("Select a location:");
int index = 1;
foreach (var item in places)
{
Console.Write($"{index} City: {item.name}, State: {item.state}, Country: {item.country}\n");
index += 1;
}
// get user input and check bounds
string input;
int selection;
bool loop = true;
do
{
input = Console.ReadLine();
if (!Int32.TryParse(input, out selection))
{
Console.WriteLine("not a number!!!");
}
else if (selection > places.Count || selection <= 0)
{
Console.WriteLine("Selection out of bounds");
}
else
{
selectedPlace = places[selection - 1];
loop = false;
}
} while (loop);
// i'd love for forecast to be static
// but it it really did not make much sense
// yeah im not following func.prog. princibles
// : (
var forecast = new Weather(selectedPlace.lat, selectedPlace.lon);
switch (options.Type)
{
case 'c':
{
Cli.printCurrent(selectedPlace, forecast);
break;
}
case 'd':
{
Cli.printDaily(selectedPlace, forecast);
break;
}
case 'w':
{
Cli.printWeekly(selectedPlace, forecast);
break;
}
case 'm':
{
Cli.printMonthly(selectedPlace, forecast);
break;
}
default:
{
Console.WriteLine("not supposed to be there lmao");
break;
}
}
}
}
}
}