Loading...
Searching...
No Matches
ConfigFwd.hpp
Go to the documentation of this file.
// Copyright (c) 2017-2025, University of Cincinnati, developed by Henry Schreiner
// under NSF AWARD 1414736 and by the respective contributors.
// All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
// IWYU pragma: private, include "CLI/CLI.hpp"
// [CLI11:public_includes:set]
#include <algorithm>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
// [CLI11:public_includes:end]
#include "Error.hpp"
#include "StringTools.hpp"
namespace CLI {
// [CLI11:config_fwd_hpp:verbatim]
class App;
struct ConfigItem {
std::vector<std::string> parents{};
std::string name{};
std::vector<std::string> inputs{};
bool multiline{false};
CLI11_NODISCARD std::string fullname() const {
std::vector<std::string> tmp = parents;
tmp.emplace_back(name);
return detail::join(tmp, ".");
(void)multiline; // suppression for cppcheck false positive
}
};
class Config {
protected:
std::vector<ConfigItem> items{};
public:
virtual std::string to_config(const App *, bool, bool, std::string) const = 0;
virtual std::vector<ConfigItem> from_config(std::istream &) const = 0;
CLI11_NODISCARD virtual std::string to_flag(const ConfigItem &item) const {
if(item.inputs.size() == 1) {
return item.inputs.at(0);
}
if(item.inputs.empty()) {
return "{}";
}
throw ConversionError::TooManyInputsFlag(item.fullname()); // LCOV_EXCL_LINE
}
CLI11_NODISCARD std::vector<ConfigItem> from_file(const std::string &name) const {
std::ifstream input{name};
if(!input.good())
throw FileError::Missing(name);
return from_config(input);
}
virtual ~Config() = default;
};
class ConfigBase : public Config {
protected:
char commentChar = '#';
char arrayStart = '[';
char arrayEnd = ']';
char arraySeparator = ',';
char valueDelimiter = '=';
char stringQuote = '"';
char literalQuote = '\'';
uint8_t maximumLayers{255};
char parentSeparatorChar{'.'};
bool commentDefaultsBool = false;
bool allowMultipleDuplicateFields{false};
int16_t configIndex{-1};
std::string configSection{};
public:
std::string
to_config(const App * /*app*/, bool default_also, bool write_description, std::string prefix) const override;
std::vector<ConfigItem> from_config(std::istream &input) const override;
ConfigBase *comment(char cchar) {
commentChar = cchar;
return this;
}
ConfigBase *arrayBounds(char aStart, char aEnd) {
arrayStart = aStart;
arrayEnd = aEnd;
return this;
}
ConfigBase *arrayDelimiter(char aSep) {
arraySeparator = aSep;
return this;
}
ConfigBase *valueSeparator(char vSep) {
valueDelimiter = vSep;
return this;
}
ConfigBase *quoteCharacter(char qString, char literalChar) {
stringQuote = qString;
literalQuote = literalChar;
return this;
}
ConfigBase *maxLayers(uint8_t layers) {
maximumLayers = layers;
return this;
}
ConfigBase *parentSeparator(char sep) {
parentSeparatorChar = sep;
return this;
}
ConfigBase *commentDefaults(bool comDef = true) {
commentDefaultsBool = comDef;
return this;
}
std::string &sectionRef() { return configSection; }
CLI11_NODISCARD const std::string &section() const { return configSection; }
ConfigBase *section(const std::string &sectionName) {
configSection = sectionName;
return this;
}
int16_t &indexRef() { return configIndex; }
CLI11_NODISCARD int16_t index() const { return configIndex; }
ConfigBase *index(int16_t sectionIndex) {
configIndex = sectionIndex;
return this;
}
ConfigBase *allowDuplicateFields(bool value = true) {
allowMultipleDuplicateFields = value;
return this;
}
};
using ConfigTOML = ConfigBase;
class ConfigINI : public ConfigTOML {
public:
ConfigINI() {
commentChar = ';';
arrayStart = '\0';
arrayEnd = '\0';
arraySeparator = ' ';
valueDelimiter = '=';
}
};
// [CLI11:config_fwd_hpp:end]
} // namespace CLI
Error.hpp
CLI11_NODISCARD
#define CLI11_NODISCARD
Definition Macros.hpp:58
StringTools.hpp
CLI::App
Creates a command line program, with very few defaults.
Definition App.hpp:98
CLI::ConfigBase
This converter works with INI/TOML files; to write INI files use ConfigINI.
Definition ConfigFwd.hpp:85
CLI::ConfigBase::from_config
std::vector< ConfigItem > from_config(std::istream &input) const override
Convert a configuration into an app.
CLI::ConfigBase::configSection
std::string configSection
Specify the configuration section that should be used.
Definition ConfigFwd.hpp:112
CLI::ConfigBase::comment
ConfigBase * comment(char cchar)
Specify the configuration for comment characters.
Definition ConfigFwd.hpp:120
CLI::ConfigBase::arrayDelimiter
ConfigBase * arrayDelimiter(char aSep)
Specify the delimiter character for an array.
Definition ConfigFwd.hpp:131
CLI::ConfigBase::quoteCharacter
ConfigBase * quoteCharacter(char qString, char literalChar)
Specify the quote characters used around strings and literal strings.
Definition ConfigFwd.hpp:141
CLI::ConfigBase::arraySeparator
char arraySeparator
the character used to separate elements in an array
Definition ConfigFwd.hpp:94
CLI::ConfigBase::sectionRef
std::string & sectionRef()
get a reference to the configuration section
Definition ConfigFwd.hpp:162
CLI::ConfigBase::arrayBounds
ConfigBase * arrayBounds(char aStart, char aEnd)
Specify the start and end characters for an array.
Definition ConfigFwd.hpp:125
CLI::ConfigBase::to_config
std::string to_config(const App *, bool default_also, bool write_description, std::string prefix) const override
Convert an app into a configuration.
CLI::ConfigBase::maxLayers
ConfigBase * maxLayers(uint8_t layers)
Specify the maximum number of parents.
Definition ConfigFwd.hpp:147
CLI::ConfigBase::allowDuplicateFields
ConfigBase * allowDuplicateFields(bool value=true)
specify that multiple duplicate arguments should be merged even if not sequential
Definition ConfigFwd.hpp:181
CLI::ConfigBase::section
CLI11_NODISCARD const std::string & section() const
get the section
Definition ConfigFwd.hpp:164
CLI::ConfigBase::commentDefaults
ConfigBase * commentDefaults(bool comDef=true)
comment default value options
Definition ConfigFwd.hpp:157
CLI::ConfigBase::stringQuote
char stringQuote
the character to use around strings
Definition ConfigFwd.hpp:98
CLI::ConfigBase::section
ConfigBase * section(const std::string &sectionName)
specify a particular section of the configuration file to use
Definition ConfigFwd.hpp:166
CLI::ConfigBase::valueSeparator
ConfigBase * valueSeparator(char vSep)
Specify the delimiter between a name and value.
Definition ConfigFwd.hpp:136
CLI::ConfigBase::maximumLayers
uint8_t maximumLayers
the maximum number of layers to allow
Definition ConfigFwd.hpp:102
CLI::ConfigBase::valueDelimiter
char valueDelimiter
the character used separate the name from the value
Definition ConfigFwd.hpp:96
CLI::ConfigBase::arrayStart
char arrayStart
the character used to start an array '\0' is a default to not use
Definition ConfigFwd.hpp:90
CLI::ConfigBase::parentSeparatorChar
char parentSeparatorChar
the separator used to separator parent layers
Definition ConfigFwd.hpp:104
CLI::ConfigBase::allowMultipleDuplicateFields
bool allowMultipleDuplicateFields
specify the config reader should collapse repeated field names to a single vector
Definition ConfigFwd.hpp:108
CLI::ConfigBase::index
CLI11_NODISCARD int16_t index() const
get the section index
Definition ConfigFwd.hpp:174
CLI::ConfigBase::literalQuote
char literalQuote
the character to use around single characters and literal strings
Definition ConfigFwd.hpp:100
CLI::ConfigBase::index
ConfigBase * index(int16_t sectionIndex)
specify a particular index in the section to use (-1) for all sections to use
Definition ConfigFwd.hpp:176
CLI::ConfigBase::indexRef
int16_t & indexRef()
get a reference to the configuration index
Definition ConfigFwd.hpp:172
CLI::ConfigBase::arrayEnd
char arrayEnd
the character used to end an array '\0' is a default to not use
Definition ConfigFwd.hpp:92
CLI::ConfigBase::parentSeparator
ConfigBase * parentSeparator(char sep)
Specify the separator to use for parent layers.
Definition ConfigFwd.hpp:152
CLI::ConfigBase::commentDefaultsBool
bool commentDefaultsBool
comment default values
Definition ConfigFwd.hpp:106
CLI::ConfigBase::configIndex
int16_t configIndex
Specify the configuration index to use for arrayed sections.
Definition ConfigFwd.hpp:110
CLI::ConfigBase::commentChar
char commentChar
the character used for comments
Definition ConfigFwd.hpp:88
CLI::ConfigINI::ConfigINI
ConfigINI()
Definition ConfigFwd.hpp:194
CLI::Config
This class provides a converter for configuration files.
Definition ConfigFwd.hpp:49
CLI::Config::to_flag
virtual CLI11_NODISCARD std::string to_flag(const ConfigItem &item) const
Get a flag value.
Definition ConfigFwd.hpp:61
CLI::Config::items
std::vector< ConfigItem > items
Definition ConfigFwd.hpp:51
CLI::Config::to_config
virtual std::string to_config(const App *, bool, bool, std::string) const =0
Convert an app into a configuration.
CLI::Config::from_file
CLI11_NODISCARD std::vector< ConfigItem > from_file(const std::string &name) const
Parse a config file, throw an error (ParseError:ConfigParseError or FileError) on failure.
Definition ConfigFwd.hpp:72
CLI::Config::~Config
virtual ~Config()=default
Virtual destructor.
CLI::Config::from_config
virtual std::vector< ConfigItem > from_config(std::istream &) const =0
Convert a configuration into an app.
CLI::detail::join
std::string join(const T &v, std::string delim=",")
Simple function to join a string.
Definition StringTools.hpp:54
CLI
Definition App.hpp:36
CLI::ConfigTOML
ConfigBase ConfigTOML
the default Config is the TOML file format
Definition ConfigFwd.hpp:188
CLI::ConfigItem
Holds values to load into Options.
Definition ConfigFwd.hpp:29
CLI::ConfigItem::inputs
std::vector< std::string > inputs
Listing of inputs.
Definition ConfigFwd.hpp:36
CLI::ConfigItem::name
std::string name
This is the name.
Definition ConfigFwd.hpp:34
CLI::ConfigItem::fullname
CLI11_NODISCARD std::string fullname() const
The list of parents and name joined by ".".
Definition ConfigFwd.hpp:40
CLI::ConfigItem::multiline
bool multiline
indicator if a multiline vector separator was inserted
Definition ConfigFwd.hpp:38
CLI::ConfigItem::parents
std::vector< std::string > parents
This is the list of parents.
Definition ConfigFwd.hpp:31