blob: 099f5ca4413b88e8b4de9fb5ce9c47dd391a81bd [file] [log] [blame]
Jayashree Dhanapalb6779842022-10-07 13:34: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 "argument.hpp"
18
19#include <algorithm>
20#include <cassert>
21#include <iostream>
22#include <iterator>
23
24namespace phosphor
25{
26namespace led
27{
28// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
29ArgumentParser::ArgumentParser(int argc, char* argv[])
30{
31 int option = 0;
32 while (-1 !=
33 (option = getopt_long(argc, argv, optionstr, &options[0], nullptr)))
34 {
35 switch (option)
36 {
37 case '?':
38 case 'h':
39 usage(argv);
40 exit(-1);
41 break;
42 case 'p':
43 arguments["path"] = optarg;
44 break;
45 }
46 }
47}
48
49const std::string& ArgumentParser::operator[](const std::string& opt)
50{
51 static const std::string emptyString{};
52
53 auto i = arguments.find(opt);
54 if (i == arguments.end())
55 {
56 return emptyString;
57 }
58
59 return i->second;
60}
61
62// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
63void ArgumentParser::usage(char* argv[])
64{
65 // NOLINTNEXTLINE
66 std::cerr << "Usage: " << argv[0] << " [options]" << std::endl;
67 std::cerr << "Options:" << std::endl;
68 std::cerr << " --help Print this menu" << std::endl;
69 std::cerr << " --path=<path> absolute path of LED in sysfs; like";
70 std::cerr << " /sys/class/leds/<name>" << std::endl;
71}
72} // namespace led
73} // namespace phosphor