1/* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15// Autogenerated file -- DO NOT EDIT! 16 17#ifndef PANDA_<%= Common::module.name.upcase %>_OPTIONS_GEN_H_ 18#define PANDA_<%= Common::module.name.upcase %>_OPTIONS_GEN_H_ 19 20#include "utils/pandargs.h" 21 22#include <optional> 23#include <string> 24#include <unordered_set> 25#include <vector> 26 27namespace <%= Common::module.namespace %> { 28 29class Options { 30public: 31 class Error { 32 public: 33 explicit Error(std::string msg) : msg_(std::move(msg)) {} 34 35 std::string GetMessage() { 36 return msg_; 37 } 38 private: 39 std::string msg_; 40 }; 41 42 explicit Options(const std::string &exe_path) : exe_dir_(GetExeDir(exe_path)) {} 43 44 void AddOptions(PandArgParser *parser) { 45 parser->Add(&version); 46% Common::options.each do |op| 47% next if op.sub_option? 48% next if op.deprecated? 49 parser->Add(&<%= op.field_name %>); 50% end 51 } 52 53 bool IsVersion() const { 54 return version.GetValue(); 55 } 56 57 void SetVersion(bool value) { 58 version.SetValue(value); 59 } 60 61 bool WasSetVersion() { 62 return version.WasSet(); 63 } 64 65% Common::options.each do |op| 66% if !op.lang_specific? && !op.has_lang_suboptions? 67 <%= op.type %> <%= op.getter_name %>([[maybe_unused]] std::string_view lang = "") const { 68% if op.deprecated? 69 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 70% end 71 return <%= op.field_name %>.GetValue(); 72 } 73 74 void <%= op.setter_name %>(<%= op.type %> value) { 75% if op.deprecated? 76 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 77% end 78 <%= op.field_name %>.SetValue(<%= op.type == 'std::string' || op.type == 'arg_list_t' ? 'std::move(value)' : 'value' %>); 79 } 80 81 bool WasSet<%= op.camel_name %>([[maybe_unused]] std::string_view lang = "") const { 82% if op.deprecated? 83 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 84% end 85 return <%= op.field_name %>.WasSet(); 86 } 87% end 88% if op.has_lang_suboptions? 89 <%= op.type %> <%= op.getter_name %>(std::string_view lang) const { 90% if op.deprecated? 91 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 92% end 93% op.lang.each do |lang| 94 if (lang == "<%= "#{lang}" %>") { 95 return WasSet<%= op.lang_camel_name(lang) %>() ? <%= op.lang_field_name(lang) %>.GetValue() : <%= op.field_name %>.GetValue(); 96 } 97% end 98 return <%= op.field_name %>.GetValue(); 99 } 100 101 void <%= op.setter_name %>(<%= op.type %> value) { 102% if op.deprecated? 103 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 104% end 105 <%= op.field_name %>.SetValue(<%= op.type == 'std::string' || op.type == 'arg_list_t' ? 'std::move(value)' : 'value' %>); 106 } 107 108 bool WasSet<%= op.camel_name %>(std::string_view lang) const { 109% if op.deprecated? 110 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 111% end 112% op.lang.each do |lang| 113 if (lang == "<%= "#{lang}" %>") { 114 return <%= op.lang_field_name(lang) %>.WasSet(); 115 } 116% end 117 return <%= op.field_name %>.WasSet(); 118 } 119% end 120% end 121// NOLINTNEXTLINE(readability-function-size) 122 std::optional<Error> Validate() const { 123% Common::options.each do |op| 124% next unless defined? op.possible_values 125% is_string = op.type == 'std::string' || op.type == 'arg_list_t' 126% possible_values = op.possible_values.map { |e| is_string ? Common::to_raw(e) : e }.join(', ') 127 { 128 std::unordered_set<<%= is_string ? "std::string" : op.type %>> possible_values{<%= possible_values %>}; 129% if op.type != 'arg_list_t' 130 std::vector<<%= op.type %>> values{<%= op.field_name %>.GetValue()}; 131% else 132 const auto &values = <%= op.field_name %>.GetValue(); 133% end 134 for (const auto &value : values) { 135 if (possible_values.find(value) == possible_values.cend()) { 136 return Error("argument --<%= op.name %>: invalid value: '" + <%= is_string ? "value" : "std::to_string(value)" %> + \ 137 R"('. Possible values: <%= op.possible_values %>)"); 138 } 139 } 140 } 141% end 142 return {}; 143 } 144 145private: 146% Common::options.each do |op| 147% if op.lang_specific? 148 bool WasSet<%= op.camel_name %>() const { 149% if op.deprecated? 150 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 151% end 152 return <%= op.field_name %>.WasSet(); 153 } 154% end 155% end 156 157 static std::string GetExeDir(const std::string &exe_path) { 158 auto pos = exe_path.find_last_of('/'); 159 return exe_path.substr(0, pos); 160 } 161 162% Common::options.each do |op| 163% next unless op.need_default_constant 164 static constexpr <%= op.type %> <%= op.default_constant_name %> = <%= op.default %>; 165 166% end 167 std::string exe_dir_; 168 PandArg<bool> version{"version", false, R"(Ark version, file format version and minimum supported file format version)"}; 169% Common::options.each do |op| 170% if defined? op.delimiter 171 PandArg<<%= op.type %>> <%= op.field_name %>{"<%= op.name %>", <%= op.default_value %>, <%= op.full_description %>, "<%= op.delimiter %>"}; 172% elsif defined? op.range 173% min, max = op.range.match('(\d+)\D*(\d+)').captures; 174% if op.default.to_i > max.to_i || op.default.to_i < min.to_i 175% abort "FAILED: Default value of argument " + op.name + " has out of range default parameter" 176% end 177 PandArg<<%= op.type %>> <%= op.field_name %>{"<%= op.name %>", <%= op.default_value %>, <%= op.full_description %>, <%= min.to_i %>, <%= max.to_i %>}; 178% elsif op.has_sub_options 179 PandArgCompound <%= op.field_name %>{"<%= op.name %>", <%= op.full_description %>, {<%= op.sub_options.map{|o| '&' + o.field_name}.join(', ') %>}}; 180% else 181 PandArg<<%= op.type %>> <%= op.field_name %>{"<%= op.name %>", <%= op.default_value %>, <%= op.full_description %>}; 182% end 183% end 184}; 185 186} // namespace <%= Common::module.namespace %> 187 188#endif // PANDA_<%= Common::module.name.upcase %>_OPTIONS_GEN_H_ 189