@@ -52,20 +52,17 @@ bool print_part(Callable&& fmt_or_adaparse_print, std::string_view get_part,
52
52
if (get_part == " protocol" ) {
53
53
fmt_or_adaparse_print (" {}\n " , url.get_protocol ());
54
54
return true ;
55
- }
56
- if (get_part == " password" ) {
55
+ } else if (get_part == " password" ) {
57
56
fmt_or_adaparse_print (" {}\n " , url.get_password ());
58
57
return true ;
59
- }
60
- if (get_part == " pathname" ) {
58
+ } else if (get_part == " pathname" ) {
61
59
fmt_or_adaparse_print (" {}\n " , url.get_pathname ());
62
60
return true ;
63
61
}
62
+ } else if (get_part == " hostname" ) {
63
+ fmt_or_adaparse_print (" {}\n " , url.get_hostname ());
64
+ return true ;
64
65
}
65
- } else if (get_part == " hostname" ) {
66
- fmt_or_adaparse_print (" {}\n " , url.get_hostname ());
67
- return true ;
68
-
69
66
} else if (get_part == " username" ) {
70
67
fmt_or_adaparse_print (" {}\n " , url.get_username ());
71
68
return true ;
@@ -88,12 +85,12 @@ int piped_file(Callable&& adaparse_print, const cxxopts::ParseResult result,
88
85
89
86
uint64_t before = nano ();
90
87
91
- size_t total_bytes_read = 0 ;
92
- size_t bytes_read_this_loop_iteration;
93
- size_t offset = 0 ;
94
- size_t lines = 0 ;
95
- size_t blocks = 0 ;
96
- std::string get_part;
88
+ size_t total_bytes_read{ 0 } ;
89
+ size_t bytes_read_this_loop_iteration{ 0 } ;
90
+ size_t offset{ 0 } ;
91
+ size_t lines{ 0 } ;
92
+ size_t blocks{ 0 } ;
93
+ std::string get_part{} ;
97
94
if (result.count (" get" )) {
98
95
get_part = result[" get" ].as <std::string>();
99
96
}
@@ -121,7 +118,7 @@ int piped_file(Callable&& adaparse_print, const cxxopts::ParseResult result,
121
118
while (li.find_another_complete_line ()) {
122
119
std::string_view line = li.grab_line ();
123
120
124
- ada::result<ada::url_aggregator> url = ada::parse (line);
121
+ auto url = ada::parse<ada::url_aggregator> (line);
125
122
if (!url) {
126
123
adaparse_print (" Invalid URL: {}\n " , line);
127
124
} else if (!get_part.empty ()) {
@@ -138,7 +135,7 @@ int piped_file(Callable&& adaparse_print, const cxxopts::ParseResult result,
138
135
// have a line of length offset at cachebuffer.get()
139
136
std::string_view line (cachebuffer.get (), offset);
140
137
141
- ada::result<ada::url_aggregator> url = ada::parse (line);
138
+ auto url = ada::parse<ada::url_aggregator> (line);
142
139
if (!url) {
143
140
adaparse_print (" Invalid URL:{}\n " , line);
144
141
} else if (!get_part.empty ()) {
@@ -207,30 +204,22 @@ int main(int argc, char** argv) {
207
204
cxxopts::Options options (" adaparse" ,
208
205
" Command-line version of the Ada URL parser" );
209
206
210
- options.add_options ()(" d,diagram" , " Print a diagram of the result" ,
211
- cxxopts::value<bool >()->default_value (" false" ))(
212
- " u,url" , " URL Parameter (required)" , cxxopts::value<std::string>())(
213
- " h,help" , " Print usage" )(
214
- " g,get" , " Get a specific part of the URL (e.g., 'origin', 'host', etc.)" ,
215
- cxxopts::value<std::string>())(
216
- " b,benchmark" , " Display chronometer for piped_file function" ,
217
- cxxopts::value<bool >()->default_value (" false" ))(
218
- " p,path" , " Takes in a path to a file and process all the URL within" ,
219
- cxxopts::value<std::string>())(
220
- " o,output" , " Takes in a path and outputs to a text file." ,
221
- cxxopts::value<std::string>()->default_value (" __no_output__" ))
222
-
223
- ;
207
+ // clang-format off
208
+ options.add_options ()
209
+ (" d,diagram" , " Print a diagram of the result" , cxxopts::value<bool >()->default_value (" false" ))
210
+ (" u,url" , " URL" , cxxopts::value<std::string>())
211
+ (" g,get" , " Get a specific part of the URL (e.g., 'origin', 'host', etc.)" , cxxopts::value<std::string>())
212
+ (" b,benchmark" , " Display chronometer for piped_file function" , cxxopts::value<bool >()->default_value (" false" ))
213
+ (" p,path" , " Takes in a path to a file and process all the URL within" , cxxopts::value<std::string>())
214
+ (" o,output" , " Takes in a path and outputs to a text file." , cxxopts::value<std::string>()->default_value (" /dev/null" ))
215
+ (" h,help" , " Print usage" );
216
+ // clang-format on
217
+
224
218
options.parse_positional ({" url" });
225
219
226
220
auto result = options.parse (argc, argv);
227
221
228
222
std::string output_filename = result[" output" ].as <std::string>();
229
-
230
- result[" output" ].as <std::string>() == " __no_output__"
231
- ? output_filename = " /dev/null"
232
- : output_filename = result[" output" ].as <std::string>();
233
-
234
223
auto out = fmt::output_file (output_filename);
235
224
bool has_result = result.count (" output" );
236
225
@@ -255,15 +244,14 @@ int main(int argc, char** argv) {
255
244
}
256
245
257
246
if (result.count (" path" )) {
258
- std::string filepath = result[" path" ].as <std::string>();
259
- FILE* file = fopen (filepath .c_str (), " r" );
247
+ auto file_path = result[" path" ].as <std::string>();
248
+ auto file = fopen (file_path .c_str (), " r" );
260
249
if (file) {
261
250
piped_file (adaparse_print, result, file);
262
251
fclose (file);
263
252
return EXIT_SUCCESS;
264
253
} else {
265
- int err = errno;
266
- fmt::print (stderr, " Error opening file: {}\n " , strerror (err));
254
+ fmt::print (stderr, " Error opening file: {}\n " , strerror (errno));
267
255
return EXIT_FAILURE;
268
256
}
269
257
}
@@ -274,14 +262,15 @@ int main(int argc, char** argv) {
274
262
return EXIT_SUCCESS;
275
263
}
276
264
277
- std::string url_string = result[" url" ].as <std::string>();
265
+ auto input_url = result[" url" ].as <std::string>();
278
266
bool to_diagram = result[" diagram" ].as <bool >();
279
267
280
- ada::result<ada::url_aggregator> url = ada::parse (url_string );
268
+ auto url = ada::parse<ada::url_aggregator>(input_url );
281
269
if (!url) {
282
- fmt::print (stderr, " Invalid URL: {}\n " , url_string );
270
+ fmt::print (stderr, " Invalid URL: {}\n " , input_url );
283
271
return EXIT_FAILURE;
284
272
}
273
+
285
274
if (result.count (" get" )) {
286
275
std::string get_part = result[" get" ].as <std::string>();
287
276
@@ -292,6 +281,7 @@ int main(int argc, char** argv) {
292
281
return print_part (print_lambda, get_part, url.value ()) ? EXIT_SUCCESS
293
282
: EXIT_FAILURE;
294
283
};
284
+
295
285
if (to_diagram) {
296
286
fmt::print (" {}\n " , url->to_diagram ());
297
287
} else {
0 commit comments