Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-05 08:51:42

0001 // Copyright (c) 2017-2024, University of Cincinnati, developed by Henry Schreiner
0002 // under NSF AWARD 1414736 and by the respective contributors.
0003 // All rights reserved.
0004 //
0005 // SPDX-License-Identifier: BSD-3-Clause
0006 
0007 #pragma once
0008 
0009 // IWYU pragma: private, include "CLI/CLI.hpp"
0010 
0011 // [CLI11:public_includes:set]
0012 #include <algorithm>
0013 #include <fstream>
0014 #include <iostream>
0015 #include <memory>
0016 #include <string>
0017 #include <vector>
0018 // [CLI11:public_includes:end]
0019 
0020 #include "Error.hpp"
0021 #include "StringTools.hpp"
0022 
0023 namespace CLI {
0024 // [CLI11:config_fwd_hpp:verbatim]
0025 
0026 class App;
0027 
0028 /// Holds values to load into Options
0029 struct ConfigItem {
0030     /// This is the list of parents
0031     std::vector<std::string> parents{};
0032 
0033     /// This is the name
0034     std::string name{};
0035     /// Listing of inputs
0036     std::vector<std::string> inputs{};
0037 
0038     /// The list of parents and name joined by "."
0039     CLI11_NODISCARD std::string fullname() const {
0040         std::vector<std::string> tmp = parents;
0041         tmp.emplace_back(name);
0042         return detail::join(tmp, ".");
0043     }
0044 };
0045 
0046 /// This class provides a converter for configuration files.
0047 class Config {
0048   protected:
0049     std::vector<ConfigItem> items{};
0050 
0051   public:
0052     /// Convert an app into a configuration
0053     virtual std::string to_config(const App *, bool, bool, std::string) const = 0;
0054 
0055     /// Convert a configuration into an app
0056     virtual std::vector<ConfigItem> from_config(std::istream &) const = 0;
0057 
0058     /// Get a flag value
0059     CLI11_NODISCARD virtual std::string to_flag(const ConfigItem &item) const {
0060         if(item.inputs.size() == 1) {
0061             return item.inputs.at(0);
0062         }
0063         if(item.inputs.empty()) {
0064             return "{}";
0065         }
0066         throw ConversionError::TooManyInputsFlag(item.fullname());  // LCOV_EXCL_LINE
0067     }
0068 
0069     /// Parse a config file, throw an error (ParseError:ConfigParseError or FileError) on failure
0070     CLI11_NODISCARD std::vector<ConfigItem> from_file(const std::string &name) const {
0071         std::ifstream input{name};
0072         if(!input.good())
0073             throw FileError::Missing(name);
0074 
0075         return from_config(input);
0076     }
0077 
0078     /// Virtual destructor
0079     virtual ~Config() = default;
0080 };
0081 
0082 /// This converter works with INI/TOML files; to write INI files use ConfigINI
0083 class ConfigBase : public Config {
0084   protected:
0085     /// the character used for comments
0086     char commentChar = '#';
0087     /// the character used to start an array '\0' is a default to not use
0088     char arrayStart = '[';
0089     /// the character used to end an array '\0' is a default to not use
0090     char arrayEnd = ']';
0091     /// the character used to separate elements in an array
0092     char arraySeparator = ',';
0093     /// the character used separate the name from the value
0094     char valueDelimiter = '=';
0095     /// the character to use around strings
0096     char stringQuote = '"';
0097     /// the character to use around single characters and literal strings
0098     char literalQuote = '\'';
0099     /// the maximum number of layers to allow
0100     uint8_t maximumLayers{255};
0101     /// the separator used to separator parent layers
0102     char parentSeparatorChar{'.'};
0103     /// Specify the configuration index to use for arrayed sections
0104     int16_t configIndex{-1};
0105     /// Specify the configuration section that should be used
0106     std::string configSection{};
0107 
0108   public:
0109     std::string
0110     to_config(const App * /*app*/, bool default_also, bool write_description, std::string prefix) const override;
0111 
0112     std::vector<ConfigItem> from_config(std::istream &input) const override;
0113     /// Specify the configuration for comment characters
0114     ConfigBase *comment(char cchar) {
0115         commentChar = cchar;
0116         return this;
0117     }
0118     /// Specify the start and end characters for an array
0119     ConfigBase *arrayBounds(char aStart, char aEnd) {
0120         arrayStart = aStart;
0121         arrayEnd = aEnd;
0122         return this;
0123     }
0124     /// Specify the delimiter character for an array
0125     ConfigBase *arrayDelimiter(char aSep) {
0126         arraySeparator = aSep;
0127         return this;
0128     }
0129     /// Specify the delimiter between a name and value
0130     ConfigBase *valueSeparator(char vSep) {
0131         valueDelimiter = vSep;
0132         return this;
0133     }
0134     /// Specify the quote characters used around strings and literal strings
0135     ConfigBase *quoteCharacter(char qString, char literalChar) {
0136         stringQuote = qString;
0137         literalQuote = literalChar;
0138         return this;
0139     }
0140     /// Specify the maximum number of parents
0141     ConfigBase *maxLayers(uint8_t layers) {
0142         maximumLayers = layers;
0143         return this;
0144     }
0145     /// Specify the separator to use for parent layers
0146     ConfigBase *parentSeparator(char sep) {
0147         parentSeparatorChar = sep;
0148         return this;
0149     }
0150     /// get a reference to the configuration section
0151     std::string &sectionRef() { return configSection; }
0152     /// get the section
0153     CLI11_NODISCARD const std::string &section() const { return configSection; }
0154     /// specify a particular section of the configuration file to use
0155     ConfigBase *section(const std::string &sectionName) {
0156         configSection = sectionName;
0157         return this;
0158     }
0159 
0160     /// get a reference to the configuration index
0161     int16_t &indexRef() { return configIndex; }
0162     /// get the section index
0163     CLI11_NODISCARD int16_t index() const { return configIndex; }
0164     /// specify a particular index in the section to use (-1) for all sections to use
0165     ConfigBase *index(int16_t sectionIndex) {
0166         configIndex = sectionIndex;
0167         return this;
0168     }
0169 };
0170 
0171 /// the default Config is the TOML file format
0172 using ConfigTOML = ConfigBase;
0173 
0174 /// ConfigINI generates a "standard" INI compliant output
0175 class ConfigINI : public ConfigTOML {
0176 
0177   public:
0178     ConfigINI() {
0179         commentChar = ';';
0180         arrayStart = '\0';
0181         arrayEnd = '\0';
0182         arraySeparator = ' ';
0183         valueDelimiter = '=';
0184     }
0185 };
0186 // [CLI11:config_fwd_hpp:end]
0187 }  // namespace CLI