-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pas
268 lines (247 loc) · 8.34 KB
/
main.pas
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
program main;
{$APPTYPE CONSOLE}
{$mode objfpc}{$H+}
uses SysUtils, Classes,
{$IFDEF UNIX}
{$IFDEF UseCThreads}
cthreads,
{$ENDIF}
unix,
{$ENDIF}
{$IFDEF MSWINDOWS}
Process,
{$ENDIF}
CustApp, AppConfig, WeatherEngine;
type
MyApp = class(TCustomApplication)
private
Locale : Locale;
Settings : AppSettings;
Display : DisplayOptions;
ToJSON : Integer;
FeedLine : Boolean;
URL : String;
protected
procedure DoRun; override;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
procedure WriteHelp; virtual;
end;
procedure MyApp.DoRun;
begin
// flags for non-weather things
if HasOption('h', 'help') then begin
WriteHelp();
Halt();
end;
if HasOption('c', 'config') then begin
SetConfig(Settings);
Halt();
end;
// check if config exists
if not (ConfigExists()) then
begin
try
writeln('Note: No config file found. I''m creating new one.');
SetConfig(Settings);
except
on E: Exception do
begin
writeln(E.toString());
end;
end;
end;
// get config
//ToJSON := HasOption('j', 'json');
if HasOption('J', 'JSON')
then ToJSON := 3
else if HasOption('j', 'json')
then ToJSON := 2
else if HasOption('raw-json')
then ToJSON := 1
else ToJSON := 0;
FeedLine := not HasOption('n', 'no-feed-line');
try
GetConfig(Settings);
except
on E: Exception do
begin
writeln(E.toString());
Terminate;
end;
end;
// change config to suit flags
if HasOption('u', 'units') then begin
try
if StrToInt(getOptionValue('u', 'units')) in [0, 1, 2, 3] then
begin
Settings.Units := StrToInt(getOptionValue('u', 'units'));
end else begin
writeln('Note: wrong unit flag value. I''m not changing the units then.');
end;
except
on E: Exception do
begin
writeln('Note: wrong unit flag value. I''m not changing the units then.');
writeln(E.toString());
end;
end;
end;
if HasOption('l', 'location') then begin
try
if getOptionValue('l', 'location') <> '' then
begin
Settings.City := getOptionValue('l', 'location');
end else begin
writeln('Note: empty city name. I''m not changing the city then.');
end;
except
on E: Exception do
begin
writeln('Note: wrong city name. I''m not changing the city then.');
writeln(E.toString());
end;
end;
end;
if HasOption('o', 'output') then begin
try
Display := adjustDisplay(getOptionValue('o', 'output'));
except
on E: Exception do
begin
writeln('Note: wrong token name. I''m not changing the token then.');
writeln(E.toString());
end;
end;
end else begin
Display := adjustDisplay(Settings.Output);
end;
if HasOption('s', 'style') then begin
try
if StrToInt(getOptionValue('s', 'style')) in [0, 1, 2, 3] then
begin
setSeparators(Display, StrToInt(getOptionValue('s', 'style')));
end else begin
writeln('Note: wrong style flag value. I''m not changing the styles then.');
end;
except
on E: Exception do
begin
writeln('Note: wrong style flag value. I''m not changing the styles then.');
writeln(E.toString());
end;
end;
end else begin
try
setSeparators(Display, Settings.Style);
except
on E: Exception do
begin
writeln('Note: wrong style flag value. I''m not changing the styles then.');
writeln(E.toString());
end;
end;
end;
if HasOption('T', 'token') then begin
try
if getOptionValue('T', 'token') <> '' then
begin
Settings.Token := getOptionValue('T', 'token');
end else begin
writeln('Note: empty token name. Using your token from the config file.');
end;
except
on E: Exception do
begin
writeln('Note: wrong token name. I''m not changing the token then.');
writeln(E.toString());
end;
end;
end;
// print weather info
Locale := GetLocale(Settings.Units);
if (toJSON = 1) then Locale.Param := 'standard';
if checkInternet() then
begin
try
URL := 'http://api.openweathermap.org/data/2.5/weather?q='+Settings.City+'&appid='+Settings.Token+'&units='+Locale.Param+'';
if (toJSON >= 3) then
begin
write(printFormattedJSON(URL));
if FeedLine then writeln();
end else if (toJSON = 2) then
begin
write(printJSON(URL));
if FeedLine then writeln();
end else if (toJSON = 1) then
begin
write(printRaw(URL));
if FeedLine then writeln();
end else begin
write(printInfo(URL, Locale, Display));
if FeedLine then writeln();
end;
except
on E: Exception do
begin
writeln(E.toString());
end;
end;
end else begin
writeln('Cannot get weather data - no Internet connection!');
end;
// terminate
Terminate;
end;
constructor MyApp.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
//StopOnException:=True;
end;
destructor MyApp.Destroy;
begin
inherited Destroy;
end;
procedure MyApp.WriteHelp;
begin
writeln('Usage: '+Title+' [flags]');
writeln('Available flags: ');
writeln(' (no flags) : Show weather using your default configuration');
writeln(' (if it does not exist, then create one)');
writeln(' -c , --config : Set up the config file');
writeln(' -h , --help : Display help');
writeln(' -j , --json : Print a flat JSON');
writeln(' -J , --JSON : Print a human-readable JSON');
writeln(' -l S, --location=S : Show weather in a specified city');
writeln(' -n , --no-feed-line : Do not feed line after program execution');
writeln(' --raw-json : Get a raw JSON from OpenWeatherMap (in standard units)');
writeln(' -o S, --output=S : Determine which data entries may be displayed using S');
writeln(' (If S="full", then display all data)');
writeln(' -s 0, --style=0 : Flat output');
writeln(' 1, =1 : Flat output with location in separate line (default)');
writeln(' 2, =2 : Flat output with location in separate line + labels');
writeln(' 3, =3 : Output as a list of values with flat subvalues');
writeln(' 4, =4 : Output as a list of values with a list of subvalues');
writeln(' -T S, --token=S : Show weather using a specified OpenWeatherMap API token');
writeln(' -u 0, --units=0 : Use international SI units: Kelvin, m/s, hPa, km');
writeln(' 1, =1 : Use metric units: Celsius, km/h, hPa, km');
writeln(' 2, =2 : Use US Imperial units: Fahrenheit, mph, psi, mi');
writeln(' 3, =3 : Use UK Imperial units: Celsius, mph, mb, mi');
//writeln(' 4, =4 : Use EU Aviation units: Celsius, kts, hPa, nm');
//writeln(' 5, =5 : Use US Aviation units: Fahrenheit, kts, psi, nm');
writeln();
writeln('See more at https://github.com/RooiGevaar19/fpWeather');
end;
var App : MyApp;
//{$R *.res}
begin
App := MyApp.Create(nil);
App.Title := 'fpweather';
App.Run;
App.Free;
//{$IFDEF MSWINDOWS}
//Sleep(500);
//{$ENDIF}
//readln();
end.