blob: 80bddef9cd10df8690f4a1c9cee95dee06306f43 [file] [log] [blame]
Matt Spinlerdd945862018-09-07 12:41:05 -05001/**
2 * Copyright © 2018 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "argument.hpp"
18
19#include <algorithm>
20#include <cassert>
21#include <iostream>
22#include <iterator>
23
24const std::string ArgumentParser::true_string = "true";
25const std::string ArgumentParser::empty_string = "";
26
27const char* ArgumentParser::optionstr = "s:b:i:?h";
28const option ArgumentParser::options[] = {
29 {"service-namespaces", required_argument, nullptr, 's'},
30 {"service-blacklists", required_argument, nullptr, 'b'},
31 {"interface-namespaces", required_argument, nullptr, 'i'},
32 {"help", no_argument, nullptr, 'h'},
33 {0, 0, 0, 0},
34};
35
36ArgumentParser::ArgumentParser(int argc, char** argv)
37{
38 int option = 0;
39 while (-1 !=
40 (option = getopt_long(argc, argv, optionstr, options, nullptr)))
41 {
42 if ((option == '?') || (option == 'h'))
43 {
44 usage(argv);
45 exit(-1);
46 }
47
48 auto i = &options[0];
49 while ((i->val != option) && (i->val != 0))
50 ++i;
51
52 if (i->val)
53 arguments[i->name] = (i->has_arg ? optarg : true_string);
54 }
55}
56
57const std::string& ArgumentParser::operator[](const std::string& opt)
58{
59 auto i = arguments.find(opt);
60 if (i == arguments.end())
61 {
62 return empty_string;
63 }
64 else
65 {
66 return i->second;
67 }
68}
69
70void ArgumentParser::usage(char** argv)
71{
72 std::cerr << "Usage: " << argv[0] << " [options]" << std::endl;
73 std::cerr << "Options:" << std::endl;
74 std::cerr << " --help Print this menu" << std::endl;
75 std::cerr << " --service-namespaces=<services> Space separated list of ";
76 std::cerr << "service namespaces to whitelist\n";
77 std::cerr << " --service-blacklists=<services> Space separated list of ";
78 std::cerr << "service names to blacklist\n";
79 std::cerr << " --interface-namespaces=<ifaces> Space separated list of ";
80 std::cerr << "interface namespaces to whitelist\n";
81}