blob: c756649d51a73816680fc5888d256bd76700d0d5 [file] [log] [blame]
Matthew Barth6ad28432017-08-22 11:18:19 -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 phosphor
22{
23namespace fan
24{
25namespace util
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);
William A. Kennington III3e781062018-10-19 17:18:34 -070036 exit(1);
Matthew Barth6ad28432017-08-22 11:18:19 -050037 }
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 : true_string);
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 empty_string;
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";
69 std::cerr << " --help Print this menu\n";
70 std::cerr << " --init Set fans to functional\n";
71 std::cerr << " --monitor Start fan funcitonal monitoring\n";
72 std::cerr << std::flush;
73}
74
75const option ArgumentParser::options[] =
76{
77 { "init", no_argument, NULL, 'i' },
78 { "monitor", no_argument, NULL, 'm' },
79 { "help", no_argument, NULL, 'h' },
80 { 0, 0, 0, 0 },
81};
82
83const char* ArgumentParser::optionstr = "imh?";
84
85const std::string ArgumentParser::true_string = "true";
86const std::string ArgumentParser::empty_string = "";
87
88}
89}
90}