blob: d668de1f7e361471fea53538177b984eba93fc2d [file] [log] [blame]
Matt Spinlerafb39132017-08-14 13:53:07 -05001/**
2 * Copyright © 2017 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#include <iostream>
17#include <iterator>
18#include <algorithm>
19#include "argument.hpp"
20
21namespace witherspoon
22{
23namespace power
24{
25
26void ArgumentParser::usage(char** argv)
27{
28 std::cerr << "Usage: " << argv[0] << " [options]\n";
29 std::cerr << "Options:\n";
30 std::cerr << " --help Print this menu\n";
31 std::cerr << " --action=<action> Action: pgood-monitor\n";
32 std::cerr << " --interval=<interval> Time to allow PGOOD to come up\n";
33 std::cerr << std::flush;
34}
35
36const option ArgumentParser::options[] =
37{
38 {"action", required_argument, NULL, 'a'},
39 {"interval", required_argument, NULL, 'i'},
40 {"help", no_argument, NULL, 'h'},
41 {0, 0, 0, 0},
42};
43
44const char* ArgumentParser::optionStr = "a:i:h?";
45ArgumentParser::ArgumentParser(int argc, char** argv)
46{
47 int option = 0;
48 while (-1 != (option = getopt_long(argc, argv, optionStr, options, NULL)))
49 {
50 if ((option == '?') || (option == 'h'))
51 {
52 usage(argv);
53 exit(-1);
54 }
55
56 auto i = &options[0];
57 while ((i->val != option) && (i->val != 0))
58 {
59 ++i;
60 }
61
62 if (i->val)
63 {
64 arguments[i->name] = (i->has_arg ? optarg : trueString);
65 }
66 }
67}
68
69const std::string& ArgumentParser::operator[](const std::string& opt)
70{
71 auto i = arguments.find(opt);
72 if (i == arguments.end())
73 {
74 return emptyString;
75 }
76 else
77 {
78 return i->second;
79 }
80}
81
82const std::string ArgumentParser::trueString = "true";
83const std::string ArgumentParser::emptyString = "";
84
85}
86}