blob: cd6e6daa8c8f6066f0a11d5e9000deca0362d8e4 [file] [log] [blame]
Matt Spinler389ca672017-05-09 10:50:28 -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 */
Matthew Barth3e1bb272020-05-26 11:09:21 -050016#include "argument.hpp"
17
18#include <algorithm>
Matt Spinler389ca672017-05-09 10:50:28 -050019#include <iostream>
20#include <iterator>
Matt Spinler389ca672017-05-09 10:50:28 -050021
22namespace phosphor
23{
24namespace fan
25{
26namespace util
27{
28
29ArgumentParser::ArgumentParser(int argc, char** argv)
30{
31 auto option = 0;
32 while (-1 != (option = getopt_long(argc, argv, optionstr, options, NULL)))
33 {
34 if ((option == '?') || (option == 'h'))
35 {
36 usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070037 exit(1);
Matt Spinler389ca672017-05-09 10:50:28 -050038 }
39
40 auto i = &options[0];
41 while ((i->val != option) && (i->val != 0))
42 {
43 ++i;
44 }
45
46 if (i->val)
47 {
48 arguments[i->name] = (i->has_arg ? optarg : true_string);
49 }
50 }
51}
52
53const std::string& ArgumentParser::operator[](const std::string& opt)
54{
55 auto i = arguments.find(opt);
56 if (i == arguments.end())
57 {
58 return empty_string;
59 }
60 else
61 {
62 return i->second;
63 }
64}
65
66void ArgumentParser::usage(char** argv)
67{
68 std::cerr << "Usage: " << argv[0] << " [options]\n";
69 std::cerr << "Options:\n";
70 std::cerr << " --help Print this menu\n";
Matthew Barth3e1bb272020-05-26 11:09:21 -050071 std::cerr
72 << " --init Sets fans to full speed, delays, exits\n";
Matt Spinler389ca672017-05-09 10:50:28 -050073 std::cerr << " --control Start fan control algorithm\n";
74 std::cerr << std::flush;
75}
76
Matthew Barth3e1bb272020-05-26 11:09:21 -050077const option ArgumentParser::options[] = {
78 {"init", no_argument, NULL, 'i'},
79 {"control", no_argument, NULL, 'c'},
80 {"help", no_argument, NULL, 'h'},
81 {0, 0, 0, 0},
Matt Spinler389ca672017-05-09 10:50:28 -050082};
83
84const char* ArgumentParser::optionstr = "ich?";
85
Matt Spinler06bae852017-05-24 11:42:25 -050086const std::string ArgumentParser::true_string = "true";
Matt Spinler389ca672017-05-09 10:50:28 -050087const std::string ArgumentParser::empty_string = "";
88
Matthew Barth3e1bb272020-05-26 11:09:21 -050089} // namespace util
90} // namespace fan
91} // namespace phosphor
Matt Spinler389ca672017-05-09 10:50:28 -050092// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4