blob: 342515ce344af8544899c1e51e6a5daab9f37ddb [file] [log] [blame]
Brandon Wyman24e422f2017-07-25 19:40:14 -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 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050016#include "argument.hpp"
17
Brandon Wyman24e422f2017-07-25 19:40:14 -050018#include <algorithm>
19#include <cassert>
Matt Spinlerf0f02b92018-10-25 16:12:43 -050020#include <iostream>
21#include <iterator>
Brandon Wyman24e422f2017-07-25 19:40:14 -050022
23namespace witherspoon
24{
25namespace power
26{
27
28ArgumentParser::ArgumentParser(int argc, char** argv)
29{
30 auto option = 0;
31 while (-1 != (option = getopt_long(argc, argv, optionStr, options, NULL)))
32 {
33 if ((option == '?') || (option == 'h'))
34 {
35 usage(argv);
36 exit(-1);
37 }
38
39 auto i = &options[0];
40 while ((i->val != option) && (i->val != 0))
41 {
42 ++i;
43 }
44
45 if (i->val)
46 {
47 arguments[i->name] = (i->has_arg ? optarg : trueString);
48 }
49 }
50}
51
52const std::string& ArgumentParser::operator[](const std::string& opt)
53{
54 auto i = arguments.find(opt);
55 if (i == arguments.end())
56 {
57 return emptyString;
58 }
59 else
60 {
61 return i->second;
62 }
63}
64
65void ArgumentParser::usage(char** argv)
66{
67 std::cerr << "Usage: " << argv[0] << " [options]\n";
68 std::cerr << "Options:\n";
Matt Spinler15b5f402018-01-18 14:37:34 -060069 std::cerr << " --help Print this menu\n";
70 std::cerr << " --path=<objpath> Path to location to"
71 " monitor\n";
72 std::cerr << " --instance=<instance number> Instance number for"
73 " this power supply\n";
74 std::cerr << " --inventory=<inventory path> Inventory path for"
75 " this power supply\n";
76 std::cerr << " --num-history-records=<num records> Number of input"
77 " power history records to provide on D-Bus\n";
78 std::cerr << " --sync-gpio-path=<path> GPIO chip device"
79 " for the GPIO that performs the sync function\n";
80 std::cerr << " --sync-gpio-num=<path> GPIO number for the"
81 " GPIO that performs the sync function\n";
Brandon Wyman24e422f2017-07-25 19:40:14 -050082 std::cerr << std::flush;
83}
84
Matt Spinlerf0f02b92018-10-25 16:12:43 -050085const option ArgumentParser::options[] = {
86 {"path", required_argument, NULL, 'p'},
87 {"instance", required_argument, NULL, 'n'},
88 {"inventory", required_argument, NULL, 'i'},
89 {"num-history-records", required_argument, NULL, 'r'},
90 {"sync-gpio-path", required_argument, NULL, 'a'},
91 {"sync-gpio-num", required_argument, NULL, 'u'},
92 {"help", no_argument, NULL, 'h'},
93 {0, 0, 0, 0},
Brandon Wyman24e422f2017-07-25 19:40:14 -050094};
95
Matt Spinler15b5f402018-01-18 14:37:34 -060096const char* ArgumentParser::optionStr = "p:n:i:r:a:u:h";
Brandon Wyman24e422f2017-07-25 19:40:14 -050097
98const std::string ArgumentParser::trueString = "true";
99const std::string ArgumentParser::emptyString = "";
100
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500101} // namespace power
102} // namespace witherspoon