blob: b81de5a171f3bfa9d74ebedecc4b94e95a04e0e2 [file] [log] [blame]
William A. Kennington III03e6def2021-05-11 15:23:15 -07001#include "args.hpp"
2
3#include <fmt/format.h>
4#include <getopt.h>
5
6#include <stdexcept>
7
8namespace kcsbridge
9{
10
11Args::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