blob: 99ee710f7968d44623e4590e19f5b12e9a83c445 [file] [log] [blame]
Brad Bishop29dbfa62016-12-19 13:39:57 -05001/**
2 * Copyright © 2016 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 */
Patrick Venture043d3232018-08-31 10:10:53 -070016#include "argument.hpp"
17
Patrick Williams3667cf32015-10-20 22:39:11 -050018#include <algorithm>
19#include <cassert>
Patrick Venture043d3232018-08-31 10:10:53 -070020#include <iostream>
21#include <iterator>
Patrick Williams3667cf32015-10-20 22:39:11 -050022
23ArgumentParser::ArgumentParser(int argc, char** argv)
24{
25 int option = 0;
Brad Bishop6bb97a92016-12-19 13:06:40 -050026 while (-1 != (option = getopt_long(argc, argv, optionstr, options, NULL)))
Patrick Williams3667cf32015-10-20 22:39:11 -050027 {
28 if ((option == '?') || (option == 'h'))
29 {
30 usage(argv);
31 exit(-1);
32 }
33
34 auto i = &options[0];
Brad Bishop6bb97a92016-12-19 13:06:40 -050035 while ((i->val != option) && (i->val != 0))
36 {
37 ++i;
38 }
Patrick Williams3667cf32015-10-20 22:39:11 -050039
40 if (i->val)
Brad Bishop6bb97a92016-12-19 13:06:40 -050041 {
Patrick Williams3667cf32015-10-20 22:39:11 -050042 arguments[i->name] = (i->has_arg ? optarg : true_string);
Brad Bishop6bb97a92016-12-19 13:06:40 -050043 }
Patrick Williams3667cf32015-10-20 22:39:11 -050044 }
45}
46
47const std::string& ArgumentParser::operator[](const std::string& opt)
48{
49 auto i = arguments.find(opt);
50 if (i == arguments.end())
51 {
52 return empty_string;
53 }
54 else
55 {
56 return i->second;
57 }
58}
59
60void ArgumentParser::usage(char** argv)
61{
Brad Bishopc8065532017-01-05 15:46:47 -050062 std::cerr << "Usage: " << argv[0] << " [options]\n";
63 std::cerr << "Options:\n";
64 std::cerr << " --help print this menu\n";
65 std::cerr << " --path=<path> sysfs location to monitor\n";
Matt Spinler147b0332018-03-05 12:07:46 -060066 std::cerr << " --dev-path=<path> device path to monitor\n";
Brad Bishopc8065532017-01-05 15:46:47 -050067 std::cerr << std::flush;
Patrick Williams3667cf32015-10-20 22:39:11 -050068}
69
Patrick Venture043d3232018-08-31 10:10:53 -070070// clang-format off
71const option ArgumentParser::options[] = {
72 {"path", required_argument, NULL, 'p'},
73 {"dev-path", required_argument, NULL, 'o'},
74 {"help", no_argument, NULL, 'h'},
75 {0, 0, 0, 0},
Patrick Williams3667cf32015-10-20 22:39:11 -050076};
Patrick Venture043d3232018-08-31 10:10:53 -070077// clang-format on
Patrick Williams3667cf32015-10-20 22:39:11 -050078
Brad Bishop4da01612017-01-05 21:10:06 -050079const char* ArgumentParser::optionstr = "o:p:?h";
Patrick Williams3667cf32015-10-20 22:39:11 -050080
81const std::string ArgumentParser::true_string = "true";
82const std::string ArgumentParser::empty_string = "";
Brad Bishop03476f12016-12-19 13:09:12 -050083
84// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4