.. _program_listing_file_src_common_config_parser.h: Program Listing for File config_parser.h ======================================== |exhale_lsh| :ref:`Return to documentation for file ` (``src/common/config_parser.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #pragma once #include "3rd_party/yaml-cpp/yaml.h" #include "common/cli_wrapper.h" #include "common/definitions.h" // TODO: why are these needed by a config parser? Can they be removed for Linux // as well? #ifndef _WIN32 #include #include #endif namespace marian { namespace cli { enum struct mode { training, translation, scoring, server, embedding }; } // namespace cli class ConfigParser { public: ConfigParser(cli::mode mode); ConfigParser(int argc, char** argv, cli::mode mode, bool validate = false) : ConfigParser(mode) { parseOptions(argc, argv, validate); } template ConfigParser& addOption(const std::string& args, const std::string& group, const std::string& help, const T val) { std::string previous_group = cli_.switchGroup(group); cli_.add(args,help,val); cli_.switchGroup(previous_group); return *this; } template ConfigParser& addOption(const std::string& args, const std::string& group, const std::string& help, const T val, const T implicit_val) { std::string previous_group = cli_.switchGroup(group); cli_.add(args,help,val)->implicit_val(implicit_val); cli_.switchGroup(previous_group); return *this; } template ConfigParser& addOption(const std::string& args, const std::string& group, const std::string& help) { std::string previous_group = cli_.switchGroup(group); cli_.add(args,help); cli_.switchGroup(previous_group); return *this; } Ptr parseOptions(int argc, char** argv, bool validate); YAML::Node const& getConfig() const; cli::mode getMode() const; std::string const& cmdLine() const; private: cli::CLIWrapper cli_; cli::mode mode_; YAML::Node config_; std::string cmdLine_; // Check if the config contains value for option key bool has(const std::string& key) const { return (bool)config_[key]; } // Return value for given option key cast to given type. // Abort if not set. template T get(const std::string& key) const { ABORT_IF(!has(key), "CLI object has no key '{}'", key); return config_[key].as(); } void addOptionsGeneral(cli::CLIWrapper&); void addOptionsServer(cli::CLIWrapper&); void addOptionsModel(cli::CLIWrapper&); void addOptionsTraining(cli::CLIWrapper&); void addOptionsValidation(cli::CLIWrapper&); void addOptionsTranslation(cli::CLIWrapper&); void addOptionsScoring(cli::CLIWrapper&); void addOptionsEmbedding(cli::CLIWrapper&); void addAliases(cli::CLIWrapper&); void addSuboptionsDevices(cli::CLIWrapper&); void addSuboptionsBatching(cli::CLIWrapper&); void addSuboptionsInputLength(cli::CLIWrapper&); void addSuboptionsTSV(cli::CLIWrapper&); void addSuboptionsULR(cli::CLIWrapper&); void addSuboptionsQuantization(cli::CLIWrapper&); // Extract paths to all config files found in the config object. // Look at --config option and model.npz.yml files. std::vector findConfigPaths(); // Load options from config files. // Handle environment variables and relative paths. YAML::Node loadConfigFiles(const std::vector&); }; } // namespace marian