blob: 56e1e9ce2c69fcef3123ffd839d39bd3315d1869 [file] [log] [blame]
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +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
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093017#include "argument.hpp"
18
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053019#include <algorithm>
20#include <cassert>
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093021#include <iostream>
22#include <iterator>
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053023
24namespace phosphor
25{
26namespace led
27{
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053028ArgumentParser::ArgumentParser(int argc, char** argv)
29{
30 int option = 0;
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093031 while (-1 !=
Andrew Jeffery96487702023-02-03 16:28:49 +103032 (option = getopt_long(argc, argv, optionstr, &options[0], nullptr)))
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053033 {
Andrew Jeffery9eb55922023-02-03 16:41:28 +103034 switch (option)
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053035 {
Andrew Jeffery9eb55922023-02-03 16:41:28 +103036 case '?':
37 case 'h':
38 usage(argv);
39 exit(-1);
40 break;
41 case 'p':
42 arguments["path"] = optarg;
43 break;
Andrew Jefferyd8e3da62023-02-03 16:30:12 +103044 }
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053045 }
46}
47
48const std::string& ArgumentParser::operator[](const std::string& opt)
49{
Andrew Jefferyf9e6cd32023-02-03 15:49:44 +103050 static const std::string emptyString{};
51
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053052 auto i = arguments.find(opt);
53 if (i == arguments.end())
54 {
Andrew Jefferyf9e6cd32023-02-03 15:49:44 +103055 return emptyString;
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053056 }
Andrew Jeffery1c73fa62023-02-03 16:43:49 +103057
58 return i->second;
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053059}
60
61void ArgumentParser::usage(char** argv)
62{
63 std::cerr << "Usage: " << argv[0] << " [options]" << std::endl;
64 std::cerr << "Options:" << std::endl;
65 std::cerr << " --help Print this menu" << std::endl;
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053066 std::cerr << " --path=<path> absolute path of LED in sysfs; like";
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093067 std::cerr << " /sys/class/leds/<name>" << std::endl;
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053068}
69} // namespace led
70} // namespace phosphor