blob: c0d0ee358616a1472d9ef7c2cd4fec3428411dc3 [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
Anthony Wilson206f0042019-05-02 00:02:23 -050033const char* ArgumentParser::optionStr = "p:k:n:i:d:e:?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'},
Anthony Wilson206f0042019-05-02 00:02:23 -050040 {"extra-ifaces", required_argument, nullptr, 'e'},
Patrick Venturedace6802018-11-01 16:52:10 -070041 {"help", no_argument, nullptr, 'h'},
42 {0, 0, 0, 0},
Gunnar Mills72639152017-06-22 15:06:21 -050043};
44
45ArgumentParser::ArgumentParser(int argc, char** argv)
46{
47 auto option = 0;
Patrick Venturedace6802018-11-01 16:52:10 -070048 while (-1 !=
49 (option = getopt_long(argc, argv, optionStr, options, nullptr)))
Gunnar Mills72639152017-06-22 15:06:21 -050050 {
51 if ((option == '?') || (option == 'h'))
52 {
53 usage(argv);
54 exit(-1);
55 }
56
57 auto i = &options[0];
58 while ((i->val != option) && (i->val != 0))
59 {
60 ++i;
61 }
62
63 if (i->val)
64 {
65 arguments[i->name] = (i->has_arg ? optarg : trueString);
66 }
67 }
68}
69
70const std::string& ArgumentParser::operator[](const std::string& opt)
71{
72 auto i = arguments.find(opt);
73 if (i == arguments.end())
74 {
75 return emptyString;
76 }
77 else
78 {
79 return i->second;
80 }
81}
82
83void ArgumentParser::usage(char** argv)
84{
85 std::cerr << "Usage: " << argv[0] << " [options]\n";
86 std::cerr << "Options:\n";
87 std::cerr << " --help Print this menu\n";
88 std::cerr << " --inventory=<inventory> Object path under inventory"
Patrick Venturedace6802018-11-01 16:52:10 -070089 " that will be created\n";
Gunnar Mills72639152017-06-22 15:06:21 -050090 std::cerr << " --path=<path> Path of device to read for GPIO pin"
Patrick Venturedace6802018-11-01 16:52:10 -070091 " state to determine presence of inventory item\n";
Gunnar Mills72639152017-06-22 15:06:21 -050092 std::cerr << " --key=<key> Input GPIO key number\n";
93 std::cerr << " --name=<name> Pretty name of the inventory"
Patrick Venturedace6802018-11-01 16:52:10 -070094 " item\n";
Matt Spinlerd5636b02017-09-01 14:00:19 -050095 std::cerr << " --drivers=<drivers> List of drivers to bind when card"
Patrick Venturedace6802018-11-01 16:52:10 -070096 " is added and unbind when card is removed\n";
Matt Spinlerd5636b02017-09-01 14:00:19 -050097 std::cerr << " Format is a space separated list"
Patrick Venturedace6802018-11-01 16:52:10 -070098 " of path,device pairs. For example:\n";
Matt Spinlerd5636b02017-09-01 14:00:19 -050099 std::cerr << " "
100 "/sys/bus/i2c/drivers/some-driver,3-0068\n";
Anthony Wilson206f0042019-05-02 00:02:23 -0500101 std::cerr << " --extra-ifaces=<ifaces> List of interfaces to associate to"
102 " inventory item\n";
103 std::cerr << " Format is a comma separated list"
104 " of interfaces. For example:\n";
105 std::cerr << " "
106 "/xyz/openbmc_project/.../1,/xyz/openbmc_project/.../2\n";
Gunnar Mills72639152017-06-22 15:06:21 -0500107 std::cerr << std::flush;
108 exit(-1);
109}
110} // namespace gpio
111} // namespace phosphor