Skip to content

Commit 36b94b6

Browse files
authored
Improving adaparse (#320)
1 parent 6443b92 commit 36b94b6

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

tools/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
add_executable(adaparse adaparse.cpp)
22
target_link_libraries(adaparse PRIVATE ada)
33
target_include_directories(adaparse PUBLIC "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>")
4+
5+
import_dependency(cxxopts jarro2783/cxxopts eb78730)
6+
add_dependency(cxxopts)
7+
target_link_libraries(adaparse PRIVATE cxxopts::cxxopts)

tools/adaparse.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <cstdlib>
22
#include <iostream>
33
#include <string_view>
4-
4+
#include <cxxopts.hpp>
55
#include "ada.h"
66
/**
77
* @private
@@ -39,18 +39,23 @@
3939
* `-------------------------------- protocol_end 5
4040
**/
4141
int main(int argc, char** argv) {
42-
if (argc < 2) {
43-
std::cout << "use a URL as a parameter." << std::endl;
42+
cxxopts::Options options("adaparse",
43+
"Command-line version of the Ada URL parser");
44+
45+
options.add_options()("d,diagram", "Print a diagram of the result",
46+
cxxopts::value<bool>()->default_value("false"))(
47+
"u,url", "URL Parameter (required)", cxxopts::value<std::string>())(
48+
"h,help", "Print usage");
49+
options.parse_positional({"url"});
50+
51+
auto result = options.parse(argc, argv);
52+
// the first argument without an option name will be parsed into file
53+
if (result.count("help") || !result.count("url")) {
54+
std::cout << options.help() << std::endl;
4455
return EXIT_SUCCESS;
4556
}
46-
std::string url_string = argv[1];
47-
bool to_diagram = false;
48-
if (argc > 2) {
49-
if (std::string_view(argv[1]) == "-d") {
50-
url_string = argv[2];
51-
to_diagram = true;
52-
}
53-
}
57+
std::string url_string = result["url"].as<std::string>();
58+
bool to_diagram = result["diagram"].as<bool>();
5459
ada::result<ada::url_aggregator> url = ada::parse(url_string);
5560
if (!url) {
5661
std::cerr << "Invalid." << std::endl;

0 commit comments

Comments
 (0)