blob: 74bdda266f80667aab8891080e2c63564fdae16d [file] [log] [blame]
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +05301/**
2 * Copyright © 2016 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
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053019#include <algorithm>
20#include <cassert>
Patrick Venturedace6802018-11-01 16:52:10 -070021#include <iostream>
22#include <iterator>
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053023
24namespace phosphor
25{
26namespace gpio
27{
28
29using namespace std::string_literals;
30
31const std::string ArgumentParser::trueString = "true"s;
32const std::string ArgumentParser::emptyString = ""s;
33
34const char* ArgumentParser::optionStr = "p:k:r:t:?h";
Patrick Venturedace6802018-11-01 16:52:10 -070035const option ArgumentParser::options[] = {
36 {"path", required_argument, nullptr, 'p'},
37 {"key", required_argument, nullptr, 'k'},
38 {"polarity", required_argument, nullptr, 'r'},
39 {"target", required_argument, nullptr, 't'},
40 {"continue", no_argument, nullptr, 'c'},
41 {"help", no_argument, nullptr, 'h'},
42 {0, 0, 0, 0},
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053043};
44
45ArgumentParser::ArgumentParser(int argc, char** argv)
46{
47 int option = 0;
Patrick Venturedace6802018-11-01 16:52:10 -070048 while (-1 !=
49 (option = getopt_long(argc, argv, optionStr, options, nullptr)))
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053050 {
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 {
Gunnar Mills96e01b72018-08-14 11:59:05 -050065 // optional argument may get nullptr for optarg
Lei YUbc4a4ff2018-04-11 13:33:25 +080066 // make it empty string in such case
67 auto arg = (optarg == nullptr ? "" : optarg);
68 arguments[i->name] = (i->has_arg ? arg : trueString);
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053069 }
70 }
71}
72
73const std::string& ArgumentParser::operator[](const std::string& opt)
74{
75 auto i = arguments.find(opt);
76 if (i == arguments.end())
77 {
78 return emptyString;
79 }
80 else
81 {
82 return i->second;
83 }
84}
85
86void ArgumentParser::usage(char** argv)
87{
88 std::cerr << "Usage: " << argv[0] << " [options]\n";
89 std::cerr << "Options:\n";
90 std::cerr << " --help Print this menu\n";
91 std::cerr << " --path=<path> Path of input device."
Patrick Venturedace6802018-11-01 16:52:10 -070092 " Ex: /dev/input/event2\n";
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053093 std::cerr << " --key=<key> Input GPIO key number\n";
94 std::cerr << " --polarity=<polarity> Asertion polarity to look for."
Patrick Venturedace6802018-11-01 16:52:10 -070095 " This is 0 / 1 \n";
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053096 std::cerr << " --target=<systemd unit> Systemd unit to be called on GPIO"
Patrick Venturedace6802018-11-01 16:52:10 -070097 " state change\n";
Lei YU065b7672018-04-19 10:59:40 +080098 std::cerr << " [--continue] Whether or not to continue"
Patrick Venturedace6802018-11-01 16:52:10 -070099 " after key pressed\n";
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +0530100}
101} // namespace gpio
102} // namespace phosphor