blob: 33194ce23698f1074803eed340370664878d22ec [file] [log] [blame]
Matt Spinler8b633b72017-06-02 12:35:59 -05001/**
2 * Copyright © 2017 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 */
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050016#include "argument.hpp"
17
18#include <algorithm>
Matt Spinler8b633b72017-06-02 12:35:59 -050019#include <iostream>
20#include <iterator>
Matt Spinler8b633b72017-06-02 12:35:59 -050021
22namespace phosphor
23{
24namespace unit
25{
26namespace failure
27{
28
29ArgumentParser::ArgumentParser(int argc, char** argv)
30{
31 int option = 0;
32 while (-1 != (option = getopt_long(argc, argv, optionStr, options, NULL)))
33 {
34 if ((option == '?') || (option == 'h'))
35 {
36 usage(argv);
37 exit(-1);
38 }
39
40 auto i = &options[0];
41 while ((i->val != option) && (i->val != 0))
42 {
43 ++i;
44 }
45
46 if (i->val)
47 {
48 arguments[i->name] = (i->has_arg ? optarg : trueString);
49 }
50 }
51}
52
53const std::string& ArgumentParser::operator[](const std::string& opt)
54{
55 auto i = arguments.find(opt);
56 if (i == arguments.end())
57 {
58 return emptyString;
59 }
60 else
61 {
62 return i->second;
63 }
64}
65
66void ArgumentParser::usage(char** argv)
67{
68 std::cerr << "Usage: " << argv[0] << " [options]\n";
69 std::cerr << "Options:\n";
70 std::cerr << " --help Print this menu\n";
71 std::cerr << " --source=<source> The source unit to monitor\n";
72 std::cerr << " --target=<target> The target unit to start or stop\n";
73 std::cerr << " --action=<action> Target unit action - start or stop\n";
74 std::cerr << std::flush;
75}
76
Ed Tanous167e2372018-05-07 11:59:10 -070077const option ArgumentParser::options[] = {
78 {"source", required_argument, NULL, 's'},
79 {"action", required_argument, NULL, 'a'},
80 {"target", required_argument, NULL, 't'},
81 {"help", no_argument, NULL, 'h'},
82 {0, 0, 0, 0},
Matt Spinler8b633b72017-06-02 12:35:59 -050083};
84
85const char* ArgumentParser::optionStr = "s:a:t:h?";
86
87const std::string ArgumentParser::trueString = "true";
88const std::string ArgumentParser::emptyString = "";
Matt Spinlercc6ee9c2018-09-19 13:23:13 -050089} // namespace failure
90} // namespace unit
91} // namespace phosphor