|
| 1 | +// Compiler for PHP (aka KPHP) |
| 2 | +// Copyright (c) 2025 LLC «V Kontakte» |
| 3 | +// Distributed under the GPL v3 License, see LICENSE.notice.txt |
| 4 | + |
| 5 | +#include <cctype> |
| 6 | +#include <cstdint> |
| 7 | +#include <functional> |
| 8 | +#include <string_view> |
| 9 | + |
| 10 | +#include "runtime-common/core/runtime-core.h" |
| 11 | +#include "runtime-light/state/instance-state.h" |
| 12 | +#include "runtime-light/stdlib/server/args-functions.h" |
| 13 | + |
| 14 | +Optional<array<mixed>> f$getopt(const string &short_options, const array<string> &long_options, |
| 15 | + [[maybe_unused]] Optional<std::optional<std::reference_wrapper<string>>> rest_index) noexcept { |
| 16 | + if (const auto &instance_st{InstanceState::get()}; instance_st.image_kind() != ImageKind::CLI) [[unlikely]] { |
| 17 | + return false; |
| 18 | + } |
| 19 | + |
| 20 | + enum class option_kind : uint8_t { flag, required, optional }; |
| 21 | + |
| 22 | + const auto &cli_opts{ComponentState::get().cli_opts}; |
| 23 | + array<mixed> options; |
| 24 | + |
| 25 | + std::string_view short_options_view{short_options.c_str(), short_options.size()}; |
| 26 | + // parse short options |
| 27 | + for (size_t pos = 0; pos < short_options_view.size(); ++pos) { |
| 28 | + if (!std::isalnum(short_options_view[pos])) { |
| 29 | + continue; |
| 30 | + } |
| 31 | + const string option{1, short_options_view[pos]}; |
| 32 | + |
| 33 | + option_kind kind{option_kind::flag}; |
| 34 | + // check that char followed by a colon |
| 35 | + if (pos + 1 < short_options_view.size() && short_options_view[pos + 1] == ':') { |
| 36 | + kind = option_kind::required; |
| 37 | + pos++; |
| 38 | + |
| 39 | + // check that char followed by a two colon |
| 40 | + if (pos + 1 < short_options_view.size() && short_options_view[pos + 1] == ':') { |
| 41 | + kind = option_kind::optional; |
| 42 | + pos++; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (!cli_opts.has_key(option)) [[unlikely]] { |
| 47 | + // option has not been set |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + const mixed &value{cli_opts.get_value(option)}; |
| 52 | + if (kind == option_kind::optional) { |
| 53 | + options.set_value(option, value.empty() ? false : value); |
| 54 | + } else if (kind == option_kind::required && !value.empty()) { |
| 55 | + options.set_value(option, value); |
| 56 | + } else if (kind == option_kind::flag) { |
| 57 | + options.set_value(option, false); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // parse long options |
| 62 | + for (const auto &long_option : long_options) { |
| 63 | + const std::string_view option_view{long_option.get_value().c_str(), long_option.get_value().size()}; |
| 64 | + uint8_t offset{}; |
| 65 | + |
| 66 | + option_kind kind{option_kind::flag}; |
| 67 | + if (option_view.ends_with("::")) { |
| 68 | + kind = option_kind::optional; |
| 69 | + offset = 2; |
| 70 | + } else if (option_view.ends_with(":")) { |
| 71 | + kind = option_kind::required; |
| 72 | + offset = 1; |
| 73 | + } |
| 74 | + const string option{option_view.data(), static_cast<string::size_type>(option_view.size() - offset)}; |
| 75 | + |
| 76 | + if (!cli_opts.has_key(option)) [[unlikely]] { |
| 77 | + // option has not been set |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + const mixed &value{cli_opts.get_value(option)}; |
| 82 | + if (kind == option_kind::optional) { |
| 83 | + options.set_value(option, value.empty() ? false : value); |
| 84 | + } else if (kind == option_kind::required && !value.empty()) { |
| 85 | + options.set_value(option, value); |
| 86 | + } else if (kind == option_kind::flag) { |
| 87 | + options.set_value(option, false); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return options; |
| 92 | +} |
0 commit comments