blob: 3849cae2367d9e8b3b08f1b0b0cccff2dd8a1083 [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 Williams3667cf32015-10-20 22:39:11 -050016#include <iostream>
17#include <iterator>
18#include <algorithm>
19#include <cassert>
Matthew Barth6292aee2016-10-06 10:15:48 -050020#include "argument.hpp"
Patrick Williams3667cf32015-10-20 22:39:11 -050021
22ArgumentParser::ArgumentParser(int argc, char** argv)
23{
24 int option = 0;
Brad Bishop6bb97a92016-12-19 13:06:40 -050025 while (-1 != (option = getopt_long(argc, argv, optionstr, options, NULL)))
Patrick Williams3667cf32015-10-20 22:39:11 -050026 {
27 if ((option == '?') || (option == 'h'))
28 {
29 usage(argv);
30 exit(-1);
31 }
32
33 auto i = &options[0];
Brad Bishop6bb97a92016-12-19 13:06:40 -050034 while ((i->val != option) && (i->val != 0))
35 {
36 ++i;
37 }
Patrick Williams3667cf32015-10-20 22:39:11 -050038
39 if (i->val)
Brad Bishop6bb97a92016-12-19 13:06:40 -050040 {
Patrick Williams3667cf32015-10-20 22:39:11 -050041 arguments[i->name] = (i->has_arg ? optarg : true_string);
Brad Bishop6bb97a92016-12-19 13:06:40 -050042 }
Patrick Williams3667cf32015-10-20 22:39:11 -050043 }
44}
45
46const std::string& ArgumentParser::operator[](const std::string& opt)
47{
48 auto i = arguments.find(opt);
49 if (i == arguments.end())
50 {
51 return empty_string;
52 }
53 else
54 {
55 return i->second;
56 }
57}
58
59void ArgumentParser::usage(char** argv)
60{
Brad Bishopc8065532017-01-05 15:46:47 -050061 std::cerr << "Usage: " << argv[0] << " [options]\n";
62 std::cerr << "Options:\n";
63 std::cerr << " --help print this menu\n";
64 std::cerr << " --path=<path> sysfs location to monitor\n";
Matt Spinler147b0332018-03-05 12:07:46 -060065 std::cerr << " --dev-path=<path> device path to monitor\n";
Brad Bishopc8065532017-01-05 15:46:47 -050066 std::cerr << std::flush;
Patrick Williams3667cf32015-10-20 22:39:11 -050067}
68
69const option ArgumentParser::options[] =
70{
71 { "path", required_argument, NULL, 'p' },
Matt Spinler147b0332018-03-05 12:07:46 -060072 { "dev-path", required_argument, NULL, 'o' },
Patrick Williams3667cf32015-10-20 22:39:11 -050073 { "help", no_argument, NULL, 'h' },
74 { 0, 0, 0, 0},
75};
76
Brad Bishop4da01612017-01-05 21:10:06 -050077const char* ArgumentParser::optionstr = "o:p:?h";
Patrick Williams3667cf32015-10-20 22:39:11 -050078
79const std::string ArgumentParser::true_string = "true";
80const std::string ArgumentParser::empty_string = "";
Brad Bishop03476f12016-12-19 13:09:12 -050081
82// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4