blob: e25642670bfae4bc57cebb8f5f237750432251a2 [file] [log] [blame]
Gunnar Mills72639152017-06-22 15:06:21 -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
Patrick Venturedace6802018-11-01 16:52:10 -070017#include "argument.hpp"
18
19#include <algorithm>
Gunnar Mills72639152017-06-22 15:06:21 -050020#include <iostream>
21#include <iterator>
Gunnar Mills72639152017-06-22 15:06:21 -050022
23namespace phosphor
24{
25namespace gpio
26{
27
28using namespace std::string_literals;
29
30const std::string ArgumentParser::trueString = "true"s;
31const std::string ArgumentParser::emptyString = ""s;
32
Matt Spinlerd5636b02017-09-01 14:00:19 -050033const char* ArgumentParser::optionStr = "p:k:n:i:d:?h";
Patrick Venturedace6802018-11-01 16:52:10 -070034const option ArgumentParser::options[] = {
35 {"path", required_argument, nullptr, 'p'},
36 {"key", required_argument, nullptr, 'k'},
37 {"name", required_argument, nullptr, 'n'},
38 {"inventory", required_argument, nullptr, 'i'},
39 {"drivers", required_argument, nullptr, 'd'},
40 {"help", no_argument, nullptr, 'h'},
41 {0, 0, 0, 0},
Gunnar Mills72639152017-06-22 15:06:21 -050042};
43
44ArgumentParser::ArgumentParser(int argc, char** argv)
45{
46 auto option = 0;
Patrick Venturedace6802018-11-01 16:52:10 -070047 while (-1 !=
48 (option = getopt_long(argc, argv, optionStr, options, nullptr)))
Gunnar Mills72639152017-06-22 15:06:21 -050049 {
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
82void ArgumentParser::usage(char** argv)
83{
84 std::cerr << "Usage: " << argv[0] << " [options]\n";
85 std::cerr << "Options:\n";
86 std::cerr << " --help Print this menu\n";
87 std::cerr << " --inventory=<inventory> Object path under inventory"
Patrick Venturedace6802018-11-01 16:52:10 -070088 " that will be created\n";
Gunnar Mills72639152017-06-22 15:06:21 -050089 std::cerr << " --path=<path> Path of device to read for GPIO pin"
Patrick Venturedace6802018-11-01 16:52:10 -070090 " state to determine presence of inventory item\n";
Gunnar Mills72639152017-06-22 15:06:21 -050091 std::cerr << " --key=<key> Input GPIO key number\n";
92 std::cerr << " --name=<name> Pretty name of the inventory"
Patrick Venturedace6802018-11-01 16:52:10 -070093 " item\n";
Matt Spinlerd5636b02017-09-01 14:00:19 -050094 std::cerr << " --drivers=<drivers> List of drivers to bind when card"
Patrick Venturedace6802018-11-01 16:52:10 -070095 " is added and unbind when card is removed\n";
Matt Spinlerd5636b02017-09-01 14:00:19 -050096 std::cerr << " Format is a space separated list"
Patrick Venturedace6802018-11-01 16:52:10 -070097 " of path,device pairs. For example:\n";
Matt Spinlerd5636b02017-09-01 14:00:19 -050098 std::cerr << " "
99 "/sys/bus/i2c/drivers/some-driver,3-0068\n";
Gunnar Mills72639152017-06-22 15:06:21 -0500100 std::cerr << std::flush;
101 exit(-1);
102}
103} // namespace gpio
104} // namespace phosphor