blob: a4b664e9586b9cd1a17ba072d3d3dc6dc39f0d2d [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
17#include <iostream>
18#include <iterator>
19#include <algorithm>
20#include "argument.hpp"
21
22namespace phosphor
23{
24namespace gpio
25{
26
27using namespace std::string_literals;
28
29const std::string ArgumentParser::trueString = "true"s;
30const std::string ArgumentParser::emptyString = ""s;
31
Matt Spinlerd5636b02017-09-01 14:00:19 -050032const char* ArgumentParser::optionStr = "p:k:n:i:d:?h";
Gunnar Mills72639152017-06-22 15:06:21 -050033const option ArgumentParser::options[] =
34{
35 { "path", required_argument, nullptr, 'p' },
36 { "key", required_argument, nullptr, 'k' },
37 { "name", required_argument, nullptr, 'n' },
38 { "inventory", required_argument, nullptr, 'i' },
Matt Spinlerd5636b02017-09-01 14:00:19 -050039 { "drivers", required_argument, nullptr, 'd' },
Gunnar Mills72639152017-06-22 15:06:21 -050040 { "help", no_argument, nullptr, 'h' },
41 { 0, 0, 0, 0},
42};
43
44ArgumentParser::ArgumentParser(int argc, char** argv)
45{
46 auto option = 0;
47 while (-1 != (option = getopt_long(argc, argv,
48 optionStr, options, nullptr)))
49 {
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"
88 " that will be created\n";
89 std::cerr << " --path=<path> Path of device to read for GPIO pin"
90 " state to determine presence of inventory item\n";
91 std::cerr << " --key=<key> Input GPIO key number\n";
92 std::cerr << " --name=<name> Pretty name of the inventory"
93 " item\n";
Matt Spinlerd5636b02017-09-01 14:00:19 -050094 std::cerr << " --drivers=<drivers> List of drivers to bind when card"
95 " is added and unbind when card is removed\n";
96 std::cerr << " Format is a space separated list"
97 " of path,device pairs. For example:\n";
98 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