|
1 | 1 | #include <cstdlib> |
2 | 2 | #include <iostream> |
3 | 3 | #include <string_view> |
4 | | - |
| 4 | +#include <cxxopts.hpp> |
5 | 5 | #include "ada.h" |
6 | 6 | /** |
7 | 7 | * @private |
|
39 | 39 | * `-------------------------------- protocol_end 5 |
40 | 40 | **/ |
41 | 41 | 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; |
44 | 55 | return EXIT_SUCCESS; |
45 | 56 | } |
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>(); |
54 | 59 | ada::result<ada::url_aggregator> url = ada::parse(url_string); |
55 | 60 | if (!url) { |
56 | 61 | std::cerr << "Invalid." << std::endl; |
|
0 commit comments