blob: c47dcd9c35152989d6d49275e6c5ee1d8ffe01f1 [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
17#include <iostream>
18#include <iterator>
19#include <algorithm>
20#include <cassert>
21#include "argument.hpp"
22
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
33const char* ArgumentParser::optionStr = "p:k:r:t:?h";
34const option ArgumentParser::options[] =
35{
36 { "path", required_argument, nullptr, 'p' },
37 { "key", required_argument, nullptr, 'k' },
38 { "polarity", required_argument, nullptr, 'r' },
39 { "target", required_argument, nullptr, 't' },
Lei YU065b7672018-04-19 10:59:40 +080040 { "continue", no_argument, nullptr, 'c' },
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053041 { "help", no_argument, nullptr, 'h' },
42 { 0, 0, 0, 0},
43};
44
45ArgumentParser::ArgumentParser(int argc, char** argv)
46{
47 int option = 0;
48 while (-1 != (option = getopt_long(argc, argv, 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 {
Lei YUbc4a4ff2018-04-11 13:33:25 +080064 // optinal argument may get nullptr for optarg
65 // make it empty string in such case
66 auto arg = (optarg == nullptr ? "" : optarg);
67 arguments[i->name] = (i->has_arg ? arg : trueString);
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053068 }
69 }
70}
71
72const std::string& ArgumentParser::operator[](const std::string& opt)
73{
74 auto i = arguments.find(opt);
75 if (i == arguments.end())
76 {
77 return emptyString;
78 }
79 else
80 {
81 return i->second;
82 }
83}
84
85void ArgumentParser::usage(char** argv)
86{
87 std::cerr << "Usage: " << argv[0] << " [options]\n";
88 std::cerr << "Options:\n";
89 std::cerr << " --help Print this menu\n";
90 std::cerr << " --path=<path> Path of input device."
91 " Ex: /dev/input/event2\n";
92 std::cerr << " --key=<key> Input GPIO key number\n";
93 std::cerr << " --polarity=<polarity> Asertion polarity to look for."
94 " This is 0 / 1 \n";
95 std::cerr << " --target=<systemd unit> Systemd unit to be called on GPIO"
96 " state change\n";
Lei YU065b7672018-04-19 10:59:40 +080097 std::cerr << " [--continue] Whether or not to continue"
Lei YUbc4a4ff2018-04-11 13:33:25 +080098 " after key pressed\n";
Vishwanatha Subbannaaffea8b2017-04-04 14:02:16 +053099}
100} // namespace gpio
101} // namespace phosphor