1*b6779846SJayashree Dhanapal /**
2*b6779846SJayashree Dhanapal * Copyright © 2016 IBM Corporation
3*b6779846SJayashree Dhanapal *
4*b6779846SJayashree Dhanapal * Licensed under the Apache License, Version 2.0 (the "License");
5*b6779846SJayashree Dhanapal * you may not use this file except in compliance with the License.
6*b6779846SJayashree Dhanapal * You may obtain a copy of the License at
7*b6779846SJayashree Dhanapal *
8*b6779846SJayashree Dhanapal * http://www.apache.org/licenses/LICENSE-2.0
9*b6779846SJayashree Dhanapal *
10*b6779846SJayashree Dhanapal * Unless required by applicable law or agreed to in writing, software
11*b6779846SJayashree Dhanapal * distributed under the License is distributed on an "AS IS" BASIS,
12*b6779846SJayashree Dhanapal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*b6779846SJayashree Dhanapal * See the License for the specific language governing permissions and
14*b6779846SJayashree Dhanapal * limitations under the License.
15*b6779846SJayashree Dhanapal */
16*b6779846SJayashree Dhanapal
17*b6779846SJayashree Dhanapal #include "argument.hpp"
18*b6779846SJayashree Dhanapal
19*b6779846SJayashree Dhanapal #include <algorithm>
20*b6779846SJayashree Dhanapal #include <cassert>
21*b6779846SJayashree Dhanapal #include <iostream>
22*b6779846SJayashree Dhanapal #include <iterator>
23*b6779846SJayashree Dhanapal
24*b6779846SJayashree Dhanapal namespace phosphor
25*b6779846SJayashree Dhanapal {
26*b6779846SJayashree Dhanapal namespace led
27*b6779846SJayashree Dhanapal {
28*b6779846SJayashree Dhanapal // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
ArgumentParser(int argc,char * argv[])29*b6779846SJayashree Dhanapal ArgumentParser::ArgumentParser(int argc, char* argv[])
30*b6779846SJayashree Dhanapal {
31*b6779846SJayashree Dhanapal int option = 0;
32*b6779846SJayashree Dhanapal while (-1 !=
33*b6779846SJayashree Dhanapal (option = getopt_long(argc, argv, optionstr, &options[0], nullptr)))
34*b6779846SJayashree Dhanapal {
35*b6779846SJayashree Dhanapal switch (option)
36*b6779846SJayashree Dhanapal {
37*b6779846SJayashree Dhanapal case '?':
38*b6779846SJayashree Dhanapal case 'h':
39*b6779846SJayashree Dhanapal usage(argv);
40*b6779846SJayashree Dhanapal exit(-1);
41*b6779846SJayashree Dhanapal break;
42*b6779846SJayashree Dhanapal case 'p':
43*b6779846SJayashree Dhanapal arguments["path"] = optarg;
44*b6779846SJayashree Dhanapal break;
45*b6779846SJayashree Dhanapal }
46*b6779846SJayashree Dhanapal }
47*b6779846SJayashree Dhanapal }
48*b6779846SJayashree Dhanapal
operator [](const std::string & opt)49*b6779846SJayashree Dhanapal const std::string& ArgumentParser::operator[](const std::string& opt)
50*b6779846SJayashree Dhanapal {
51*b6779846SJayashree Dhanapal static const std::string emptyString{};
52*b6779846SJayashree Dhanapal
53*b6779846SJayashree Dhanapal auto i = arguments.find(opt);
54*b6779846SJayashree Dhanapal if (i == arguments.end())
55*b6779846SJayashree Dhanapal {
56*b6779846SJayashree Dhanapal return emptyString;
57*b6779846SJayashree Dhanapal }
58*b6779846SJayashree Dhanapal
59*b6779846SJayashree Dhanapal return i->second;
60*b6779846SJayashree Dhanapal }
61*b6779846SJayashree Dhanapal
62*b6779846SJayashree Dhanapal // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
usage(char * argv[])63*b6779846SJayashree Dhanapal void ArgumentParser::usage(char* argv[])
64*b6779846SJayashree Dhanapal {
65*b6779846SJayashree Dhanapal // NOLINTNEXTLINE
66*b6779846SJayashree Dhanapal std::cerr << "Usage: " << argv[0] << " [options]" << std::endl;
67*b6779846SJayashree Dhanapal std::cerr << "Options:" << std::endl;
68*b6779846SJayashree Dhanapal std::cerr << " --help Print this menu" << std::endl;
69*b6779846SJayashree Dhanapal std::cerr << " --path=<path> absolute path of LED in sysfs; like";
70*b6779846SJayashree Dhanapal std::cerr << " /sys/class/leds/<name>" << std::endl;
71*b6779846SJayashree Dhanapal }
72*b6779846SJayashree Dhanapal } // namespace led
73*b6779846SJayashree Dhanapal } // namespace phosphor
74