blob: fd0c50dbc94ea4aa979cb35fa5ed74a0def90c74 [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{
28
29const std::string ArgumentParser::true_string = "true";
30const std::string ArgumentParser::empty_string = "";
31
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053032const char* ArgumentParser::optionstr = "p:?h";
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093033const option ArgumentParser::options[] = {
34 {"path", required_argument, nullptr, 'p'},
35 {"help", no_argument, nullptr, 'h'},
36 {0, 0, 0, 0},
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053037};
38
39ArgumentParser::ArgumentParser(int argc, char** argv)
40{
41 int option = 0;
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093042 while (-1 !=
43 (option = getopt_long(argc, argv, optionstr, options, nullptr)))
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053044 {
45 if ((option == '?') || (option == 'h'))
46 {
47 usage(argv);
48 exit(-1);
49 }
50
51 auto i = &options[0];
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093052 while ((i->val != option) && (i->val != 0))
53 ++i;
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053054
55 if (i->val)
56 arguments[i->name] = (i->has_arg ? optarg : true_string);
57 }
58}
59
60const std::string& ArgumentParser::operator[](const std::string& opt)
61{
62 auto i = arguments.find(opt);
63 if (i == arguments.end())
64 {
65 return empty_string;
66 }
67 else
68 {
69 return i->second;
70 }
71}
72
73void ArgumentParser::usage(char** argv)
74{
75 std::cerr << "Usage: " << argv[0] << " [options]" << std::endl;
76 std::cerr << "Options:" << std::endl;
77 std::cerr << " --help Print this menu" << std::endl;
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053078 std::cerr << " --path=<path> absolute path of LED in sysfs; like";
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093079 std::cerr << " /sys/class/leds/<name>" << std::endl;
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053080}
81} // namespace led
82} // namespace phosphor