William A. Kennington III | 03e6def | 2021-05-11 15:23:15 -0700 | [diff] [blame^] | 1 | #include "args.hpp" |
| 2 | |
| 3 | #include <fmt/format.h> |
| 4 | #include <getopt.h> |
| 5 | |
| 6 | #include <stdexcept> |
| 7 | |
| 8 | namespace kcsbridge |
| 9 | { |
| 10 | |
| 11 | Args::Args(int argc, char* argv[]) |
| 12 | { |
| 13 | static const char opts[] = ":c:"; |
| 14 | static const struct option longopts[] = { |
| 15 | {"channel", required_argument, nullptr, 'c'}, |
| 16 | {nullptr, 0, nullptr, 0}, |
| 17 | }; |
| 18 | int c; |
| 19 | optind = 0; |
| 20 | while ((c = getopt_long(argc, argv, opts, longopts, nullptr)) > 0) |
| 21 | { |
| 22 | switch (c) |
| 23 | { |
| 24 | case 'c': |
| 25 | channel = optarg; |
| 26 | break; |
| 27 | case ':': |
| 28 | throw std::runtime_error( |
| 29 | fmt::format("Missing argument for `{}`", argv[optind - 1])); |
| 30 | break; |
| 31 | default: |
| 32 | throw std::runtime_error(fmt::format( |
| 33 | "Invalid command line argument `{}`", argv[optind - 1])); |
| 34 | } |
| 35 | } |
| 36 | if (optind != argc) |
| 37 | { |
| 38 | throw std::invalid_argument("Requires no additional arguments"); |
| 39 | } |
| 40 | if (channel == nullptr) |
| 41 | { |
| 42 | throw std::invalid_argument("Missing KCS channel"); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | } // namespace kcsbridge |