blob: 389fa54a0f4b3daaa454bc87a58e90b6b8ff36f4 [file] [log] [blame]
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -05001/**
2 * Copyright © 2018 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#include "argument.hpp"
17
18#include <algorithm>
19#include <iostream>
20#include <iterator>
21
Nan Zhoue1289ad2021-12-28 11:02:56 -080022namespace phosphor::certs::util
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050023{
24
25ArgumentParser::ArgumentParser(int argc, char** argv)
26{
27 auto option = 0;
Nan Zhoucfb58022021-12-28 11:02:26 -080028 while (-1 !=
29 (option = getopt_long(argc, argv, optionstr, options, nullptr)))
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050030 {
31 if ((option == '?') || (option == 'h'))
32 {
33 usage(argv);
34 exit(-1);
35 }
36
37 auto i = &options[0];
38 while ((i->val != option) && (i->val != 0))
39 {
40 ++i;
41 }
42
43 if (i->val)
44 {
45 arguments[i->name] = (i->has_arg ? optarg : true_string);
46 }
47 }
48}
49
50const std::string& ArgumentParser::operator[](const std::string& opt)
51{
52 auto i = arguments.find(opt);
53 if (i == arguments.end())
54 {
55 return empty_string;
56 }
57 else
58 {
59 return i->second;
60 }
61}
62
63void ArgumentParser::usage(char** argv)
64{
65 std::cerr << "Usage: " << argv[0] << " [options]\n";
66 std::cerr << "Options:\n";
67 std::cerr << " --help Print this menu\n";
68 std::cerr << " --type certificate type\n";
Jayanth Othayothb50789c2018-10-09 07:13:54 -050069 std::cerr << " Valid types: client,server,authority\n";
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050070 std::cerr << " --endpoint d-bus endpoint\n";
71 std::cerr << " --path certificate file path\n";
72 std::cerr << " --unit=<name> Optional systemd unit need to reload\n";
73 std::cerr << std::flush;
74}
75
76const option ArgumentParser::options[] = {
77 {"type", required_argument, nullptr, 't'},
78 {"endpoint", required_argument, nullptr, 'e'},
79 {"path", required_argument, nullptr, 'p'},
80 {"unit", optional_argument, nullptr, 'u'},
81 {"help", no_argument, nullptr, 'h'},
82 {0, 0, 0, 0},
83};
84
85const char* ArgumentParser::optionstr = "tepuh?";
86
87const std::string ArgumentParser::true_string = "true";
88const std::string ArgumentParser::empty_string = "";
89
Nan Zhoue1289ad2021-12-28 11:02:56 -080090} // namespace phosphor::certs::util