Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 1 | /** |
| 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 Zhou | e1289ad | 2021-12-28 11:02:56 -0800 | [diff] [blame^] | 22 | namespace phosphor::certs::util |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 23 | { |
| 24 | |
| 25 | ArgumentParser::ArgumentParser(int argc, char** argv) |
| 26 | { |
| 27 | auto option = 0; |
Nan Zhou | cfb5802 | 2021-12-28 11:02:26 -0800 | [diff] [blame] | 28 | while (-1 != |
| 29 | (option = getopt_long(argc, argv, optionstr, options, nullptr))) |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 30 | { |
| 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 | |
| 50 | const 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 | |
| 63 | void 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 Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 69 | std::cerr << " Valid types: client,server,authority\n"; |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 70 | 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 | |
| 76 | const 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 | |
| 85 | const char* ArgumentParser::optionstr = "tepuh?"; |
| 86 | |
| 87 | const std::string ArgumentParser::true_string = "true"; |
| 88 | const std::string ArgumentParser::empty_string = ""; |
| 89 | |
Nan Zhou | e1289ad | 2021-12-28 11:02:56 -0800 | [diff] [blame^] | 90 | } // namespace phosphor::certs::util |